mirror of
https://github.com/urosran/cally.git
synced 2025-07-15 17:47:08 +00:00
Implementation of fetching, adding and updating todos to db
This commit is contained in:
11
hooks/firebase/types/todoData.ts
Normal file
11
hooks/firebase/types/todoData.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export interface IToDo {
|
||||
id: string;
|
||||
title: string;
|
||||
done: boolean;
|
||||
date: Date | null;
|
||||
points?: number;
|
||||
rotate: boolean;
|
||||
repeatType: string;
|
||||
creatorId?: string,
|
||||
familyId?: string
|
||||
}
|
26
hooks/firebase/useCreateTodo.ts
Normal file
26
hooks/firebase/useCreateTodo.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { useMutation, useQueryClient } from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import { useAuthContext } from "@/contexts/AuthContext";
|
||||
import { IToDo } from "@/hooks/firebase/types/todoData";
|
||||
|
||||
export const useCreateTodo = () => {
|
||||
const { user: currentUser, profileData } = useAuthContext();
|
||||
const queryClients = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createTodo"],
|
||||
mutationFn: async (todoData: Partial<IToDo>) => {
|
||||
try {
|
||||
const newDoc = firestore().collection('Todos').doc();
|
||||
await firestore()
|
||||
.collection("Todos")
|
||||
.add({...todoData, id: newDoc.id, familyId: profileData?.familyId, creatorId: currentUser?.uid})
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClients.invalidateQueries("todos")
|
||||
}
|
||||
})
|
||||
}
|
32
hooks/firebase/useGetTodos.ts
Normal file
32
hooks/firebase/useGetTodos.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { useQuery } from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import { useAuthContext } from "@/contexts/AuthContext";
|
||||
|
||||
export const useGetTodos = () => {
|
||||
const { user, profileData } = useAuthContext();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["todos", user?.uid],
|
||||
queryFn: async () => {
|
||||
const snapshot = await firestore()
|
||||
.collection("Todos")
|
||||
.where("familyId", "==", profileData?.familyId)
|
||||
.get();
|
||||
|
||||
return snapshot.docs.map((doc) => {
|
||||
const data = doc.data();
|
||||
|
||||
return {
|
||||
id: doc.id,
|
||||
title: data.title,
|
||||
done: data.done,
|
||||
date: data.date ? new Date(data.date.seconds * 1000) : null,
|
||||
points: data.points,
|
||||
rotate: data.points,
|
||||
repeatType: data.repeatType,
|
||||
creatorId: data.creatorId
|
||||
};
|
||||
});
|
||||
}
|
||||
})
|
||||
};
|
24
hooks/firebase/useUpdateTodo.ts
Normal file
24
hooks/firebase/useUpdateTodo.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { useMutation, useQueryClient } from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import { IToDo } from "@/hooks/firebase/types/todoData";
|
||||
|
||||
export const useUpdateTodo = () => {
|
||||
const queryClients = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["updateTodo"],
|
||||
mutationFn: async (todoData: Partial<IToDo>) => {
|
||||
try {
|
||||
await firestore()
|
||||
.collection("Todos")
|
||||
.doc(todoData.id)
|
||||
.update(todoData);
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClients.invalidateQueries("todos")
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user