- Improved fetching of events for family device

- Removed fetching events and considering if they're private or not
This commit is contained in:
Dejan
2025-01-18 00:31:48 +01:00
parent 413164128b
commit dcca652019

View File

@ -27,17 +27,23 @@ const fetchEvents = async (userId: string, profileData: UserProfile | undefined,
let constraints; let constraints;
let familyId = profileData?.familyId; let familyId = profileData?.familyId;
if (isFamilyView || profileData?.userType === ProfileType.FAMILY_DEVICE) { if (profileData?.userType === ProfileType.FAMILY_DEVICE) {
constraints = [ constraints = [
eventsQuery.where("familyId", "==", familyId).where("private", "not-in", [true]), eventsQuery.where("familyId", "==", familyId)
eventsQuery.where("creatorId", "==", userId),
eventsQuery.where("attendees", "array-contains", userId)
]; ];
} else { } else {
constraints = [ if (isFamilyView) {
eventsQuery.where("creatorId", "==", userId), constraints = [
eventsQuery.where("attendees", "array-contains", userId) eventsQuery.where("familyId", "==", familyId),
]; eventsQuery.where("creatorId", "==", userId),
eventsQuery.where("attendees", "array-contains", userId)
];
} else {
constraints = [
eventsQuery.where("creatorId", "==", userId),
eventsQuery.where("attendees", "array-contains", userId)
];
}
} }
const snapshots = await Promise.all(constraints.map(query => query.get())); const snapshots = await Promise.all(constraints.map(query => query.get()));
@ -49,6 +55,7 @@ const fetchEvents = async (userId: string, profileData: UserProfile | undefined,
snapshots.forEach(snapshot => { snapshots.forEach(snapshot => {
snapshot.docs.forEach(doc => { snapshot.docs.forEach(doc => {
const event = doc.data(); const event = doc.data();
console.log(event);
const hash = createEventHash(event); const hash = createEventHash(event);
if (!processedHashes.has(hash)) { if (!processedHashes.has(hash)) {
@ -102,13 +109,13 @@ export const useGetEvents = () => {
await queryClient.prefetchQuery({ await queryClient.prefetchQuery({
queryKey: ["events", user.uid, false], // Personal events queryKey: ["events", user.uid, false], // Personal events
queryFn: () => fetchEvents(user.uid, profileData, false), queryFn: () => fetchEvents(user.uid, profileData, false),
staleTime: 5 * 60 * 1000, staleTime: 1000,
}); });
await queryClient.prefetchQuery({ await queryClient.prefetchQuery({
queryKey: ["events", user.uid, true], // Family events queryKey: ["events", user.uid, true], // Family events
queryFn: () => fetchEvents(user.uid, profileData, true), queryFn: () => fetchEvents(user.uid, profileData, true),
staleTime: 5 * 60 * 1000, staleTime:1000,
}); });
}; };