File size: 2,328 Bytes
6d44341 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import React, {useContext, useState, useEffect} from 'react';
import {db} from "../firebase";
const DBContext = React.createContext();
export const useDB = () => useContext(DBContext);
export const DBProvider = ({children}) => {
const [loading, setLoading] = useState()
const [lists, setLists] = useState([])
const [currentList, setCurrentList] = useState({})
const getLists = async () => {
setLoading(true)
return db.collection('lists')
.onSnapshot((snapshot) => {
setLists(snapshot.docs.map(doc => doc.data()))
});
}
const getCurrentList = async (listId) => {
const colRef = db.collection('lists').doc(listId).collection('todos')
return colRef.onSnapshot((snapshot) => {setCurrentList(prev => snapshot.docs.map(doc => doc.data()))})
}
const newList = async (list) => {
const docRef = db.collection('lists').doc()
return await docRef.set({...list, id: docRef.id})
}
const deleteList = async (list) => {
const docRef = db.collection('lists').doc(list.id)
return await docRef.delete()
}
const addNewTodo = async (listId, todo) => {
const docRef = db.collection('lists').doc(listId).collection('todos').doc()
const data = {...todo, id: docRef.id}
return await docRef.set({...data})
}
const deleteTodo = async (listId, todo) => {
const docRef = db.collection('lists').doc(listId).collection('todos').doc(todo.id)
return await docRef.delete()
}
const toggleTodoCompleted = async (listId, todo) => {
const docRef = db.collection('lists').doc(listId).collection('todos').doc(todo.id)
await docRef.get().then(async doc => {
return await docRef.update({
completed: !doc.data().completed
})
})
}
useEffect(() => {
getLists().then(() => {setLoading(false)})
}, [])
const value = {
getCurrentList,
setCurrentList,
currentList,
getLists,
lists,
newList,
deleteList,
addNewTodo,
toggleTodoCompleted,
deleteTodo,
loading
}
return (
<DBContext.Provider value={value}>
{children}
</DBContext.Provider>
);
}
|