From 569c81360494a7179393894bf00e614977e84445 Mon Sep 17 00:00:00 2001 From: Junping Luo <53324036+JacE070@users.noreply.github.com> Date: Wed, 17 Jan 2024 09:30:39 -0800 Subject: [PATCH] Add customEventChange listener --- apps/antalmanac/src/components/Map/Map.tsx | 33 ++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/apps/antalmanac/src/components/Map/Map.tsx b/apps/antalmanac/src/components/Map/Map.tsx index bc4d493d5..4516c7ef3 100644 --- a/apps/antalmanac/src/components/Map/Map.tsx +++ b/apps/antalmanac/src/components/Map/Map.tsx @@ -148,7 +148,7 @@ export default function CourseMap() { const [searchParams] = useSearchParams(); const [selectedDayIndex, setSelectedDay] = useState(0); const [markers, setMarkers] = useState(getCoursesPerBuilding()); - const [customEventMarkers] = useState(getCustomEventPerBuilding()); + const [customEventMarkers, setCustomEventMarkers] = useState(getCustomEventPerBuilding()); const [calendarEvents, setCalendarEvents] = useState(AppStore.getCourseEventsInCalendar()); useEffect(() => { @@ -181,6 +181,20 @@ export default function CourseMap() { }; }, []); + useEffect(() => { + const updateCustomEventMarkers = () => { + setCustomEventMarkers(getCustomEventPerBuilding()); + }; + + AppStore.on('customEventsChange', updateCustomEventMarkers); + AppStore.on('currentScheduleIndexChange', updateCustomEventMarkers); + + return () => { + AppStore.removeListener('customEventsChange', updateCustomEventMarkers); + AppStore.removeListener('currentScheduleIndexChange', updateCustomEventMarkers); + }; + }, []); + useEffect(() => { const locationID = Number(searchParams.get('location') ?? 0); const building = locationID in buildingCatalogue ? buildingCatalogue[locationID] : undefined; @@ -275,16 +289,13 @@ export default function CourseMap() { */ const startDestPairs = useMemo(() => { const allEvents = [...markersToDisplay, ...customEventMarkersToDisplay]; - return allEvents.reduce( - (acc, cur, index) => { - acc.push([cur]); - if (index > 0) { - acc[index - 1].push(cur); - } - return acc; - }, - [] as (typeof allEvents)[] - ); + return allEvents.reduce((acc, cur, index) => { + acc.push([cur]); + if (index > 0) { + acc[index - 1].push(cur); + } + return acc; + }, [] as (typeof allEvents)[]); }, [markersToDisplay, customEventMarkersToDisplay]); return (