- 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
110 lines
3.5 KiB
Vue
110 lines
3.5 KiB
Vue
<script setup lang="ts">
|
|
import type { FormError, TableColumn } from '@nuxt/ui'
|
|
import type { Person } from '~/types/yale'
|
|
|
|
const api = useApi()
|
|
const toast = useToast()
|
|
|
|
const people = ref<Person[]>([])
|
|
const loading = ref(true)
|
|
const saving = ref(false)
|
|
const addOpen = ref(false)
|
|
const newPerson = reactive({ name: '', phoneNumber: '' })
|
|
|
|
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 () => {
|
|
loading.value = true
|
|
const response = await api.get<Person[]>('/api/People')
|
|
if (response.success) {
|
|
people.value = response.data ?? []
|
|
} else {
|
|
toast.add({ title: 'Could not load people', description: response.error, color: 'error' })
|
|
}
|
|
loading.value = false
|
|
}
|
|
|
|
onMounted(loadPeople)
|
|
|
|
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
|
|
}
|
|
|
|
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>
|
|
<div>
|
|
<div class="flex justify-end mb-4">
|
|
<UButton icon="i-lucide-user-plus" @click="addOpen = true">Add Person</UButton>
|
|
</div>
|
|
|
|
<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>
|