Event filtering

This commit is contained in:
Milan Paunovic
2024-10-30 02:41:00 +01:00
parent 7f79a1c819
commit 01338e7c70
2 changed files with 26 additions and 18 deletions

View File

@ -13,6 +13,7 @@ import {
import { useAuthContext } from "@/contexts/AuthContext"; import { useAuthContext } from "@/contexts/AuthContext";
import { CalendarEvent } from "@/components/pages/calendar/interfaces"; import { CalendarEvent } from "@/components/pages/calendar/interfaces";
import { Text } from "react-native-ui-lib"; import { Text } from "react-native-ui-lib";
import {addDays, isWithinInterval, subDays} from "date-fns";
interface EventCalendarProps { interface EventCalendarProps {
calendarHeight: number; calendarHeight: number;
@ -46,7 +47,7 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
setIsRendering(true); setIsRendering(true);
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
setIsRendering(false); setIsRendering(false);
}, 300); }, 10 );
return () => clearTimeout(timeout); return () => clearTimeout(timeout);
} }
}, [events, mode]); }, [events, mode]);
@ -125,7 +126,16 @@ export const EventCalendar: React.FC<EventCalendarProps> = React.memo(
} }
}, [mode]); }, [mode]);
const memoizedEvents = useMemo(() => events ?? [], [events]); const memoizedEvents = useMemo(() => {
const startOffset = mode === "month" ? 40 : mode === "week" ? 10 : 1;
const endOffset = mode === "month" ? 40 : mode === "week" ? 10 : 1;
return events?.filter(event =>
event.start && event.end &&
isWithinInterval(event.start, { start: subDays(selectedDate, startOffset), end: addDays(selectedDate, endOffset) }) &&
isWithinInterval(event.end, { start: subDays(selectedDate, startOffset), end: addDays(selectedDate, endOffset) })
) ?? [];
}, [events, selectedDate, mode]);
const renderCustomDateForMonth = (date: Date) => { const renderCustomDateForMonth = (date: Date) => {
const circleStyle = useMemo<ViewStyle>( const circleStyle = useMemo<ViewStyle>(

View File

@ -18,7 +18,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
.document('Events/{eventId}') .document('Events/{eventId}')
.onCreate(async (snapshot, context) => { .onCreate(async (snapshot, context) => {
const eventData = snapshot.data(); const eventData = snapshot.data();
const {familyId, creatorId, email} = eventData; const { familyId, creatorId, email } = eventData;
if (email) { if (email) {
console.log('Event has an email field. Skipping notification.'); console.log('Event has an email field. Skipping notification.');
@ -30,12 +30,11 @@ exports.sendNotificationOnEventCreation = functions.firestore
return; return;
} }
let pushTokens = await getPushTokensForFamilyExcludingCreator(familyId, creatorId);
if (!pushTokens.length) { if (!pushTokens.length) {
pushTokens = await getPushTokensForFamilyExcludingCreator(familyId, creatorId); console.log('No push tokens available for the event.');
if (!pushTokens.length) { return;
console.log('No push tokens available for the event.');
return;
}
} }
eventCount++; eventCount++;
@ -49,21 +48,20 @@ exports.sendNotificationOnEventCreation = functions.firestore
? `An event "${eventData.title}" has been added. Check it out!` ? `An event "${eventData.title}" has been added. Check it out!`
: `${eventCount} new events have been added.`; : `${eventCount} new events have been added.`;
let messages = []; let messages = pushTokens.map(pushToken => {
for (let pushToken of pushTokens) {
if (!Expo.isExpoPushToken(pushToken)) { if (!Expo.isExpoPushToken(pushToken)) {
console.error(`Push token ${pushToken} is not a valid Expo push token`); console.error(`Push token ${pushToken} is not a valid Expo push token`);
continue; return null;
} }
messages.push({ return {
to: pushToken, to: pushToken,
sound: 'default', sound: 'default',
title: 'New Events Added!', title: 'New Events Added!',
body: eventMessage, body: eventMessage,
data: {eventId: context.params.eventId}, data: { eventId: context.params.eventId },
}); };
} }).filter(Boolean);
let chunks = expo.chunkPushNotifications(messages); let chunks = expo.chunkPushNotifications(messages);
let tickets = []; let tickets = [];
@ -78,7 +76,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
console.log('Notification successfully sent:', ticket.id); console.log('Notification successfully sent:', ticket.id);
} else if (ticket.status === 'error') { } else if (ticket.status === 'error') {
console.error(`Notification error: ${ticket.message}`); console.error(`Notification error: ${ticket.message}`);
if (ticket.details && ticket.details.error === 'DeviceNotRegistered') { if (ticket.details?.error === 'DeviceNotRegistered') {
await removeInvalidPushToken(ticket.to); await removeInvalidPushToken(ticket.to);
} }
} }
@ -88,8 +86,8 @@ exports.sendNotificationOnEventCreation = functions.firestore
} }
} }
eventCount = 0; // Reset the event count after sending notification eventCount = 0;
pushTokens = []; // Reset push tokens for the next round pushTokens = [];
}, 5000); }, 5000);
}); });