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:
LiamPietralla committed 2026-11-16 12:23:31 +11:00
1 parent d0425c8cb2
commit a9bef7b50d
22 files changed
+4732 -3472

No files matched your search

+90 -113
View File
@@ -1,132 +1,109 @@
<script setup lang="ts">
import { type Person } from '@/types/yale';
import { onMounted, ref } from 'vue';
import type { ApiResponse } from '~/types/api-response';
import type { FormError, TableColumn } from '@nuxt/ui'
import type { Person } from '~/types/yale'
const people = ref([] as Person[]);
const person = ref({ id: 0, name: '', phoneNumber: '' } as Person);
const loading = ref(true);
const runtimeConfig = useRuntimeConfig();
const api = useApi()
const toast = useToast()
const addModal = ref(false);
const people = ref<Person[]>([])
const loading = ref(true)
const saving = ref(false)
const addOpen = ref(false)
const newPerson = reactive({ name: '', phoneNumber: '' })
// When the component is mounted, load the people
onMounted(async () => {
await loadPeople();
loading.value = false;
});
const columns: TableColumn<Person>[] = [
{ accessorKey: 'id', header: 'Person ID' },
{ accessorKey: 'name', header: 'Name' },
{ accessorKey: 'phoneNumber', header: 'Phone Number' },
{ id: 'actions', header: 'Actions' }
]
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
people.value = apiResponse.data || [];
}
const handleAddPerson = () => {
addModal.value = true;
}
const handleSavePerson = async () => {
// Validate the person fields
if (!person.value.name || !person.value.phoneNumber) {
alert('Name and phone number are required');
return;
}
// Set the loading status
loading.value = true;
// Send a request to the api to save the person
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/People`, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(person.value)
});
// Parse the response
const apiResponse = await response.json() as ApiResponse<Person>;
if (apiResponse.success) {
// Add the new person to the list
people.value.push(apiResponse.data!);
addModal.value = false;
loading.value = true
const response = await api.get<Person[]>('/api/People')
if (response.success) {
people.value = response.data ?? []
} else {
alert(apiResponse.error ?? 'An error occurred');
toast.add({ title: 'Could not load people', description: response.error, color: 'error' })
}
// Close the modal and reset the person
loading.value = false;
person.value = { id: 0, name: '', phoneNumber: '' };
// Set the loading status
loading.value = false;
loading.value = false
}
const handleDeletePerson = async (id: number) => {
// Set the loading status
loading.value = true;
onMounted(loadPeople)
// Send a request to the api to delete the person
const response = await fetch(`${runtimeConfig.public.apiBaseUrl}/api/People/${id}`, {
method: 'DELETE',
credentials: 'include'
});
// Parse the response
const apiResponse = await response.json() as ApiResponse<Person>;
if (apiResponse.success) {
// Remove the person from the list
const index = people.value.findIndex(x => x.id === id);
people.value.splice(index, 1);
} else {
alert(apiResponse.error ?? 'An error occurred');
const validate = (state: typeof newPerson): FormError[] => {
const errors: FormError[] = []
if (!state.name.trim()) errors.push({ name: 'name', message: 'Name is required' })
if (!/^\+[1-9]\d{6,14}$/.test(state.phoneNumber.trim())) {
errors.push({ name: 'phoneNumber', message: 'Use international format, e.g. +61400000000' })
}
return errors
}
// Set the loading status
loading.value = false;
const savePerson = async () => {
saving.value = true
const response = await api.post<Person>('/api/People', { id: 0, ...newPerson })
saving.value = false
if (response.success && response.data) {
people.value.push(response.data)
addOpen.value = false
newPerson.name = ''
newPerson.phoneNumber = ''
toast.add({ title: 'Person added', color: 'success' })
} else {
toast.add({ title: 'Could not add person', description: response.error, color: 'error' })
}
}
const deletePerson = async (id: number) => {
const response = await api.delete<Person>(`/api/People/${id}`)
if (response.success) {
people.value = people.value.filter(x => x.id !== id)
toast.add({ title: 'Person removed', color: 'success' })
} else {
toast.add({ title: 'Could not remove person', description: response.error, color: 'error' })
}
}
</script>
<template>
<!-- Handle loading state -->
<div v-if="loading">Loading...</div>
<template v-else>
<div class="text-end my-2">
<YaleButton type="button" @click="handleAddPerson">Add Person</YaleButton>
<div>
<div class="flex justify-end mb-4">
<UButton icon="i-lucide-user-plus" @click="addOpen = true">Add Person</UButton>
</div>
<table class="w-full text-left">
<tr>
<th scope="col">Person ID</th>
<th scope="col">Name</th>
<th scope="col">Phone Number</th>
<th scope="col">Actions</th>
</tr>
<PeopleListRow v-for="person in people" :key="person.id" :person="person" @delete-person="handleDeletePerson" />
</table>
</template>
<!-- Add modal -->
<Modal v-if="addModal" @close="addModal = false">
<template #title>
Add Person
</template>
<template #default>
<div class="flex flex-col space-y-2">
<YaleFormInput v-model="person.name" type="text" placeholder="Name" class="block w-full" />
<YaleFormInput v-model="person.phoneNumber" type="text" placeholder="Phone" class="block w-full" />
<div class="flex justify-end">
<YaleButton type="button" @click="handleSavePerson">Save</YaleButton>
</div>
</div>
</template>
</Modal>
</template>
<UTable
:data="people" :columns="columns" :loading="loading"
:ui="{ th: 'px-2 whitespace-nowrap', td: 'px-2' }"
>
<template #actions-cell="{ row }">
<UButton
color="error"
variant="ghost"
icon="i-lucide-trash-2"
:aria-label="`Delete ${row.original.name}`"
@click="deletePerson(row.original.id)"
/>
</template>
<template #empty>No people yet.</template>
</UTable>
<UModal v-model:open="addOpen" title="Add Person">
<template #body>
<UForm id="add-person" :state="newPerson" :validate="validate" class="space-y-4" @submit="savePerson">
<UFormField label="Name" name="name">
<UInput v-model="newPerson.name" placeholder="Name" class="w-full" />
</UFormField>
<UFormField label="Phone" name="phoneNumber">
<UInput v-model="newPerson.phoneNumber" type="tel" placeholder="+61400000000" class="w-full" />
</UFormField>
</UForm>
</template>
<template #footer>
<UButton color="neutral" variant="outline" @click="addOpen = false">Cancel</UButton>
<UButton type="submit" form="add-person" :loading="saving">Save</UButton>
</template>
</UModal>
</div>
</template>