From b617d89f0577fc00981cb4330bb98710a215d799 Mon Sep 17 00:00:00 2001 From: ImJustChew Date: Sun, 2 Jun 2024 15:47:14 +0800 Subject: [PATCH] fix: merged compulsory and elective into one --- .../courses/ClasssRefinementItem.tsx | 60 ++++--------------- src/app/api/sync-algolia/route.ts | 3 +- 2 files changed, 14 insertions(+), 49 deletions(-) diff --git a/src/app/[lang]/(mods-pages)/courses/ClasssRefinementItem.tsx b/src/app/[lang]/(mods-pages)/courses/ClasssRefinementItem.tsx index 5ab16487..92c07e0e 100644 --- a/src/app/[lang]/(mods-pages)/courses/ClasssRefinementItem.tsx +++ b/src/app/[lang]/(mods-pages)/courses/ClasssRefinementItem.tsx @@ -26,33 +26,16 @@ const ClassRefinementItem = ({ }) => { const { - items: compulsoryItems, - refine: refineCompulsory, - searchForItems: searchForCompulsory, - canToggleShowMore: canToggleShowMoreCompulsory, - isShowingMore: isShowingMoreCompulsory, - toggleShowMore: toggleShowMoreCompulsory, - } = useCustomRefinementList({ - attribute: 'compulsory_for', - limit: limit, - }) - - const { - items: electiveItems, - refine: refineElective, - searchForItems: searchForElective, - canToggleShowMore: canToggleShowMoreElective, - isShowingMore: isShowingMoreElective, - toggleShowMore: toggleShowMoreElective, + items, + refine, + searchForItems, } = useCustomRefinementList({ - attribute: 'elective_for', + attribute: 'for_class', limit: limit, }) const { items: semesterItem, - refine, - canRefine, } = useCustomMenu({ attribute: 'semester', }); @@ -61,20 +44,9 @@ const ClassRefinementItem = ({ const searchParams = useSearchParams() useEffect(() => { - const refinedCompulsory = compulsoryItems.filter((item) => item.isRefined) - const refinedElective = electiveItems.filter((item) => item.isRefined) - // const merged = [...refinedCompulsory, ...refinedElective] - // merge but remove duplicates - const merged = [...refinedCompulsory, ...refinedElective.filter((item) => !refinedCompulsory.some((i) => i.value == item.value))] - setSelected(merged.map((item) => item.value)) - }, [compulsoryItems, electiveItems, searchParams]) - - //sort by name - const items = [...compulsoryItems, ...electiveItems.filter((item) => !compulsoryItems.some((i) => i.value == item.value))].sort((a, b) => { - if (a.label < b.label) return -1 - if (a.label > b.label) return 1 - return 0 - }) + const refinedItems = items.filter((item) => item.isRefined) + setSelected(refinedItems.map((item) => item.value)) + }, [items, searchParams]) const [searchValue, setSearchValue] = useState('') const [searching, setSearching] = useState(false) @@ -83,15 +55,11 @@ const ClassRefinementItem = ({ const search = (name: string) => { setSearchValue(name) if (!clientSearch) { - // searchForItems(name) - searchForCompulsory(name) - searchForElective(name) + searchForItems(name) } if (name == '') { - const refinedCompulsory = compulsoryItems.filter((item) => item.isRefined) - const refinedElective = electiveItems.filter((item) => item.isRefined) - const merged = [...refinedCompulsory, ...refinedElective.filter((item) => !refinedCompulsory.some((i) => i.value == item.value))] - setSelected(merged.map((item) => item.value)) + const refined = items.filter((item) => item.isRefined) + setSelected(refined.map((item) => item.value)) } } @@ -107,16 +75,12 @@ const ClassRefinementItem = ({ const select = (value: string) => { setSearchValue('') - // refine(value) - refineCompulsory(value) - refineElective(value) + refine(value) } const clear = () => { selected.forEach((value) => { - // refine(value) - refineCompulsory(value) - refineElective(value) + refine(value) }) } diff --git a/src/app/api/sync-algolia/route.ts b/src/app/api/sync-algolia/route.ts index aa6bd971..58791261 100644 --- a/src/app/api/sync-algolia/route.ts +++ b/src/app/api/sync-algolia/route.ts @@ -17,8 +17,9 @@ const syncCoursesToAlgolia = async (semester: string) => { }, [] as Database['public']['Tables']['courses']['Row'][][]); for(const chunk of chunked) { - const algoliaChunk = chunk.map(course => ({ + const algoliaChunk = chunk.map(({ elective_for, compulsory_for , ...course}) => ({ ...course, + for_class: [...(elective_for || []), ...(compulsory_for || [])], objectID: course.raw_id, separate_times: course.times.flatMap(s => s.match(/.{1,2}/g)), }))