Various updates....

This commit is contained in:
Milan Paunovic
2024-10-20 01:44:20 +02:00
parent d87d67eae5
commit 00583ac115
13 changed files with 280 additions and 154 deletions
+44 -24
View File
@@ -12,37 +12,57 @@ export const useGetEvents = () => {
return useQuery({
queryKey: ["events", user?.uid, isFamilyView],
queryFn: async () => {
const eventsQuery = firestore()
.collection("Events")
.where("creatorId", "==", user?.uid)
.where("attendees", "array-contains", user?.uid);
const db = firestore();
const userId = user?.uid; // Assuming user is defined
const familyId = profileData?.familyId; // Assuming profileData is defined
let allEvents = [];
if (isFamilyView) {
eventsQuery.where("familyID", "==", profileData?.familyId);
const familyQuery = db.collection("Events").where("familyID", "==", familyId);
const familySnapshot = await familyQuery.get();
const familyEvents = familySnapshot.docs.map(doc => doc.data());
allEvents = [...familyEvents];
} else {
const creatorQuery = db.collection("Events").where("creatorId", "==", userId);
const attendeeQuery = db.collection("Events").where("attendees", "array-contains", userId);
const [creatorSnapshot, attendeeSnapshot] = await Promise.all([
creatorQuery.get(),
attendeeQuery.get(),
]);
const creatorEvents = creatorSnapshot.docs.map(doc => doc.data());
const attendeeEvents = attendeeSnapshot.docs.map(doc => doc.data());
allEvents = [...creatorEvents, ...attendeeEvents];
}
const snapshot = await eventsQuery.get();
allEvents = allEvents.filter((event, index, self) =>
index === self.findIndex(e => e.id === event.id)
);
return await Promise.all(snapshot.docs.map(async (doc) => {
const data = doc.data();
return await Promise.all(
allEvents.map(async (event) => {
const profileSnapshot = await db
.collection("Profiles")
.doc(event.creatorId)
.get();
const profileSnapshot = await firestore()
.collection("Profiles")
.doc(data.creatorId)
.get();
const profileData = profileSnapshot.data();
const eventColor = profileData?.eventColor || colorMap.pink; // Default color if not found
const profileData = profileSnapshot.data();
const eventColor: string = profileData?.eventColor || colorMap.pink // Default color if not found
return {
id: doc.id,
title: data.title,
start: new Date(data.startDate.seconds * 1000),
end: new Date(data.endDate.seconds * 1000),
hideHours: data.allDay,
eventColor: eventColor,
};
}));
return {
id: event.id,
title: event.title,
start: new Date(event.startDate.seconds * 1000),
end: new Date(event.endDate.seconds * 1000),
hideHours: event.allDay,
eventColor: eventColor,
};
})
);
},
staleTime: Infinity,
cacheTime: Infinity