Skip to content

Commit

Permalink
feat: sort categories and subcategories
Browse files Browse the repository at this point in the history
  • Loading branch information
einal3m committed Nov 7, 2024
1 parent 6f0e8b7 commit b199b85
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions app/javascript/hooks/useGroupedCategories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ type UseGroupedCategories = {
currentSubcategory?: Subcategory
}

const sortFn = (
a: Category | Subcategory,
b: Category | Subcategory,
): number => {
const nameA = a.name.toUpperCase()
const nameB = b.name.toUpperCase()
if (nameA < nameB) {
return -1
}
if (nameA > nameB) {
return 1
}

return 0
}

export const useGroupedCategories = (): UseGroupedCategories => {
const {
data: categories,
Expand Down Expand Up @@ -53,20 +69,22 @@ export const useGroupedCategories = (): UseGroupedCategories => {
const isSuccess = isSuccessC && isSuccessS && isSuccessT

const groupedCategories = categoryTypes
? categoryTypes
.map((ct) => ({
categoryType: ct,
categories: categories
? categories
.filter((c) => c.categoryTypeId == ct.id)
.map((c) => ({
...c,
subcategories: subcategories
? subcategories?.filter((s) => s.categoryId == c.id)
: [],
}))
: [],
}))
? categoryTypes.map((ct) => ({
categoryType: ct,
categories: categories
? categories
.filter((c) => c.categoryTypeId == ct.id)
.sort(sortFn)
.map((c) => ({
...c,
subcategories: subcategories
? subcategories
?.filter((s) => s.categoryId == c.id)
.sort(sortFn)
: [],
}))
: [],
}))
: []

return {
Expand Down

0 comments on commit b199b85

Please sign in to comment.