Skip to content

Commit

Permalink
optimize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsupa597 committed Mar 7, 2023
1 parent 8e90578 commit 9b9e0e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
21 changes: 13 additions & 8 deletions pages/multi-token-collections/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from 'react'
import type { GetStaticProps } from 'next'
import { useEffect } from 'react'
import { useRouter } from 'next/router'
import NextLink from 'next/link'
import { useTranslation } from 'next-i18next'
Expand All @@ -19,7 +19,7 @@ import { SIZES } from 'components/PageSize'

import NoDataIcon from 'assets/icons/no-data.svg'
import SortIcon from 'assets/icons/sort.svg'
import { client, GraphQLSchema, handleSorterArrayToMap, handleSorterArrayAboutPath } from 'utils'
import { client, GraphQLSchema, handleSorterArrayAboutPath, sorterType, handleSorterArrayInOrder } from 'utils'

import styles from './styles.module.scss'

Expand Down Expand Up @@ -118,7 +118,7 @@ const MultiTokenCollectionList = () => {
const title = t(`multi-token-collections`)

const sorters = ['holder_count_sort', 'name_sort', 'type_count_sort']
const DEFAULT_SORTERS = [
const DEFAULT_SORTERS: sorterType[] = [
{ type: 'name_sort', order: 'ASC' },
{ type: 'type_count_sort', order: 'ASC' },
{ type: 'holder_count_sort', order: 'ASC' },
Expand All @@ -129,13 +129,18 @@ const MultiTokenCollectionList = () => {
// get a sorter array to query listdata from server
const sorterArrayForQuery = handleSorterArrayAboutPath(asPath, sorters, UdtsSorterValueEnum)

const handleUrlForPush = (clickedSorter = null) => {
return `${asPath.split('?')[0] ?? ''}?${new URLSearchParams({
const handleUrlForPush = (clickedSorter: sorterType = null) => {
const searchParams = new URLSearchParams({
...restQuery,
name: name ? (name as string) : '',
page_size: page_size as string,
...handleSorterArrayToMap(clickedSorter ? sorterArrayFromPath : DEFAULT_SORTERS, clickedSorter),
})}`
})

const orderedSorter = handleSorterArrayInOrder(clickedSorter ? sorterArrayFromPath : DEFAULT_SORTERS, clickedSorter)
for (const item of orderedSorter) {
searchParams.append(item.type, item.order)
}

return `${asPath.split('?')[0] ?? ''}?${searchParams}`
}

useEffect(() => {
Expand Down
31 changes: 21 additions & 10 deletions utils/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ export const handleNftImageLoadError = (e: React.SyntheticEvent<HTMLImageElement
}

// return a sorter array
export const handleSorterArrayAboutPath = (url, sorters, sorterValueEnum = null) => {
export const handleSorterArrayAboutPath = (url: string, sorters: string[], sorterValueEnum = null) => {
const params = url.slice(url.indexOf('?') + 1)
const sorterParamsArray = []

const searchParams = new URLSearchParams(params)
Array(...searchParams.keys()).forEach((item, index) => {
const keys = [...searchParams.keys()]

keys?.map((item, index) => {
if (sorters.includes(item)) {
// return sort array which used for query, like: [{sort_type: ASC , sort_value: xxx}]
if (sorterValueEnum) {
Expand All @@ -98,17 +100,26 @@ export const handleSorterArrayAboutPath = (url, sorters, sorterValueEnum = null)

return sorterParamsArray
}
export type sorterType = {
type: string
order: 'ASC' | 'DESC'
}

export const handleSorterArrayToMap = (pathSorterArray, onClickedSorter = null) => {
export const handleSorterArrayInOrder = (pathSorterArray: sorterType[], onClickedSorter: sorterType = null) => {
if (onClickedSorter) {
const { type } = onClickedSorter
const arrayExcludeClickedSorter = pathSorterArray.filter(item => item.type !== type)

arrayExcludeClickedSorter.unshift(onClickedSorter)

// return a sorter map with the clicked one is on the first position
return arrayExcludeClickedSorter.reduce((pre, cur) => ({ ...pre, [cur.type]: cur.order }), {})
} else {
return pathSorterArray.reduce((pre, cur) => ({ ...pre, [cur.type]: cur.order }), {})
// return a sorter array with the clicked one is on the first position
pathSorterArray.sort((preSorter, curSorter) => {
if (preSorter.type === type) {
return -1
} else if (curSorter.type === type) {
return 1
} else {
return 0
}
})
pathSorterArray[0] = onClickedSorter
}
return pathSorterArray
}

0 comments on commit 9b9e0e6

Please sign in to comment.