Migrate frontend to Nuxt 4 and Nuxt UI
- 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
This commit is contained in:
1 parent
d0425c8cb2
commit
a9bef7b50d
22 files changed
+4732
-3472
No files matched your search
@@ -1,300 +1,228 @@
|
||||
<script setup lang='ts'>
|
||||
import type { ApiResponse } from '@/types/api-response';
|
||||
import UserCodeListRow from './UserCodeListRow.vue';
|
||||
import { onMounted, ref } from 'vue';
|
||||
import { UserCodeStatus, type Person, type YaleUserCode } from '@/types/yale';
|
||||
<script setup lang="ts">
|
||||
import type { TableColumn } from '@nuxt/ui'
|
||||
import { UserCodeStatus, type Person, type YaleUserCode } from '~/types/yale'
|
||||
|
||||
const userCodes = ref([] as YaleUserCode[]);
|
||||
const userCode = ref({} as YaleUserCode);
|
||||
const people = ref([] as { label: string, value: string }[]);
|
||||
const personNumber = ref('');
|
||||
const loading = ref(true);
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
type ModalKind = 'show' | 'edit' | 'clear' | 'send'
|
||||
|
||||
// Modals
|
||||
const showModal = ref(false);
|
||||
const editModal = ref(false);
|
||||
const clearModal = ref(false);
|
||||
const sendModal = ref(false);
|
||||
const api = useApi()
|
||||
const toast = useToast()
|
||||
|
||||
// Form Fields
|
||||
const newCode = ref('');
|
||||
const newCodeError = ref('');
|
||||
const userCodes = ref<YaleUserCode[]>([])
|
||||
const people = ref<{ label: string, value: string }[]>([])
|
||||
const loading = ref(true)
|
||||
const saving = ref(false)
|
||||
|
||||
// When the component is mounted, load the user codes
|
||||
onMounted(async () => {
|
||||
// Load the user codes and set the loading status when complete
|
||||
await loadUserCodes();
|
||||
await loadPeople();
|
||||
loading.value = 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 loadUserCodes = async () => {
|
||||
// Send a request to the api to get the user codes
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Yale/codes`, { credentials: 'include' });
|
||||
const columns: TableColumn<YaleUserCode>[] = [
|
||||
{ accessorKey: 'id', header: 'Code ID' },
|
||||
{ accessorKey: 'status', header: 'Status' },
|
||||
{ id: 'actions', header: 'Actions' }
|
||||
]
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<YaleUserCode[]>;
|
||||
|
||||
// Set the user codes by id
|
||||
userCodes.value = apiResponse.data?.sort((a, b) => a.id - b.id) || [];
|
||||
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 loadPeople = async () => {
|
||||
// Send a request to the api to get the people
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/People`, { credentials: 'include' });
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<Person[]>;
|
||||
|
||||
// Set the people for drop down
|
||||
people.value = apiResponse.data?.map(x => ({ label: x.name, value: x.phoneNumber })) || [];
|
||||
const modalOpen = (kind: ModalKind) => activeModal.value === kind
|
||||
const setModalOpen = (open: boolean) => {
|
||||
if (!open) activeModal.value = null
|
||||
}
|
||||
|
||||
const confirmUpdateCode = async (id: number, code: string) => {
|
||||
// Reset the error
|
||||
newCodeError.value = '';
|
||||
const loadAll = async () => {
|
||||
loading.value = true
|
||||
const [codes, peopleResponse] = await Promise.all([
|
||||
api.get<YaleUserCode[]>('/api/Yale/codes'),
|
||||
api.get<Person[]>('/api/People')
|
||||
])
|
||||
|
||||
// Validate the new code first
|
||||
if (!/^\d{4,6}$/.test(code)) {
|
||||
newCodeError.value = 'Code must be 4, 5 or 6 digits long';
|
||||
return;
|
||||
}
|
||||
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' })
|
||||
}
|
||||
|
||||
// Set the loading status
|
||||
loading.value = true;
|
||||
|
||||
// Send a request to the api, the body will just be the new code
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Yale/code/${id}`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: `"${code}"`
|
||||
});
|
||||
|
||||
// Parse the result
|
||||
const apiResponse = await response.json() as ApiResponse<boolean>;
|
||||
|
||||
if (apiResponse.success) {
|
||||
// If the response was successful update the code in the list
|
||||
const index = userCodes.value.findIndex(x => x.id === id);
|
||||
userCodes.value[index].code = code;
|
||||
userCodes.value[index].status = UserCodeStatus.ENABLED;
|
||||
} else {
|
||||
// Otherwise display an error to the user.
|
||||
alert(apiResponse.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
// Reset the new code
|
||||
newCode.value = '';
|
||||
|
||||
// Close the edit modal
|
||||
editModal.value = false;
|
||||
|
||||
// Set the loading status
|
||||
loading.value = false;
|
||||
if (peopleResponse.success) {
|
||||
people.value = (peopleResponse.data ?? []).map(x => ({ label: x.name, value: x.phoneNumber }))
|
||||
}
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
const confirmClearCode = async (id: number) => {
|
||||
// Set the loading status
|
||||
loading.value = true;
|
||||
onMounted(loadAll)
|
||||
|
||||
// Send a request to the api
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Yale/code/${id}/status`, {
|
||||
method: 'POST',
|
||||
credentials: 'include'
|
||||
});
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<boolean>;
|
||||
|
||||
if (apiResponse.success) {
|
||||
// If the response was successful set the code to available in the list
|
||||
const index = userCodes.value.findIndex(x => x.id === id);
|
||||
userCodes.value[index].code = '';
|
||||
userCodes.value[index].status = UserCodeStatus.AVAILABLE;
|
||||
} else {
|
||||
// Otherwise display an error to the user.
|
||||
alert(apiResponse.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
// Close the clear modal
|
||||
clearModal.value = false;
|
||||
|
||||
// Set the loading status
|
||||
loading.value = false;
|
||||
const openModal = (kind: ModalKind, code: YaleUserCode) => {
|
||||
activeCode.value = code
|
||||
newCode.value = ''
|
||||
newCodeError.value = ''
|
||||
personNumber.value = undefined
|
||||
activeModal.value = kind
|
||||
}
|
||||
|
||||
const handleShowCode = (id: number) => {
|
||||
// Get the user code to show
|
||||
const code = userCodes.value.find(x => x.id === id);
|
||||
const confirmUpdateCode = async () => {
|
||||
const code = activeCode.value
|
||||
if (!code) return
|
||||
newCodeError.value = ''
|
||||
|
||||
// If the code is not found then show an error to the user
|
||||
if (!code) {
|
||||
alert('Code not found');
|
||||
return;
|
||||
}
|
||||
if (!/^\d{4,6}$/.test(newCode.value)) {
|
||||
newCodeError.value = 'Code must be 4, 5 or 6 digits long'
|
||||
return
|
||||
}
|
||||
|
||||
// Set the user code to show
|
||||
userCode.value = code;
|
||||
saving.value = true
|
||||
const response = await api.post<boolean>(`/api/Yale/code/${code.id}`, newCode.value)
|
||||
saving.value = false
|
||||
|
||||
// Show the show-modal
|
||||
showModal.value = true;
|
||||
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 handleUpdateCode = (id: number) => {
|
||||
// Get the user code to update
|
||||
const code = userCodes.value.find(x => x.id === id);
|
||||
const confirmClearCode = async () => {
|
||||
const code = activeCode.value
|
||||
if (!code) return
|
||||
|
||||
// If the code is not found then show an error to the user
|
||||
if (!code) {
|
||||
alert('Code not found');
|
||||
return;
|
||||
}
|
||||
saving.value = true
|
||||
const response = await api.post<boolean>(`/api/Yale/code/${code.id}/status`)
|
||||
saving.value = false
|
||||
|
||||
// Set the user code to update
|
||||
userCode.value = code;
|
||||
|
||||
// Show the edit-modal
|
||||
editModal.value = true;
|
||||
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 handleClearCode = (id: number) => {
|
||||
// Get the user code to clear
|
||||
const code = userCodes.value.find(x => x.id === id);
|
||||
const confirmSendCode = async () => {
|
||||
const code = activeCode.value
|
||||
if (!code || !personNumber.value) return
|
||||
|
||||
// If the code is not found then show an error to the user
|
||||
if (!code) {
|
||||
alert('Code not found');
|
||||
return;
|
||||
}
|
||||
saving.value = true
|
||||
const response = await api.post<boolean>(`/api/Yale/code/${code.id}/send`, personNumber.value)
|
||||
saving.value = false
|
||||
|
||||
// Set the user code to clear
|
||||
userCode.value = code;
|
||||
|
||||
// Show the clear-modal
|
||||
clearModal.value = true;
|
||||
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' })
|
||||
}
|
||||
}
|
||||
|
||||
const handleSendCode = (id: number) => {
|
||||
// Get the user code to send
|
||||
const code = userCodes.value.find(x => x.id === id);
|
||||
|
||||
// If the code is not found then show an error to the user
|
||||
if (!code) {
|
||||
alert('Code not found');
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the user code
|
||||
userCode.value = code;
|
||||
|
||||
// Show the send modal
|
||||
sendModal.value = true;
|
||||
}
|
||||
|
||||
const confirmSendCode = async (id: number) => {
|
||||
// Set the loading status
|
||||
loading.value = true;
|
||||
|
||||
if (!personNumber.value) {
|
||||
alert('Please select a person to send the code to');
|
||||
loading.value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Send a request to the api to send the code
|
||||
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/Yale/code/${id}/send`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: `"${personNumber.value}"`
|
||||
});
|
||||
|
||||
// Parse the response
|
||||
const apiResponse = await response.json() as ApiResponse<boolean>;
|
||||
|
||||
if (apiResponse.success) {
|
||||
alert('Code sent successfully');
|
||||
} else {
|
||||
alert(apiResponse.error ?? 'Unknown error');
|
||||
}
|
||||
|
||||
// Close the clear modal
|
||||
sendModal.value = false;
|
||||
|
||||
// Clear the person number and user code
|
||||
personNumber.value = '';
|
||||
userCode.value = {} as YaleUserCode;
|
||||
|
||||
// Set the loading status
|
||||
loading.value = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<!-- Handle loading state -->
|
||||
<div v-if="loading">Loading...</div>
|
||||
<table v-else class="w-full text-left">
|
||||
<tr>
|
||||
<th scope="col">Code ID</th>
|
||||
<th scope="col">Status</th>
|
||||
<th scope="col">Actions</th>
|
||||
</tr>
|
||||
<UserCodeListRow v-for="userCode in userCodes" :key="userCode.id" :user-code="userCode"
|
||||
@update-code="handleUpdateCode" @clear-code="handleClearCode" @show-code="handleShowCode"
|
||||
@send-code="handleSendCode" />
|
||||
</table>
|
||||
<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>
|
||||
|
||||
<!-- Show modal -->
|
||||
<Modal v-if="showModal" @close="showModal = false">
|
||||
<template #title>
|
||||
User Code # {{ userCode.id }}
|
||||
</template>
|
||||
<template #default>
|
||||
Code: {{ userCode.code }}
|
||||
</template>
|
||||
</Modal>
|
||||
<template #status-cell="{ row }">
|
||||
<UBadge
|
||||
:color="statusDisplay[row.original.status]?.color ?? 'neutral'"
|
||||
variant="subtle"
|
||||
>
|
||||
{{ statusDisplay[row.original.status]?.label ?? 'Unknown' }}
|
||||
</UBadge>
|
||||
</template>
|
||||
|
||||
<!-- Edit modal -->
|
||||
<Modal v-if="editModal" @close="editModal = false">
|
||||
<template #title>
|
||||
Edit User Code # {{ userCode.id }}
|
||||
</template>
|
||||
<template #default>
|
||||
<YaleFormInput v-model="newCode" type="text" placeholder="New Code" class="block w-full" />
|
||||
<p class="text-red-600 mt-3" v-if="newCodeError">{{ newCodeError }}</p>
|
||||
<YaleButton @click="confirmUpdateCode(userCode.id, newCode)" class="w-full mt-3">Update Code</YaleButton>
|
||||
</template>
|
||||
<template #footer v-if="userCode.isHome">
|
||||
<p class="text-red-600 mt-3">You are about to edit the home code, are you sure?</p>
|
||||
</template>
|
||||
</Modal>
|
||||
<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>
|
||||
|
||||
<!-- Clear modal -->
|
||||
<Modal v-if="clearModal" @close="clearModal = false">
|
||||
<template #title>
|
||||
Delete User Code # {{ userCode.id }}
|
||||
</template>
|
||||
<template #default>
|
||||
<p>Are you sure you want to delete this code?</p>
|
||||
<YaleButton @click="confirmClearCode(userCode.id)" class="w-full mt-3">Delete Code</YaleButton>
|
||||
</template>
|
||||
</Modal>
|
||||
<!-- 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>
|
||||
|
||||
<!-- Send modal -->
|
||||
<Modal v-if="sendModal" @close="sendModal = false">
|
||||
<template #title>
|
||||
Send User Code # {{ userCode.id }}
|
||||
</template>
|
||||
<template #default>
|
||||
<YaleFormSelect v-model="personNumber" :options="people" class="block w-full"
|
||||
placeholder="Select a Person" />
|
||||
<YaleButton @click="confirmSendCode(userCode.id)" class="w-full mt-3">Send Code</YaleButton>
|
||||
</template>
|
||||
</Modal>
|
||||
</template>
|
||||
<!-- 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>
|
||||
Reference in new issue
Block a user