Skip to content

Commit 0210d14

Browse files
committed
chore: playground new api
1 parent 57dbbc8 commit 0210d14

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

playground/src/pages/todos.vue

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
import {
33
addDoc,
44
collection,
5+
deleteDoc,
56
doc,
67
query,
78
serverTimestamp,
89
updateDoc,
910
where,
1011
} from 'firebase/firestore'
1112
import { ref } from 'vue'
12-
import { firestoreBind } from 'vuefire'
13+
import { useCollection } from 'vuefire'
1314
import { useFirestore } from '@/firebase'
1415
1516
interface Todo {
@@ -20,12 +21,24 @@ interface Todo {
2021
2122
const db = useFirestore()
2223
const todosRef = collection(db, 'todos')
24+
const todosWithConverterRef = collection(db, 'todos').withConverter<Todo>({
25+
toFirestore(todoModel) {
26+
const { id, ...todo } = todoModel
27+
return todo
28+
},
29+
fromFirestore(snapshot, options) {
30+
const todoData = snapshot.data(options) as Omit<Todo, 'id'>
31+
return {
32+
id: snapshot.id,
33+
...todoData,
34+
}
35+
},
36+
})
2337
const finishedTodos = query(todosRef, where('finished', '==', true))
2438
const unfinishedTodos = query(todosRef, where('finished', '==', false))
2539
26-
const todos = ref<Todo[]>([])
27-
// TODO: return an augmented typed ref
28-
firestoreBind(todos, todosRef)
40+
const todos = useCollection<Todo>(todosRef)
41+
const todosConverted = useCollection(todosWithConverterRef)
2942
3043
const newTodoText = ref('')
3144
@@ -40,15 +53,15 @@ function addTodo() {
4053
}
4154
}
4255
43-
function updateTodoText(todo: any, newText: string) {
44-
console.log('update', todo)
45-
return
56+
function updateTodoText(todo: Todo, newText: string) {
4657
updateDoc(doc(db, 'todos', todo.id), {
4758
text: newText,
4859
})
4960
}
5061
51-
function removeTodo() {}
62+
function removeTodo(todo: Todo) {
63+
deleteDoc(doc(db, 'todos', todo.id))
64+
}
5265
5366
function toggleTodos() {
5467
// TODO:

0 commit comments

Comments
 (0)