|
| 1 | +<script setup> |
| 2 | +definePageMeta({ |
| 3 | + middleware: 'auth' |
| 4 | +}) |
| 5 | +const loading = ref(false) |
| 6 | +const newTodo = ref('') |
| 7 | +const newTodoInput = ref(null) |
| 8 | +
|
| 9 | +const toast = useToast() |
| 10 | +const { user, clear } = useUserSession() |
| 11 | +const { data: todos } = await useFetch('/api/todos') |
| 12 | +
|
| 13 | +async function addTodo () { |
| 14 | + if (!newTodo.value.trim()) { return } |
| 15 | +
|
| 16 | + loading.value = true |
| 17 | +
|
| 18 | + try { |
| 19 | + const todo = await $fetch('/api/todos', { |
| 20 | + method: 'POST', |
| 21 | + body: { |
| 22 | + title: newTodo.value, |
| 23 | + completed: 0 |
| 24 | + } |
| 25 | + }) |
| 26 | + todos.value.push(todo) |
| 27 | + toast.add({ title: `Todo "${todo.title}" created.` }) |
| 28 | + newTodo.value = '' |
| 29 | + nextTick(() => { |
| 30 | + newTodoInput.value?.input?.focus() |
| 31 | + }) |
| 32 | + } catch (err) { |
| 33 | + if (err.data?.data?.issues) { |
| 34 | + const title = err.data.data.issues.map(issue => issue.message).join('\n') |
| 35 | + toast.add({ title, color: 'red' }) |
| 36 | + } |
| 37 | + } |
| 38 | + loading.value = false |
| 39 | +} |
| 40 | +
|
| 41 | +async function toggleTodo (todo) { |
| 42 | + todo.completed = Number(!todo.completed) |
| 43 | + await useFetch(`/api/todos/${todo.id}`, { |
| 44 | + method: 'PATCH', |
| 45 | + body: { |
| 46 | + completed: todo.completed |
| 47 | + } |
| 48 | + }) |
| 49 | +} |
| 50 | +
|
| 51 | +async function deleteTodo (todo) { |
| 52 | + await useFetch(`/api/todos/${todo.id}`, { method: 'DELETE' }) |
| 53 | + todos.value = todos.value.filter(t => t.id !== todo.id) |
| 54 | + toast.add({ title: `Todo "${todo.title}" deleted.` }) |
| 55 | +} |
| 56 | +
|
| 57 | +const items = [[{ |
| 58 | + label: 'Logout', |
| 59 | + icon: 'i-heroicons-arrow-left-on-rectangle', |
| 60 | + click: clear |
| 61 | +}]] |
| 62 | +</script> |
| 63 | +
|
| 64 | +<template> |
| 65 | + <UCard @submit.prevent="addTodo"> |
| 66 | + <template #header> |
| 67 | + <h3 class="text-lg font-semibold leading-6"> |
| 68 | + <NuxtLink to="/"> |
| 69 | + Todo List |
| 70 | + </NuxtLink> |
| 71 | + </h3> |
| 72 | +
|
| 73 | + <UDropdown v-if="user" :items="items"> |
| 74 | + <UButton color="white" trailing-icon="i-heroicons-chevron-down-20-solid"> |
| 75 | + <UAvatar :src="`https://github.com/${user.login}.png`" :alt="user.login" size="3xs" /> |
| 76 | + {{ user.login }} |
| 77 | + </UButton> |
| 78 | + </UDropdown> |
| 79 | + </template> |
| 80 | +
|
| 81 | + <div class="flex items-center gap-2"> |
| 82 | + <UInput |
| 83 | + ref="newTodoInput" |
| 84 | + v-model="newTodo" |
| 85 | + name="todo" |
| 86 | + :disabled="loading" |
| 87 | + class="flex-1" |
| 88 | + placeholder="Make a Nuxt demo" |
| 89 | + autocomplete="off" |
| 90 | + autofocus |
| 91 | + :ui="{ wrapper: 'flex-1' }" |
| 92 | + /> |
| 93 | +
|
| 94 | + <UButton type="submit" icon="i-heroicons-plus-20-solid" :loading="loading" :disabled="newTodo.trim().length === 0" /> |
| 95 | + </div> |
| 96 | +
|
| 97 | + <ul class="divide-y divide-gray-200 dark:divide-gray-800"> |
| 98 | + <li |
| 99 | + v-for="todo of todos" |
| 100 | + :key="todo.id" |
| 101 | + class="flex items-center gap-4 py-2" |
| 102 | + > |
| 103 | + <span class="flex-1 font-medium" :class="[todo.completed ? 'line-through text-gray-500' : '']">{{ todo.title }}</span> |
| 104 | +
|
| 105 | + <UToggle :model-value="Boolean(todo.completed)" @update:model-value="toggleTodo(todo)" /> |
| 106 | +
|
| 107 | + <UButton |
| 108 | + color="red" |
| 109 | + variant="soft" |
| 110 | + size="2xs" |
| 111 | + icon="i-heroicons-x-mark-20-solid" |
| 112 | + @click="deleteTodo(todo)" |
| 113 | + /> |
| 114 | + </li> |
| 115 | + </ul> |
| 116 | + </UCard> |
| 117 | +</template> |
0 commit comments