mirror of
https://github.com/urosran/cally.git
synced 2026-10-23 07:27:55 +00:00
Various updates....
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user