- Upgrade to Nuxt 4 (sources under app/), add @nuxt/ui, Tailwind v4 and Lucide icons; drop @nuxtjs/tailwindcss - Replace the custom Yale* components, Modal, Navbar and icon components with UButton/UInput/UModal/UTable/UCard/UHeader/UBadge/UAlert and toasts - Add dark/light mode toggle and a logout button - Add a useApi composable: central base URL/credentials, JSON-encoded bodies, error handling, and redirect to login when the session expires - Fix modal listener leak, missing People heading, unhandled fetch errors and login redirect for already-authenticated users - Make the code and people tables fit on phone screens - Retake README screenshots
229 lines
7.8 KiB
Vue
229 lines
7.8 KiB
Vue
<script setup lang="ts">
|
|
import type { TableColumn } from '@nuxt/ui'
|
|
import { UserCodeStatus, type Person, type YaleUserCode } from '~/types/yale'
|
|
|
|
type ModalKind = 'show' | 'edit' | 'clear' | 'send'
|
|
|
|
const api = useApi()
|
|
const toast = useToast()
|
|
|
|
const userCodes = ref<YaleUserCode[]>([])
|
|
const people = ref<{ label: string, value: string }[]>([])
|
|
const loading = ref(true)
|
|
const saving = ref(false)
|
|
|
|
// Only one modal is ever open, for one code at a time
|
|
const activeModal = ref<ModalKind | null>(null)
|
|
const activeCode = ref<YaleUserCode | null>(null)
|
|
const newCode = ref('')
|
|
const newCodeError = ref('')
|
|
const personNumber = ref<string | undefined>(undefined)
|
|
|
|
const columns: TableColumn<YaleUserCode>[] = [
|
|
{ accessorKey: 'id', header: 'Code ID' },
|
|
{ accessorKey: 'status', header: 'Status' },
|
|
{ id: 'actions', header: 'Actions' }
|
|
]
|
|
|
|
const statusDisplay: Record<UserCodeStatus, { label: string, color: 'success' | 'neutral' | 'error' }> = {
|
|
[UserCodeStatus.AVAILABLE]: { label: 'Available', color: 'neutral' },
|
|
[UserCodeStatus.ENABLED]: { label: 'Enabled', color: 'success' },
|
|
[UserCodeStatus.DISABLED]: { label: 'Disabled', color: 'error' }
|
|
}
|
|
|
|
const modalOpen = (kind: ModalKind) => activeModal.value === kind
|
|
const setModalOpen = (open: boolean) => {
|
|
if (!open) activeModal.value = null
|
|
}
|
|
|
|
const loadAll = async () => {
|
|
loading.value = true
|
|
const [codes, peopleResponse] = await Promise.all([
|
|
api.get<YaleUserCode[]>('/api/Yale/codes'),
|
|
api.get<Person[]>('/api/People')
|
|
])
|
|
|
|
if (codes.success) {
|
|
userCodes.value = (codes.data ?? []).sort((a, b) => a.id - b.id)
|
|
} else {
|
|
toast.add({ title: 'Could not load codes', description: codes.error, color: 'error' })
|
|
}
|
|
|
|
if (peopleResponse.success) {
|
|
people.value = (peopleResponse.data ?? []).map(x => ({ label: x.name, value: x.phoneNumber }))
|
|
}
|
|
loading.value = false
|
|
}
|
|
|
|
onMounted(loadAll)
|
|
|
|
const openModal = (kind: ModalKind, code: YaleUserCode) => {
|
|
activeCode.value = code
|
|
newCode.value = ''
|
|
newCodeError.value = ''
|
|
personNumber.value = undefined
|
|
activeModal.value = kind
|
|
}
|
|
|
|
const confirmUpdateCode = async () => {
|
|
const code = activeCode.value
|
|
if (!code) return
|
|
newCodeError.value = ''
|
|
|
|
if (!/^\d{4,6}$/.test(newCode.value)) {
|
|
newCodeError.value = 'Code must be 4, 5 or 6 digits long'
|
|
return
|
|
}
|
|
|
|
saving.value = true
|
|
const response = await api.post<boolean>(`/api/Yale/code/${code.id}`, newCode.value)
|
|
saving.value = false
|
|
|
|
if (response.success) {
|
|
code.code = newCode.value
|
|
code.status = UserCodeStatus.ENABLED
|
|
activeModal.value = null
|
|
toast.add({ title: `Code #${code.id} updated`, color: 'success' })
|
|
} else {
|
|
toast.add({ title: 'Could not update code', description: response.error, color: 'error' })
|
|
}
|
|
}
|
|
|
|
const confirmClearCode = async () => {
|
|
const code = activeCode.value
|
|
if (!code) return
|
|
|
|
saving.value = true
|
|
const response = await api.post<boolean>(`/api/Yale/code/${code.id}/status`)
|
|
saving.value = false
|
|
|
|
if (response.success) {
|
|
code.code = ''
|
|
code.status = UserCodeStatus.AVAILABLE
|
|
activeModal.value = null
|
|
toast.add({ title: `Code #${code.id} deleted`, color: 'success' })
|
|
} else {
|
|
toast.add({ title: 'Could not delete code', description: response.error, color: 'error' })
|
|
}
|
|
}
|
|
|
|
const confirmSendCode = async () => {
|
|
const code = activeCode.value
|
|
if (!code || !personNumber.value) return
|
|
|
|
saving.value = true
|
|
const response = await api.post<boolean>(`/api/Yale/code/${code.id}/send`, personNumber.value)
|
|
saving.value = false
|
|
|
|
if (response.success) {
|
|
activeModal.value = null
|
|
toast.add({ title: 'Code sent successfully', color: 'success' })
|
|
} else {
|
|
toast.add({ title: 'Could not send code', description: response.error, color: 'error' })
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div>
|
|
<UTable
|
|
:data="userCodes" :columns="columns" :loading="loading"
|
|
:ui="{ th: 'px-2 whitespace-nowrap', td: 'px-2' }"
|
|
>
|
|
<template #id-cell="{ row }">
|
|
<span class="inline-flex items-center gap-1">
|
|
{{ row.original.id }}
|
|
<UIcon v-if="row.original.isHome" name="i-lucide-house" class="size-4" aria-label="Home code" />
|
|
</span>
|
|
</template>
|
|
|
|
<template #status-cell="{ row }">
|
|
<UBadge
|
|
:color="statusDisplay[row.original.status]?.color ?? 'neutral'"
|
|
variant="subtle"
|
|
>
|
|
{{ statusDisplay[row.original.status]?.label ?? 'Unknown' }}
|
|
</UBadge>
|
|
</template>
|
|
|
|
<template #actions-cell="{ row }">
|
|
<div class="flex">
|
|
<UButton
|
|
color="neutral" variant="ghost" size="sm" icon="i-lucide-eye" aria-label="Show code"
|
|
:disabled="row.original.status !== UserCodeStatus.ENABLED"
|
|
@click="openModal('show', row.original)"
|
|
/>
|
|
<UButton
|
|
color="neutral" variant="ghost" size="sm" icon="i-lucide-pencil" aria-label="Edit code"
|
|
@click="openModal('edit', row.original)"
|
|
/>
|
|
<template v-if="!row.original.isHome">
|
|
<UButton
|
|
color="neutral" variant="ghost" size="sm" icon="i-lucide-send" aria-label="Send code"
|
|
:disabled="row.original.status !== UserCodeStatus.ENABLED"
|
|
@click="openModal('send', row.original)"
|
|
/>
|
|
<UButton
|
|
color="error" variant="ghost" size="sm" icon="i-lucide-trash-2" aria-label="Delete code"
|
|
:disabled="row.original.status !== UserCodeStatus.ENABLED"
|
|
@click="openModal('clear', row.original)"
|
|
/>
|
|
</template>
|
|
</div>
|
|
</template>
|
|
</UTable>
|
|
|
|
<!-- Show -->
|
|
<UModal :open="modalOpen('show')" :title="`User Code #${activeCode?.id}`" @update:open="setModalOpen">
|
|
<template #body>
|
|
<p class="text-2xl font-mono tracking-widest text-center">{{ activeCode?.code }}</p>
|
|
</template>
|
|
</UModal>
|
|
|
|
<!-- Edit -->
|
|
<UModal :open="modalOpen('edit')" :title="`Edit User Code #${activeCode?.id}`" @update:open="setModalOpen">
|
|
<template #body>
|
|
<form id="edit-code" class="space-y-4" @submit.prevent="confirmUpdateCode">
|
|
<UAlert
|
|
v-if="activeCode?.isHome"
|
|
color="warning" variant="subtle" icon="i-lucide-triangle-alert"
|
|
title="You are about to edit the home code, are you sure?"
|
|
/>
|
|
<UFormField :error="newCodeError || undefined">
|
|
<UInput
|
|
v-model="newCode" placeholder="New Code" inputmode="numeric" maxlength="6"
|
|
autocomplete="off" class="w-full" autofocus
|
|
/>
|
|
</UFormField>
|
|
</form>
|
|
</template>
|
|
<template #footer>
|
|
<UButton color="neutral" variant="outline" @click="activeModal = null">Cancel</UButton>
|
|
<UButton type="submit" form="edit-code" :loading="saving">Update Code</UButton>
|
|
</template>
|
|
</UModal>
|
|
|
|
<!-- Delete -->
|
|
<UModal :open="modalOpen('clear')" :title="`Delete User Code #${activeCode?.id}`" @update:open="setModalOpen">
|
|
<template #body>
|
|
<p>Are you sure you want to delete this code?</p>
|
|
</template>
|
|
<template #footer>
|
|
<UButton color="neutral" variant="outline" @click="activeModal = null">Cancel</UButton>
|
|
<UButton color="error" :loading="saving" @click="confirmClearCode">Delete Code</UButton>
|
|
</template>
|
|
</UModal>
|
|
|
|
<!-- Send -->
|
|
<UModal :open="modalOpen('send')" :title="`Send User Code #${activeCode?.id}`" @update:open="setModalOpen">
|
|
<template #body>
|
|
<USelect v-model="personNumber" :items="people" placeholder="Select a person" class="w-full" />
|
|
</template>
|
|
<template #footer>
|
|
<UButton color="neutral" variant="outline" @click="activeModal = null">Cancel</UButton>
|
|
<UButton :loading="saving" :disabled="!personNumber" @click="confirmSendCode">Send Code</UButton>
|
|
</template>
|
|
</UModal>
|
|
</div>
|
|
</template>
|