Files
cally/hooks/firebase/useGetTodos.ts
Dejan 04d865cce9 - Fixed issue with family device not being able to see todos of the family
- Fixed issue with todos not being able to update on family device
2024-12-30 20:08:06 +01:00

39 lines
1.5 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import firestore, {or, query, where} from "@react-native-firebase/firestore";
import { ProfileType, useAuthContext } from "@/contexts/AuthContext";
import { IToDo } from "@/hooks/firebase/types/todoData";
export const useGetTodos = () => {
const { user, profileData } = useAuthContext();
return useQuery({
queryKey: ["todos", user?.uid],
queryFn: async () => {
let snapshot;
if (profileData?.userType === ProfileType.PARENT || profileData?.userType === ProfileType.FAMILY_DEVICE) {
snapshot = await firestore()
.collection("Todos")
.where("familyId", "==", profileData?.familyId)
.get();
} else {
let todosQuery = query(firestore().collection("Todos"), or(where("assignees", "array-contains", user?.uid), where("creatorId", "==", user?.uid)));
snapshot = await todosQuery.get();
}
return snapshot.docs.map((doc) => {
const data = doc.data();
return {
...data,
id: doc.id,
date: data.date ? new Date(data.date.seconds * 1000) : null,
repeatDays: data.repeatDays ?? []
};
}) as IToDo[];
},
staleTime: Infinity,
gcTime: Infinity,
placeholderData: (previousData) => previousData,
})
};