Skip to content

Commit 9e93886

Browse files
committed
chore: check database nuxt works well
1 parent 53c5dbd commit 9e93886

File tree

3 files changed

+144
-7
lines changed

3 files changed

+144
-7
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<script lang="ts" setup>
2+
import { computed, nextTick, ref } from 'vue'
3+
4+
export interface Todo {
5+
created: Date
6+
finished: boolean
7+
text: string
8+
}
9+
10+
const props = defineProps<{
11+
todo: Todo & { id: string }
12+
}>()
13+
14+
const emit = defineEmits<{
15+
(event: 'update:todo', id: string, newTodo: Todo): void
16+
(event: 'delete', id: string): void
17+
}>()
18+
19+
const isEditing = ref(false)
20+
const textCopy = ref('')
21+
22+
function startEdit() {
23+
textCopy.value = props.todo.text
24+
isEditing.value = true
25+
}
26+
27+
function saveTodo() {
28+
if (!isEditing.value) { return }
29+
30+
emit('update:todo', props.todo.id, {
31+
...props.todo,
32+
text: textCopy.value,
33+
})
34+
35+
isEditing.value = false
36+
}
37+
38+
const finished = computed({
39+
get: () => props.todo.finished,
40+
set(finished) {
41+
emit('update:todo', props.todo.id, { ...props.todo, finished })
42+
},
43+
})
44+
45+
async function lostFocus() {
46+
await nextTick()
47+
saveTodo()
48+
}
49+
</script>
50+
51+
<template>
52+
<li>
53+
<form v-if="isEditing" @submit.prevent="saveTodo">
54+
<input
55+
v-model="textCopy"
56+
v-focus
57+
type="text"
58+
@keydown.esc="isEditing = false"
59+
@blur="lostFocus"
60+
>
61+
<button>Save</button>
62+
<button type="button" @click="isEditing = false">
63+
Cancel
64+
</button>
65+
</form>
66+
<template v-else>
67+
<input v-model="finished" type="checkbox">
68+
<span
69+
:class="todo.finished ? 'line-through' : ''"
70+
@dblclick="startEdit"
71+
>{{ todo.text }}</span>
72+
</template>
73+
<button @click="$emit('delete', todo.id)">
74+
Delete
75+
</button>
76+
</li>
77+
</template>
Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,68 @@
11
<script setup lang="ts">
2-
const todos = ref<{ title: string, id: string }[]>([])
2+
3+
import {
4+
push,
5+
remove,
6+
ref as dbRef,
7+
serverTimestamp,
8+
update,
9+
} from 'firebase/database'
10+
import { ref } from 'vue'
11+
import { useDatabase, useList } from 'vuefire'
12+
import { Todo } from '~/components/TodoItem.vue'
13+
14+
const db = useDatabase()
15+
const todosRef = dbRef(db, 'todos')
16+
// TODO:
17+
// const finishedTodos = query(todosRef, where('finished', '==', true))
18+
// const unfinishedTodos = query(todosRef, where('finished', '==', false))
19+
20+
const todos = useList<Todo>(todosRef)
21+
22+
const newTodoText = ref('')
23+
24+
function addTodo() {
25+
if (newTodoText.value) {
26+
push(todosRef, {
27+
text: newTodoText.value,
28+
finished: false,
29+
created: serverTimestamp(),
30+
})
31+
newTodoText.value = ''
32+
}
33+
}
34+
35+
function updateTodo(id: string, newTodo: Todo) {
36+
update(dbRef(db, 'todos/' + id), newTodo)
37+
}
38+
39+
function removeTodo(id: string) {
40+
remove(dbRef(db, 'todos/' + id))
41+
}
42+
43+
function toggleTodos() {
44+
// TODO:
45+
}
46+
347
</script>
448

549
<template>
650
<div>
7-
<h1>Todos</h1>
51+
<button @click="toggleTodos">
52+
Toggle todos
53+
</button> <br>
54+
<form @submit.prevent="addTodo">
55+
<input v-model.trim="newTodoText" placeholder="Add new todo">
56+
<button>Add Todo</button>
57+
</form>
858
<ul>
9-
<li v-for="todo in todos" :key="todo.id">
10-
<NuxtLink :to="`/database/todos/${todo.id}`">
11-
{{ todo.title }}
12-
</NuxtLink>
13-
</li>
59+
<TodoItem
60+
v-for="todo in todos"
61+
:key="todo.id"
62+
:todo="todo"
63+
@delete="removeTodo"
64+
@update:todo="updateTodo"
65+
/>
1466
</ul>
1567
</div>
1668
</template>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default defineNuxtPlugin((nuxt) => {
2+
nuxt.vueApp.directive('focus', {
3+
async mounted(el) {
4+
await nextTick()
5+
el.focus()
6+
},
7+
})
8+
})

0 commit comments

Comments
 (0)