Skip to content

Commit

Permalink
fix: sort table items + pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
amalcaraz committed Mar 5, 2024
1 parent 4cc790e commit d2ff82c
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 25 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint:fix": "next lint --fix"
},
"dependencies": {
"@aleph-front/core": "^1.15.67",
"@aleph-front/core": "^1.15.70",
"@fortawesome/fontawesome-svg-core": "^6.3.0",
"@helia/unixfs": "^1.4.1",
"@hookform/resolvers": "^3.1.1",
Expand Down
4 changes: 4 additions & 0 deletions src/components/common/ComputeResourceNodesTable/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import LinkCRNButton from '../LinkCRNButton'
import ButtonLink from '../ButtonLink'
import Image from 'next/image'
import { apiServer } from '@/helpers/constants'
import { UseSortedListReturn } from '@/hooks/common/useSortedList'

export type ComputeResourceNodesTableProps = {
filteredNodes?: CRN[]
Expand All @@ -25,6 +26,7 @@ export type ComputeResourceNodesTableProps = {
nodesIssues?: Record<string, string>
loadItemsDisabled?: boolean
handleLoadItems?: () => Promise<void>
handleSortItems?: UseSortedListReturn<CRN>['handleSortItems']
handleLink: (nodeHash: string) => void
handleUnlink: (nodeHash: string) => void
}
Expand All @@ -37,6 +39,7 @@ export const ComputeResourceNodesTable = ({
nodesIssues,
loadItemsDisabled,
handleLoadItems,
handleSortItems,
handleLink: onLink,
handleUnlink: onUnlink,
}: ComputeResourceNodesTableProps) => {
Expand Down Expand Up @@ -155,6 +158,7 @@ export const ComputeResourceNodesTable = ({
data: filteredNodes,
infiniteScroll: !loadItemsDisabled,
onLoadMore: handleLoadItems,
onSort: handleSortItems,
}}
/>
)
Expand Down
4 changes: 4 additions & 0 deletions src/components/common/CoreChannelNodesTable/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import NodeStaked from '@/components/common/NodeStaked'
import ButtonLink from '../ButtonLink'
import { apiServer } from '@/helpers/constants'
import Image from 'next/image'
import { UseSortedListReturn } from '@/hooks/common/useSortedList'

export type CoreChannelNodesTableProps = {
nodes: CCN[]
Expand All @@ -23,6 +24,7 @@ export type CoreChannelNodesTableProps = {
nodesIssues?: Record<string, string>
loadItemsDisabled?: boolean
handleLoadItems?: () => Promise<void>
handleSortItems?: UseSortedListReturn<CCN>['handleSortItems']
}

export const CoreChannelNodesTable = ({
Expand All @@ -32,6 +34,7 @@ export const CoreChannelNodesTable = ({
nodesIssues,
loadItemsDisabled,
handleLoadItems,
handleSortItems,
}: CoreChannelNodesTableProps) => {
const columns = useMemo(() => {
return [
Expand Down Expand Up @@ -127,6 +130,7 @@ export const CoreChannelNodesTable = ({
data: filteredNodes,
infiniteScroll: !loadItemsDisabled,
onLoadMore: handleLoadItems,
onSort: handleSortItems,
}}
/>
)
Expand Down
4 changes: 4 additions & 0 deletions src/components/common/StakingNodesTable/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import tw from 'twin.macro'
import ButtonLink from '../ButtonLink'
import { apiServer } from '@/helpers/constants'
import Image from 'next/image'
import { UseSortedListReturn } from '@/hooks/common/useSortedList'

export type StakingNodesTableProps = {
nodes: CCN[]
Expand All @@ -27,6 +28,7 @@ export type StakingNodesTableProps = {
nodesIssues?: Record<string, string>
loadItemsDisabled?: boolean
handleLoadItems?: () => Promise<void>
handleSortItems?: UseSortedListReturn<CCN>['handleSortItems']
handleStake: (nodeHash: string) => void
handleUnstake: (nodeHash: string) => void
}
Expand All @@ -40,6 +42,7 @@ export const StakingNodesTable = ({
nodesIssues,
loadItemsDisabled,
handleLoadItems,
handleSortItems,
handleStake: onStake,
handleUnstake: onUnstake,
}: StakingNodesTableProps) => {
Expand Down Expand Up @@ -156,6 +159,7 @@ export const StakingNodesTable = ({
data: filteredNodes,
infiniteScroll: !loadItemsDisabled,
onLoadMore: handleLoadItems,
onSort: handleSortItems,
}}
/>
)
Expand Down
6 changes: 4 additions & 2 deletions src/components/pages/earn/ComputeResourceNodesPage/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const ComputeResourceNodesPage = (
const {
account,
nodes,
paginatedFilteredNodes,
paginatedSortedFilteredNodes,
userNodes,
filteredUserNodes,
userNodesIssues,
Expand All @@ -35,6 +35,7 @@ export const ComputeResourceNodesPage = (
totalResources,
loadItemsDisabled,
handleLoadItems,
handleSortItems,
handleLink,
handleUnlink,
handleTabChange,
Expand Down Expand Up @@ -181,9 +182,10 @@ export const ComputeResourceNodesPage = (
<ComputeResourceNodesTable
{...{
nodes,
filteredNodes: paginatedFilteredNodes,
filteredNodes: paginatedSortedFilteredNodes,
loadItemsDisabled,
handleLoadItems,
handleSortItems,
userNode,
account,
lastVersion,
Expand Down
6 changes: 4 additions & 2 deletions src/components/pages/earn/CoreChannelNodesPage/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ export const CoreChannelNodesPage = (props: UseCoreChannelNodesPageProps) => {
nodes,
userNodes,
filteredUserNodes,
paginatedFilteredNodes,
paginatedSortedFilteredNodes,
userNodesIssues,
tabs,
selectedTab,
filter,
lastVersion,
loadItemsDisabled,
handleLoadItems,
handleSortItems,
handleTabChange,
handleFilterChange,
} = useCoreChannelNodesPage(props)
Expand Down Expand Up @@ -160,10 +161,11 @@ export const CoreChannelNodesPage = (props: UseCoreChannelNodesPageProps) => {
<CoreChannelNodesTable
{...{
nodes,
filteredNodes: paginatedFilteredNodes,
filteredNodes: paginatedSortedFilteredNodes,
lastVersion,
loadItemsDisabled,
handleLoadItems,
handleSortItems,
}}
/>
)}
Expand Down
6 changes: 4 additions & 2 deletions src/components/pages/earn/StakingPage/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const StakingPage = (props: UseStakingPageProps) => {
account,
accountBalance,
nodes,
paginatedFilteredNodes,
paginatedSortedFilteredNodes,
stakeNodes,
filteredStakeNodes,
tabs,
Expand All @@ -28,6 +28,7 @@ export const StakingPage = (props: UseStakingPageProps) => {
nodesIssues,
loadItemsDisabled,
handleLoadItems,
handleSortItems,
handleTabChange,
handleFilterChange,
handleStake,
Expand Down Expand Up @@ -148,14 +149,15 @@ export const StakingPage = (props: UseStakingPageProps) => {
<StakingNodesTable
{...{
nodes,
filteredNodes: paginatedFilteredNodes,
filteredNodes: paginatedSortedFilteredNodes,
accountBalance,
account,
handleStake,
handleUnstake,
nodesIssues,
loadItemsDisabled,
handleLoadItems,
handleSortItems,
}}
/>
)}
Expand Down
15 changes: 11 additions & 4 deletions src/hooks/common/useComputeResourceNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CRN, NodeLastVersions } from '@/domain/node'
import { useDebounceState, usePaginatedList } from '@aleph-front/core'
import { Account } from 'aleph-sdk-ts/dist/accounts/account'
import { ChangeEvent, useCallback, useMemo, useState } from 'react'
import { UseSortedListReturn, useSortedList } from './useSortedList'

export type UseComputeResourceNodesProps = {
nodes?: CRN[]
Expand All @@ -13,10 +14,11 @@ export type UseComputeResourceNodesReturn = {
accountBalance?: number
nodes?: CRN[]
filteredNodes?: CRN[]
paginatedFilteredNodes?: CRN[]
paginatedSortedFilteredNodes?: CRN[]
filter: string
lastVersion?: NodeLastVersions
loadItemsDisabled: boolean
handleSortItems: UseSortedListReturn<CRN>['handleSortItems']
handleFilterChange: (e: ChangeEvent<HTMLInputElement>) => void
handleLoadItems: () => Promise<void>
}
Expand Down Expand Up @@ -72,12 +74,16 @@ export function useComputeResourceNodes({

// -----------------------------

const { list: sortedFilteredNodes, handleSortItems } = useSortedList({
list: filteredNodes,
})

const {
list: paginatedFilteredNodes,
list: paginatedSortedFilteredNodes,
loadItemsDisabled,
handleLoadItems,
} = usePaginatedList({
list: filteredNodes,
list: sortedFilteredNodes,
})

// -----------------------------
Expand All @@ -87,10 +93,11 @@ export function useComputeResourceNodes({
accountBalance,
nodes,
filteredNodes,
paginatedFilteredNodes,
paginatedSortedFilteredNodes,
filter,
lastVersion,
loadItemsDisabled,
handleSortItems,
handleFilterChange,
handleLoadItems,
}
Expand Down
15 changes: 11 additions & 4 deletions src/hooks/common/useCoreChannelNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CCN, NodeLastVersions } from '@/domain/node'
import { useDebounceState, usePaginatedList } from '@aleph-front/core'
import { Account } from 'aleph-sdk-ts/dist/accounts/account'
import { ChangeEvent, useCallback, useMemo, useState } from 'react'
import { UseSortedListReturn, useSortedList } from './useSortedList'

export type UseCoreChannelNodesProps = {
nodes?: CCN[]
Expand All @@ -13,10 +14,11 @@ export type UseCoreChannelNodesReturn = {
accountBalance?: number
nodes?: CCN[]
filteredNodes?: CCN[]
paginatedFilteredNodes?: CCN[]
paginatedSortedFilteredNodes?: CCN[]
filter: string
lastVersion?: NodeLastVersions
loadItemsDisabled: boolean
handleSortItems: UseSortedListReturn<CCN>['handleSortItems']
handleFilterChange: (e: ChangeEvent<HTMLInputElement>) => void
handleLoadItems: () => Promise<void>
}
Expand Down Expand Up @@ -68,12 +70,16 @@ export function useCoreChannelNodes({

// -----------------------------

const { list: sortedFilteredNodes, handleSortItems } = useSortedList({
list: filteredNodes,
})

const {
list: paginatedFilteredNodes,
list: paginatedSortedFilteredNodes,
loadItemsDisabled,
handleLoadItems,
} = usePaginatedList({
list: filteredNodes,
list: sortedFilteredNodes,
})

// -----------------------------
Expand All @@ -83,10 +89,11 @@ export function useCoreChannelNodes({
accountBalance,
nodes,
filteredNodes,
paginatedFilteredNodes,
paginatedSortedFilteredNodes,
filter,
lastVersion,
loadItemsDisabled,
handleSortItems,
handleFilterChange,
handleLoadItems,
}
Expand Down
34 changes: 34 additions & 0 deletions src/hooks/common/useSortedList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useCallback, useMemo, useState } from 'react'

export type UseSortedListProps<T> = {
list?: T[]
}

export type UseSortedListReturn<T> = {
list?: T[]
handleSortItems: (sortFn: SortFn<T>) => void
}

export type SortFn<T> = (list: T[] | undefined) => T[] | undefined

export function useSortedList<T>({
list,
}: UseSortedListProps<T>): UseSortedListReturn<T> {
const [sortFn, setSortFn] = useState<SortFn<T>>()

const output = useMemo(() => {
if (!list) return list
if (!sortFn) return list
return sortFn(list)
}, [list, sortFn])

const handleSortItems = useCallback(
(sortFn: SortFn<T>) => setSortFn(() => sortFn),
[],
)

return {
list: output,
handleSortItems,
}
}
Loading

0 comments on commit d2ff82c

Please sign in to comment.