Commit e26e1557 authored by isstuev's avatar isstuev

fix sorting

parent aa3ca7e1
import { Alert, Box, HStack, Show, Button } from '@chakra-ui/react'; import { Alert, Box, HStack, Show, Button } from '@chakra-ui/react';
import React, { useState } from 'react'; import React, { useState, useCallback } from 'react';
import type { Sort } from 'types/client/txs-sort'; import type { Sort } from 'types/client/txs-sort';
...@@ -28,6 +28,31 @@ const TxsContent = ({ ...@@ -28,6 +28,31 @@ const TxsContent = ({
}: Props) => { }: Props) => {
const [ sorting, setSorting ] = useState<Sort>(); const [ sorting, setSorting ] = useState<Sort>();
const sort = useCallback((field: 'val' | 'fee') => () => {
if (field === 'val') {
setSorting((prevVal => {
if (prevVal === 'val-asc') {
return undefined;
}
if (prevVal === 'val-desc') {
return 'val-asc';
}
return 'val-desc';
}));
}
if (field === 'fee') {
setSorting((prevVal => {
if (prevVal === 'fee-asc') {
return undefined;
}
if (prevVal === 'fee-desc') {
return 'fee-asc';
}
return 'fee-desc';
}));
}
}, [ setSorting ]);
const { const {
data, data,
isLoading, isLoading,
...@@ -59,7 +84,7 @@ const TxsContent = ({ ...@@ -59,7 +84,7 @@ const TxsContent = ({
); );
if (!isLoading && txs) { if (!isLoading && txs) {
content = <TxsWithSort txs={ txs } sorting={ sorting } setSorting={ setSorting }/>; content = <TxsWithSort txs={ txs } sorting={ sorting } sort={ sort }/>;
} }
return ( return (
......
import { Box, Show } from '@chakra-ui/react'; import { Box, Show } from '@chakra-ui/react';
import React, { useCallback, useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import type { TransactionsResponse } from 'types/api/transaction'; import type { TransactionsResponse } from 'types/api/transaction';
import type { Sort } from 'types/client/txs-sort'; import type { Sort } from 'types/client/txs-sort';
...@@ -12,41 +12,16 @@ import TxsTable from './TxsTable'; ...@@ -12,41 +12,16 @@ import TxsTable from './TxsTable';
type Props = { type Props = {
txs: TransactionsResponse['items']; txs: TransactionsResponse['items'];
sorting?: Sort; sorting?: Sort;
setSorting: (sorting: Sort | ((val: Sort) => Sort)) => void; sort: (field: 'val' | 'fee') => () => void;
} }
const TxsContent = ({ const TxsWithSort = ({
txs, txs,
sorting, sorting,
setSorting, sort,
}: Props) => { }: Props) => {
const [ sortedTxs, setSortedTxs ] = useState(txs); const [ sortedTxs, setSortedTxs ] = useState(txs);
const sort = useCallback((field: 'val' | 'fee') => () => {
if (field === 'val') {
setSorting((prevVal => {
if (prevVal === 'val-asc') {
return undefined;
}
if (prevVal === 'val-desc') {
return 'val-asc';
}
return 'val-desc';
}));
}
if (field === 'fee') {
setSorting((prevVal => {
if (prevVal === 'fee-asc') {
return undefined;
}
if (prevVal === 'fee-desc') {
return 'fee-asc';
}
return 'fee-desc';
}));
}
}, [ setSorting ]);
useEffect(() => { useEffect(() => {
switch (sorting) { switch (sorting) {
case 'val-desc': case 'val-desc':
...@@ -75,4 +50,4 @@ const TxsContent = ({ ...@@ -75,4 +50,4 @@ const TxsContent = ({
}; };
export default TxsContent; export default TxsWithSort;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment