Fix adding devices and fetching events

This commit is contained in:
Milan Paunovic
2024-10-22 12:28:43 +02:00
parent 1f45f5d21f
commit bc54c902d9
2 changed files with 31 additions and 19 deletions
@@ -67,10 +67,10 @@ const MyGroup = () => {
return; return;
} }
if (selectedStatus !== ProfileType.FAMILY_DEVICE && !email) { // if (selectedStatus !== ProfileType.FAMILY_DEVICE && !email) {
console.error("Email is required for non-family device users"); // console.error("Email is required for non-family device users");
return; // return;
} // }
if (email && !email.includes("@")) { if (email && !email.includes("@")) {
console.error("Invalid email address"); console.error("Invalid email address");
+27 -15
View File
@@ -1,23 +1,24 @@
import {useQuery} from "react-query"; import { useQuery } from "react-query";
import firestore from "@react-native-firebase/firestore"; import firestore from "@react-native-firebase/firestore";
import {useAuthContext} from "@/contexts/AuthContext"; import { useAuthContext } from "@/contexts/AuthContext";
import {useAtomValue} from "jotai"; import { useAtomValue } from "jotai";
import {isFamilyViewAtom} from "@/components/pages/calendar/atoms"; import { isFamilyViewAtom } from "@/components/pages/calendar/atoms";
import {colorMap} from "@/constants/colorMap"; import { colorMap } from "@/constants/colorMap";
export const useGetEvents = () => { export const useGetEvents = () => {
const {user, profileData} = useAuthContext(); const { user, profileData } = useAuthContext();
const isFamilyView = useAtomValue(isFamilyViewAtom); const isFamilyView = useAtomValue(isFamilyViewAtom);
return useQuery({ return useQuery({
queryKey: ["events", user?.uid, isFamilyView], queryKey: ["events", user?.uid, isFamilyView],
queryFn: async () => { queryFn: async () => {
const db = firestore(); const db = firestore();
const userId = user?.uid; const userId = user?.uid;
const familyId = profileData?.familyId; const familyId = profileData?.familyId;
let allEvents = []; let allEvents = [];
// If family view is active, include family, creator, and attendee events
if (isFamilyView) { if (isFamilyView) {
const familyQuery = db.collection("Events").where("familyID", "==", familyId); const familyQuery = db.collection("Events").where("familyID", "==", familyId);
const creatorQuery = db.collection("Events").where("creatorId", "==", userId); const creatorQuery = db.collection("Events").where("creatorId", "==", userId);
@@ -29,12 +30,14 @@ export const useGetEvents = () => {
attendeeQuery.get(), attendeeQuery.get(),
]); ]);
// Collect all events
const familyEvents = familySnapshot.docs.map(doc => doc.data()); const familyEvents = familySnapshot.docs.map(doc => doc.data());
const creatorEvents = creatorSnapshot.docs.map(doc => doc.data()); const creatorEvents = creatorSnapshot.docs.map(doc => doc.data());
const attendeeEvents = attendeeSnapshot.docs.map(doc => doc.data()); const attendeeEvents = attendeeSnapshot.docs.map(doc => doc.data());
allEvents = [...familyEvents, ...creatorEvents, ...attendeeEvents]; allEvents = [...familyEvents, ...creatorEvents, ...attendeeEvents];
} else { } else {
// Only include creator and attendee events when family view is off
const creatorQuery = db.collection("Events").where("creatorId", "==", userId); const creatorQuery = db.collection("Events").where("creatorId", "==", userId);
const attendeeQuery = db.collection("Events").where("attendees", "array-contains", userId); const attendeeQuery = db.collection("Events").where("attendees", "array-contains", userId);
@@ -49,19 +52,28 @@ export const useGetEvents = () => {
allEvents = [...creatorEvents, ...attendeeEvents]; allEvents = [...creatorEvents, ...attendeeEvents];
} }
allEvents = allEvents.filter((event, index, self) => // Use a Map to ensure uniqueness only for events with IDs
index === self.findIndex(e => e.id === event.id) const uniqueEventsMap = new Map();
); allEvents.forEach(event => {
if (event.id) {
uniqueEventsMap.set(event.id, event); // Ensure uniqueness for events with IDs
} else {
uniqueEventsMap.set(Math.random().toString(36), event); // Generate a temp key for events without ID
}
});
const uniqueEvents = Array.from(uniqueEventsMap.values());
allEvents = allEvents.filter(event => { // Filter out private events unless the user is the creator
const filteredEvents = uniqueEvents.filter(event => {
if (event.private) { if (event.private) {
return event.creatorId === userId; return event.creatorId === userId;
} }
return true; return true;
}); });
// Attach event colors and return the final list of events
return await Promise.all( return await Promise.all(
allEvents.map(async (event) => { filteredEvents.map(async (event) => {
const profileSnapshot = await db const profileSnapshot = await db
.collection("Profiles") .collection("Profiles")
.doc(event.creatorId) .doc(event.creatorId)
@@ -71,13 +83,13 @@ export const useGetEvents = () => {
const eventColor = profileData?.eventColor || colorMap.pink; const eventColor = profileData?.eventColor || colorMap.pink;
return { return {
id: event.id, id: event.id || Math.random().toString(36).substr(2, 9), // Generate temp ID if missing
title: event.title, title: event.title,
start: new Date(event.startDate.seconds * 1000), start: new Date(event.startDate.seconds * 1000),
end: new Date(event.endDate.seconds * 1000), end: new Date(event.endDate.seconds * 1000),
hideHours: event.allDay, hideHours: event.allDay,
eventColor: eventColor, eventColor,
notes: event.notes notes: event.notes,
}; };
}) })
); );