mirror of
https://github.com/urosran/cally.git
synced 2025-07-16 10:06:15 +00:00
Fix adding devices and fetching events
This commit is contained in:
@ -1,23 +1,24 @@
|
||||
import {useQuery} from "react-query";
|
||||
import { useQuery } from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import {useAtomValue} from "jotai";
|
||||
import {isFamilyViewAtom} from "@/components/pages/calendar/atoms";
|
||||
import {colorMap} from "@/constants/colorMap";
|
||||
import { useAuthContext } from "@/contexts/AuthContext";
|
||||
import { useAtomValue } from "jotai";
|
||||
import { isFamilyViewAtom } from "@/components/pages/calendar/atoms";
|
||||
import { colorMap } from "@/constants/colorMap";
|
||||
|
||||
export const useGetEvents = () => {
|
||||
const {user, profileData} = useAuthContext();
|
||||
const { user, profileData } = useAuthContext();
|
||||
const isFamilyView = useAtomValue(isFamilyViewAtom);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["events", user?.uid, isFamilyView],
|
||||
queryFn: async () => {
|
||||
const db = firestore();
|
||||
|
||||
const userId = user?.uid;
|
||||
const familyId = profileData?.familyId;
|
||||
|
||||
let allEvents = [];
|
||||
|
||||
// If family view is active, include family, creator, and attendee events
|
||||
if (isFamilyView) {
|
||||
const familyQuery = db.collection("Events").where("familyID", "==", familyId);
|
||||
const creatorQuery = db.collection("Events").where("creatorId", "==", userId);
|
||||
@ -29,12 +30,14 @@ export const useGetEvents = () => {
|
||||
attendeeQuery.get(),
|
||||
]);
|
||||
|
||||
// Collect all events
|
||||
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 {
|
||||
// Only include creator and attendee events when family view is off
|
||||
const creatorQuery = db.collection("Events").where("creatorId", "==", userId);
|
||||
const attendeeQuery = db.collection("Events").where("attendees", "array-contains", userId);
|
||||
|
||||
@ -49,19 +52,28 @@ export const useGetEvents = () => {
|
||||
allEvents = [...creatorEvents, ...attendeeEvents];
|
||||
}
|
||||
|
||||
allEvents = allEvents.filter((event, index, self) =>
|
||||
index === self.findIndex(e => e.id === event.id)
|
||||
);
|
||||
// Use a Map to ensure uniqueness only for events with IDs
|
||||
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) {
|
||||
return event.creatorId === userId;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
// Attach event colors and return the final list of events
|
||||
return await Promise.all(
|
||||
allEvents.map(async (event) => {
|
||||
filteredEvents.map(async (event) => {
|
||||
const profileSnapshot = await db
|
||||
.collection("Profiles")
|
||||
.doc(event.creatorId)
|
||||
@ -71,13 +83,13 @@ export const useGetEvents = () => {
|
||||
const eventColor = profileData?.eventColor || colorMap.pink;
|
||||
|
||||
return {
|
||||
id: event.id,
|
||||
id: event.id || Math.random().toString(36).substr(2, 9), // Generate temp ID if missing
|
||||
title: event.title,
|
||||
start: new Date(event.startDate.seconds * 1000),
|
||||
end: new Date(event.endDate.seconds * 1000),
|
||||
hideHours: event.allDay,
|
||||
eventColor: eventColor,
|
||||
notes: event.notes
|
||||
eventColor,
|
||||
notes: event.notes,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
Reference in New Issue
Block a user