mirror of
https://github.com/urosran/cally.git
synced 2026-10-23 10:47:54 +00:00
Get Events fix
This commit is contained in:
@@ -1,29 +1,39 @@
|
||||
import {useQuery} from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import {colorMap} from "@/contexts/SettingsContext";
|
||||
import {useAtomValue} from "jotai";
|
||||
import {isFamilyViewAtom} from "@/components/pages/calendar/atoms";
|
||||
import {colorMap} from "@/constants/colorMap";
|
||||
|
||||
export const useGetEvents = () => {
|
||||
const {user, profileData} = useAuthContext();
|
||||
const isFamilyView = useAtomValue(isFamilyViewAtom)
|
||||
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["events", user?.uid, isFamilyView],
|
||||
queryFn: async () => {
|
||||
const db = firestore();
|
||||
|
||||
const userId = user?.uid; // Assuming user is defined
|
||||
const familyId = profileData?.familyId; // Assuming profileData is defined
|
||||
const userId = user?.uid;
|
||||
const familyId = profileData?.familyId;
|
||||
let allEvents = [];
|
||||
|
||||
if (isFamilyView) {
|
||||
const familyQuery = db.collection("Events").where("familyID", "==", familyId);
|
||||
const familySnapshot = await familyQuery.get();
|
||||
const familyEvents = familySnapshot.docs.map(doc => doc.data());
|
||||
const creatorQuery = db.collection("Events").where("creatorId", "==", userId);
|
||||
const attendeeQuery = db.collection("Events").where("attendees", "array-contains", userId);
|
||||
|
||||
allEvents = [...familyEvents];
|
||||
const [familySnapshot, creatorSnapshot, attendeeSnapshot] = await Promise.all([
|
||||
familyQuery.get(),
|
||||
creatorQuery.get(),
|
||||
attendeeQuery.get(),
|
||||
]);
|
||||
|
||||
const familyEvents = familySnapshot.docs.map(doc => doc.data());
|
||||
const creatorEvents = creatorSnapshot.docs.map(doc => doc.data());
|
||||
const attendeeEvents = attendeeSnapshot.docs.map(doc => doc.data());
|
||||
|
||||
allEvents = [...familyEvents, ...creatorEvents, ...attendeeEvents];
|
||||
} else {
|
||||
const creatorQuery = db.collection("Events").where("creatorId", "==", userId);
|
||||
const attendeeQuery = db.collection("Events").where("attendees", "array-contains", userId);
|
||||
@@ -51,7 +61,7 @@ export const useGetEvents = () => {
|
||||
.get();
|
||||
|
||||
const profileData = profileSnapshot.data();
|
||||
const eventColor = profileData?.eventColor || colorMap.pink; // Default color if not found
|
||||
const eventColor = profileData?.eventColor || colorMap.pink;
|
||||
|
||||
return {
|
||||
id: event.id,
|
||||
@@ -65,6 +75,6 @@ export const useGetEvents = () => {
|
||||
);
|
||||
},
|
||||
staleTime: Infinity,
|
||||
cacheTime: Infinity
|
||||
cacheTime: Infinity,
|
||||
});
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user