Skip to content

Commit

Permalink
fix: merged compulsory and elective into one
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustChew committed Jun 2, 2024
1 parent f509fe5 commit b617d89
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 49 deletions.
60 changes: 12 additions & 48 deletions src/app/[lang]/(mods-pages)/courses/ClasssRefinementItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand All @@ -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)
Expand All @@ -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))
}
}

Expand All @@ -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)
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/app/api/sync-algolia/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}))
Expand Down

0 comments on commit b617d89

Please sign in to comment.