From 65bb39e3c7a309da15c7434570079e1ea246c915 Mon Sep 17 00:00:00 2001 From: "Aman Kumar [SSW]" <71385247+amankumarrr@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:20:44 +1100 Subject: [PATCH] removing redundant code --- helpers/getTrimmedEvents.ts | 20 ++++++++++++++++++++ hooks/useFetchEvents.ts | 32 +++----------------------------- 2 files changed, 23 insertions(+), 29 deletions(-) create mode 100644 helpers/getTrimmedEvents.ts diff --git a/helpers/getTrimmedEvents.ts b/helpers/getTrimmedEvents.ts new file mode 100644 index 0000000000..917cae4d2e --- /dev/null +++ b/helpers/getTrimmedEvents.ts @@ -0,0 +1,20 @@ +export const getTrimmedEvent = (events) => + events?.pages.flat().flatMap((item) => + item.eventsCalendarConnection.edges.map((edge) => ({ + ...edge.node, + startDateTime: new Date(edge.node.startDateTime), + endDateTime: new Date(edge.node.endDateTime), + category: formatCategory(edge.node.category), + })) + ) || []; + +export const formatCategory = (category: string): string => { + { + const categoryReplacements = { + "Non-English Courses": "Other", + }; + const lookup = categoryReplacements[category]; + + return lookup ? lookup : category; + } +}; diff --git a/hooks/useFetchEvents.ts b/hooks/useFetchEvents.ts index 2b77b284df..41ea779c62 100644 --- a/hooks/useFetchEvents.ts +++ b/hooks/useFetchEvents.ts @@ -1,4 +1,5 @@ import { EventFilterAllCategories } from "@/components/filter/FilterBlock"; +import { formatCategory, getTrimmedEvent } from "@/helpers/getTrimmedEvents"; import { EVENTS_MAX_SIZE_OVERRIDE } from "@/services/server/getEvents"; import { GetPastEventsQueryQuery } from "@/tina/types"; import { useInfiniteQuery } from "@tanstack/react-query"; @@ -20,17 +21,6 @@ const getCategoriesForFilter = (category: string) => { return lookup ? lookup : [category]; }; -const formatCategory = (category: string): string => { - { - const categoryReplacements = { - "Non-English Courses": "Other", - }; - const lookup = categoryReplacements[category]; - - return lookup ? lookup : category; - } -}; - export const TODAY = new Date(); TODAY.setHours(0, 0, 0, 0); @@ -69,15 +59,7 @@ export const useFetchFutureEvents = (filters: SelectedCategories) => { }, }); return { - futureEvents: - data?.pages.flat().flatMap((item) => - item.eventsCalendarConnection.edges.map((edge) => ({ - ...edge.node, - startDateTime: new Date(edge.node.startDateTime), - endDateTime: new Date(edge.node.endDateTime), - category: formatCategory(edge.node.category), - })) - ) || [], + futureEvents: getTrimmedEvent(data), error, isLoadingFuturePages: isLoading, fetchFutureNextPage: fetchNextPage, @@ -117,15 +99,7 @@ export const useFetchPastEvents = (filters: SelectedCategories) => { }); return { - pastEvents: - data?.pages.flat().flatMap((item) => - item.eventsCalendarConnection.edges.map((edge) => ({ - ...edge.node, - startDateTime: new Date(edge.node.startDateTime), - endDateTime: new Date(edge.node.endDateTime), - category: formatCategory(edge.node.category), - })) - ) || [], + pastEvents: getTrimmedEvent(data), error, isLoadingPastPages: isLoading, fetchNextPastPage: fetchNextPage,