Deletion fix

This commit is contained in:
Milan Paunovic
2024-12-24 16:07:18 +01:00
parent c93d66d13d
commit 10f6616cd0
7 changed files with 78 additions and 38 deletions

View File

@ -9,14 +9,16 @@ import {useFormattedEvents} from "@/components/pages/calendar/useFormattedEvents
import {useCalendarControls} from "@/components/pages/calendar/useCalendarControls"; import {useCalendarControls} from "@/components/pages/calendar/useCalendarControls";
import {EventCell} from "@/components/pages/calendar/EventCell"; import {EventCell} from "@/components/pages/calendar/EventCell";
import {isToday} from "date-fns"; import {isToday} from "date-fns";
import { View } from "react-native-ui-lib"; import {View} from "react-native-ui-lib";
interface EventCalendarProps { interface EventCalendarProps {
calendarHeight: number; calendarHeight: number;
calendarWidth: number; calendarWidth: number;
} }
export const DetailedCalendar: React.FC<EventCalendarProps> = ({calendarHeight, calendarWidth}) => { const MemoizedEventCell = React.memo(EventCell);
export const DetailedCalendar: React.FC<EventCalendarProps> = React.memo(({calendarHeight, calendarWidth}) => {
const {profileData} = useAuthContext(); const {profileData} = useAuthContext();
const selectedDate = useAtomValue(selectedDateAtom); const selectedDate = useAtomValue(selectedDateAtom);
const mode = useAtomValue(modeAtom); const mode = useAtomValue(modeAtom);
@ -58,20 +60,21 @@ export const DetailedCalendar: React.FC<EventCalendarProps> = ({calendarHeight,
initialDate: selectedDate.toISOString(), initialDate: selectedDate.toISOString(),
}), [selectedDate]); }), [selectedDate]);
const getAttendees = useCallback((event: any) => {
return familyMembers?.filter(member => event?.attendees?.includes(member?.uid!)) || [];
}, [familyMembers]);
const renderEvent = useCallback((event: any) => { const renderEvent = useCallback((event: any) => {
const attendees = useMemo(() => const attendees = getAttendees(event);
familyMembers?.filter(member => event?.attendees?.includes(member?.uid!)) || [],
[familyMembers, event.attendees]
);
return ( return (
<EventCell <MemoizedEventCell
event={event} event={event}
onPress={handlePressEvent} onPress={handlePressEvent}
attendees={attendees} attendees={attendees}
/> />
); );
}, [familyMembers, handlePressEvent]); }, [familyMembers, handlePressEvent, getAttendees]);
useEffect(() => { useEffect(() => {
if (selectedDate && isToday(selectedDate)) { if (selectedDate && isToday(selectedDate)) {
@ -85,12 +88,12 @@ export const DetailedCalendar: React.FC<EventCalendarProps> = ({calendarHeight,
{...containerProps} {...containerProps}
numberOfDays={numberOfDays} numberOfDays={numberOfDays}
calendarWidth={calendarWidth} calendarWidth={calendarWidth}
onDateChanged={debouncedOnDateChanged} onDateChanged={debouncedOnDateChanged}
firstDay={firstDay} firstDay={firstDay}
events={formattedEvents ?? []} events={formattedEvents ?? []}
onPressEvent={handlePressEvent} onPressEvent={handlePressEvent}
onPressBackground={handlePressCell} onPressBackground={handlePressCell}
> >
<CalendarHeader {...headerProps} /> <CalendarHeader {...headerProps} />
<CalendarBody <CalendarBody
@ -100,6 +103,8 @@ export const DetailedCalendar: React.FC<EventCalendarProps> = ({calendarHeight,
<View marginB-45/> <View marginB-45/>
</CalendarContainer> </CalendarContainer>
); );
}; });
DetailedCalendar.displayName = 'DetailedCalendar';
export default DetailedCalendar; export default DetailedCalendar;

View File

@ -9,7 +9,7 @@ import {
modeAtom, modeAtom,
} from './atoms'; } from './atoms';
import {MonthCalendar} from "@/components/pages/calendar/MonthCalendar"; import {MonthCalendar} from "@/components/pages/calendar/MonthCalendar";
import {DetailedCalendar} from "@/components/pages/calendar/DetailedCalendar"; import DetailedCalendar from "@/components/pages/calendar/DetailedCalendar";
interface EventCalendarProps { interface EventCalendarProps {
calendarHeight: number; calendarHeight: number;
@ -17,7 +17,7 @@ interface EventCalendarProps {
} }
export const EventCalendar: React.FC<EventCalendarProps> = React.memo((props) => { export const EventCalendar: React.FC<EventCalendarProps> = React.memo((props) => {
const {data: events, isLoading} = useGetEvents(); const {isLoading} = useGetEvents();
const [mode] = useAtom(modeAtom); const [mode] = useAtom(modeAtom);
const {isSyncing} = useSyncEvents(); const {isSyncing} = useSyncEvents();
useCalSync(); useCalSync();

View File

@ -31,7 +31,7 @@ interface FormattedEvent {
// Precompute time constants // Precompute time constants
const DAY_IN_MS = 24 * 60 * 60 * 1000; const DAY_IN_MS = 24 * 60 * 60 * 1000;
const PERIOD_IN_MS = 45 * DAY_IN_MS; const PERIOD_IN_MS = 5 * DAY_IN_MS;
const TIME_ZONE = Intl.DateTimeFormat().resolvedOptions().timeZone; const TIME_ZONE = Intl.DateTimeFormat().resolvedOptions().timeZone;
// Memoize date range calculations // Memoize date range calculations

View File

@ -152,7 +152,11 @@ export const AuthContextProvider: FC<{ children: ReactNode }> = ({children}) =>
useEffect(() => { useEffect(() => {
if (!initializing) { if (!initializing) {
SplashScreen.hideAsync(); if(auth().currentUser) {
setTimeout(() => SplashScreen.hideAsync(), 1000);
} else {
SplashScreen.hideAsync();
}
} }
}, [initializing]); }, [initializing]);

View File

@ -333,7 +333,7 @@ exports.processEventBatches = functions.pubsub
let notificationMessage; let notificationMessage;
if (externalOrigin) { if (externalOrigin) {
notificationMessage = `Calendar sync completed: ${events.length} ${events.length === 1 ? 'event has' : 'events have'} been added.`; // notificationMessage = `Calendar sync completed: ${events.length} ${events.length === 1 ? 'event has' : 'events have'} been added.`;
} else { } else {
notificationMessage = events.length === 1 notificationMessage = events.length === 1
? `New event "${events[0].title}" has been added to the family calendar.` ? `New event "${events[0].title}" has been added to the family calendar.`

View File

@ -1,39 +1,70 @@
import {useMutation, useQueryClient} from "@tanstack/react-query"; import {useMutation, useQueryClient} from "@tanstack/react-query";
import firestore from "@react-native-firebase/firestore"; import firestore from "@react-native-firebase/firestore";
import {useAuthContext} from "@/contexts/AuthContext";
export const useDeleteEvent = () => { export const useDeleteEvent = () => {
const {user, profileData} = useAuthContext();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationKey: ["deleteEvent"], mutationKey: ["deleteEvent"],
mutationFn: async ({eventId, docId}: { eventId?: string; docId?: string }) => { mutationFn: async ({eventId, docId}: { eventId?: string; docId?: string }) => {
try { try {
if (docId) { await firestore()
await firestore() .collection("Events")
.collection("Events") .doc(eventId ?? docId)
.doc(docId) .delete();
.delete();
} else if (eventId) {
const snapshot = await firestore()
.collection("Events")
.where("id", "==", eventId)
.get();
const doc = snapshot.docs[0];
if (doc) {
await doc.ref.delete();
} else {
console.warn("Event not found");
}
} else {
console.warn("No identifier provided");
}
} catch (e) { } catch (e) {
console.error(e); console.error(e);
throw e;
} }
}, },
onSuccess: () => { onMutate: async ({eventId, docId}) => {
queryClient.invalidateQueries({queryKey: ["events"]}); await queryClient.cancelQueries({
queryKey: ["events", user?.uid]
});
const previousPersonalEvents = queryClient.getQueryData(["events", user?.uid, false]);
const previousFamilyEvents = queryClient.getQueryData(["events", user?.uid, true]);
const updateQueryData = (old: any[] | undefined) =>
old?.filter(event => event.id !== (eventId ?? docId));
queryClient.setQueryData(
["events", user?.uid, false],
updateQueryData
);
queryClient.setQueryData(
["events", user?.uid, true],
updateQueryData
);
return {previousPersonalEvents, previousFamilyEvents};
},
onError: (err, variables, context) => {
queryClient.setQueryData(
["events", user?.uid, false],
context?.previousPersonalEvents
);
queryClient.setQueryData(
["events", user?.uid, true],
context?.previousFamilyEvents
);
},
onSettled: () => {
if (profileData?.familyId) {
firestore()
.collection("Households")
.where("familyId", "==", profileData.familyId)
.get()
.then(snapshot => {
snapshot.docs.forEach(doc => {
doc.ref.update({
lastSyncTimestamp: firestore.FieldValue.serverTimestamp()
});
});
});
}
} }
}); });
}; };

View File

@ -139,7 +139,7 @@ export const useGetEvents = () => {
return useQuery({ return useQuery({
queryKey: ["events", user?.uid, isFamilyView], queryKey: ["events", user?.uid, isFamilyView],
queryFn: () => fetchEvents(user?.uid!, profileData?.familyId, isFamilyView), queryFn: () => fetchEvents(user?.uid!, profileData?.familyId, isFamilyView),
staleTime: 5 * 60 * 1000, staleTime: Infinity,
gcTime: Infinity, gcTime: Infinity,
placeholderData: (previousData) => previousData, placeholderData: (previousData) => previousData,
enabled: Boolean(user?.uid), enabled: Boolean(user?.uid),