Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: Environment view now show variable count #340

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions apps/shelve/app/pages/environments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type Environment, TeamRole } from '@shelve/types'
import { ConfirmModal } from '#components'
import { hasAccess } from '~/utils/hasAccess'

const teamEnv = useTeamEnv()
const teamEnv = ref<Environment[]>([])
const teamId = useTeamId()
const teamRole = useTeamRole()

Expand All @@ -13,21 +13,37 @@ const {

const newEnv = ref('')
const loading = ref(false)
const ceateLoading = ref(false)
const updateLoading = ref(false)

const columns = [
{
accessorKey: 'name',
header: 'Name',
},
{
accessorKey: 'variablesCount',
header: 'Variable Count',
},
{
accessorKey: 'actions',
header: 'Actions',
}
]

async function create() {
async function fetchEnvironments() {
loading.value = true
try {
teamEnv.value = await $fetch<Environment[]>(`/api/environments?teamId=${ teamId.value }`)
} catch (error) {
console.error(error)
}
loading.value = false
}
fetchEnvironments()

async function create() {
ceateLoading.value = true
try {
if (!newEnv.value) {
toast.error('Environment name is required')
Expand All @@ -43,12 +59,12 @@ async function create() {
name: newEnv.value
},
})
await fetchTeams()
await fetchEnvironments()
newEnv.value = ''
} catch (error) {
console.error(error)
}
loading.value = false
ceateLoading.value = false
}

async function updateEnv(env: Environment) {
Expand All @@ -59,15 +75,15 @@ async function updateEnv(env: Environment) {
name: env.name
},
})
await fetchTeams()
await fetchEnvironments()
updateLoading.value = false
}

async function deleteEnv(environments: Environment) {
await $fetch(`/api/teams/${teamId.value}/environments/${environments.id}`, {
method: 'DELETE',
})
await fetchTeams()
await fetchEnvironments()
}

const modal = useModal()
Expand Down Expand Up @@ -121,14 +137,17 @@ function updateEnvironment(env: Environment) {
</div>
<form class="flex items-center gap-2" @submit.prevent="createEnvironment">
<UInput v-model="newEnv" placeholder="New environment name" required />
<UButton label="Create" color="primary" :loading size="sm" type="submit" />
<UButton label="Create" color="primary" :loading="ceateLoading" size="sm" type="submit" />
</form>
</div>
<div style="--stagger: 2" data-animate class="mt-6">
<UTable :data="teamEnv" :columns>
<UTable :data="teamEnv" :columns :loading>
<template #name-cell="{ row }">
{{ capitalize(row.original.name) }}
</template>
<template #variableCount-cell="{ row }">
{{ row.original.variablesCount }}
</template>
<template #actions-cell="{ row }">
<div class="flex gap-2">
<UPopover arrow>
Expand Down
7 changes: 6 additions & 1 deletion apps/shelve/server/services/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export class EnvironmentsService {
async getEnvironments(teamId: number): Promise<Environment[]> {
return await useDrizzle().query.environments.findMany({
where: eq(tables.environments.teamId, teamId),
orderBy: [asc(tables.environments.name)]
orderBy: [asc(tables.environments.name)],
extras: {
variablesCount: useDrizzle()
.$count(tables.variableValues, eq(tables.variableValues.environmentId, tables.environments.id))
.as('variablesCount'),
},
})
}

Expand Down
1 change: 1 addition & 0 deletions packages/types/src/Environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Environment = {
id: number
name: string
teamId: number
variablesCount?: number
createdAt: Date
updatedAt: Date
}
Expand Down