Skip to content

Commit

Permalink
update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsupa597 committed Feb 5, 2023
1 parent 479a100 commit b73b457
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
34 changes: 17 additions & 17 deletions pages/nft-collections/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import FilterMenu from 'components/FilterMenu'
import { SIZES } from 'components/PageSize'
import NoDataIcon from 'assets/icons/no-data.svg'
import SortIcon from 'assets/icons/sort.svg'
import { client, GraphQLSchema, handleDeleteInvalid } from 'utils'
import { client, GraphQLSchema, handleSorterArrayAfterClick, handleSorterArrayFromUrl } from 'utils'
import styles from './styles.module.scss'

interface Variables {
Expand Down Expand Up @@ -108,24 +108,24 @@ const NftCollectionList = () => {
} = useRouter()
const [t] = useTranslation(['nft', 'common', 'list'])
const title = t(`nft-collections`)
const handleSorter = () => {
let originalSorter = {
holder_count_sort,
name_sort,
minted_count_sort,
}

// delete the invalid properties
const validSorter = handleDeleteInvalid(originalSorter)
const handleSorterValues = (url, sorters) => {
const paramArr = url.slice(url.indexOf('?') + 1).split('&')
const sorterParams = []

return Object.keys(validSorter)?.[0]
? {
sort_type: Object.values(validSorter)[0],
sort_value: UdtsSorterValueEnum[Object.keys(validSorter)[0]],
}
: null
paramArr.map(param => {
const [key, val] = param.split('=')
const decodeVal = decodeURIComponent(val)

sorters.includes(key) && sorterParams.push({ sort_type: decodeVal, sort_value: UdtsSorterValueEnum[key] })
})
return sorterParams
}

const sorters = ['holder_count_sort', 'name_sort', 'minted_count_sort']
const pathSorterArray = handleSorterArrayFromUrl(asPath, sorters)
const sorterArray = handleSorterValues(asPath, sorters)

const { isLoading, data: list } = useQuery(
['erc721-list', page_size, before, after, name, holder_count_sort, name_sort, minted_count_sort],
() =>
Expand All @@ -134,7 +134,7 @@ const NftCollectionList = () => {
after: after as string,
name: name ? `${name}%` : null,
limit: Number.isNaN(!page_size) ? +SIZES[1] : +page_size,
sorter: handleSorter() ? [handleSorter()] : [],
sorter: sorterArray || [],
}),
{ refetchInterval: 10000 },
)
Expand All @@ -148,7 +148,7 @@ const NftCollectionList = () => {
...restQuery,
name: name ? (name as string) : '',
page_size: page_size as string,
[type]: order === 'DESC' ? 'ASC' : 'DESC',
...handleSorterArrayAfterClick(pathSorterArray, { type, order: order === 'DESC' ? 'ASC' : 'DESC' }),
})}`,
)
}
Expand Down
25 changes: 19 additions & 6 deletions utils/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,24 @@ export const handleNftImageLoadError = (e: React.SyntheticEvent<HTMLImageElement
e.currentTarget.src = '/images/nft-placeholder.svg'
}

export const handleDeleteInvalid = (obj: Record<string, any>) => {
Object.keys(obj).forEach(item => {
if (!obj[item] && obj[item] != 0) {
delete obj[item]
}
export const handleSorterArrayFromUrl = (url, sorters) => {
const paramArr = url.slice(url.indexOf('?') + 1).split('&')
const sorterParams = []

paramArr.map(param => {
const [key, val] = param.split('=')
const decodeVal = decodeURIComponent(val)

sorters.includes(key) && sorterParams.push({ type: key, order: decodeVal })
})
return obj
return sorterParams
}

export const handleSorterArrayAfterClick = (pathSorterArray, onClickedSorter) => {
const { type } = onClickedSorter

const arrayExcludeClickedSorter = pathSorterArray.filter(item => item.type !== type)
arrayExcludeClickedSorter.unshift(onClickedSorter)

return arrayExcludeClickedSorter.reduce((pre, cur) => ({ ...pre, [cur.type]: cur.order }), {})
}

0 comments on commit b73b457

Please sign in to comment.