Skip to content

Commit

Permalink
chore(boards): refactor sortboard function
Browse files Browse the repository at this point in the history
  • Loading branch information
purusott committed Jul 29, 2024
1 parent c09564f commit 2fddf53
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions next-tavla/app/(admin)/boards/hooks/useSortBoardFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ function useSortBoardFunction() {
const value = useSearchParam('sort')
const sortParams = value?.split(':')

const defaultColumn: TBoardsColumn =
const sortColumn: TBoardsColumn =
(sortParams?.[0] as TBoardsColumn) || DEFAULT_SORT_COLUMN
const defaultType: TSort = (sortParams?.[1] as TSort) || DEFAULT_SORT_TYPE
const sortType: TSort = (sortParams?.[1] as TSort) || DEFAULT_SORT_TYPE

const sortBoards = useCallback(
(boardA: TBoardWithOrganizaion, boardB: TBoardWithOrganizaion) => {
let sortFunc: () => number
const sort = {
column: defaultColumn,
type: defaultType,
}

const compareTitle = () => {
const titleA =
boardA?.board.meta?.title?.toLowerCase() ??
Expand All @@ -33,7 +28,7 @@ function useSortBoardFunction() {
return titleB.localeCompare(titleA)
}

switch (sort.column) {
switch (sortColumn) {
case 'lastModified':
sortFunc = () => {
const modifiedA = boardA.board.meta?.dateModified ?? 0
Expand All @@ -49,7 +44,7 @@ function useSortBoardFunction() {
boardB.organization?.name?.toLowerCase() ?? 'Privat'

if (orgNameA == orgNameB) {
return sort.type == 'ascending'
return sortType == 'ascending'
? compareTitle()
: -compareTitle()
}
Expand All @@ -62,7 +57,7 @@ function useSortBoardFunction() {
}
break
}
switch (sort.type) {
switch (sortType) {
case 'ascending':
return -sortFunc()
case 'descending':
Expand All @@ -71,7 +66,7 @@ function useSortBoardFunction() {
return 0
}
},
[defaultColumn, defaultType],
[sortColumn, sortType],
)

return sortBoards
Expand Down

0 comments on commit 2fddf53

Please sign in to comment.