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

View File

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

View File

@ -31,7 +31,7 @@ interface FormattedEvent {
// Precompute time constants
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;
// Memoize date range calculations

View File

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

View File

@ -333,7 +333,7 @@ exports.processEventBatches = functions.pubsub
let notificationMessage;
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 {
notificationMessage = events.length === 1
? `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 firestore from "@react-native-firebase/firestore";
import {useAuthContext} from "@/contexts/AuthContext";
export const useDeleteEvent = () => {
const {user, profileData} = useAuthContext();
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["deleteEvent"],
mutationFn: async ({eventId, docId}: { eventId?: string; docId?: string }) => {
try {
if (docId) {
await firestore()
.collection("Events")
.doc(docId)
.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");
}
await firestore()
.collection("Events")
.doc(eventId ?? docId)
.delete();
} catch (e) {
console.error(e);
throw e;
}
},
onSuccess: () => {
queryClient.invalidateQueries({queryKey: ["events"]});
onMutate: async ({eventId, docId}) => {
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({
queryKey: ["events", user?.uid, isFamilyView],
queryFn: () => fetchEvents(user?.uid!, profileData?.familyId, isFamilyView),
staleTime: 5 * 60 * 1000,
staleTime: Infinity,
gcTime: Infinity,
placeholderData: (previousData) => previousData,
enabled: Boolean(user?.uid),