[#128] Use native ag-grid sort instead on useMemo (#178)

* [#128] Use native ag-grid sort instead on useMemo

* [#128] Add secondary sort on positions table - order by instrument name, market id
This commit is contained in:
Bartłomiej Głownia 2022-03-31 18:14:16 +02:00 committed by GitHub
parent ed6c22d7d0
commit 8424dc718d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 20 deletions

View File

@ -68,7 +68,9 @@ test('Render correct columns', async () => {
const headers = screen.getAllByRole('columnheader'); const headers = screen.getAllByRole('columnheader');
expect(headers).toHaveLength(5); expect(headers).toHaveLength(5);
expect(headers.map((h) => h.textContent?.trim())).toEqual([ expect(
headers.map((h) => h.querySelector('[ref="eText"]')?.textContent?.trim())
).toEqual([
'Market', 'Market',
'Amount', 'Amount',
'Average Entry Price', 'Average Entry Price',

View File

@ -10,7 +10,6 @@ import {
import { AgGridDynamic as AgGrid } from '@vegaprotocol/ui-toolkit'; import { AgGridDynamic as AgGrid } from '@vegaprotocol/ui-toolkit';
import { AgGridColumn } from 'ag-grid-react'; import { AgGridColumn } from 'ag-grid-react';
import type { AgGridReact } from 'ag-grid-react'; import type { AgGridReact } from 'ag-grid-react';
import compact from 'lodash/compact';
import type { Positions_party_positions } from './__generated__/Positions'; import type { Positions_party_positions } from './__generated__/Positions';
import { MarketTradingMode } from '@vegaprotocol/types'; import { MarketTradingMode } from '@vegaprotocol/types';
@ -21,52 +20,70 @@ interface PositionsTableProps {
export const getRowNodeId = (data: { market: { id: string } }) => export const getRowNodeId = (data: { market: { id: string } }) =>
data.market.id; data.market.id;
const sortByName = ( const alphanumericComparator = (a: string, b: string, isInverted: boolean) => {
a: Positions_party_positions, if (a < b) {
b: Positions_party_positions return isInverted ? 1 : -1;
) => {
if (
a.market.tradableInstrument.instrument.name <
b.market.tradableInstrument.instrument.name
) {
return -1;
} }
if ( if (a > b) {
a.market.tradableInstrument.instrument.name > return isInverted ? -1 : 1;
b.market.tradableInstrument.instrument.name
) {
return 1;
} }
return 0; return 0;
}; };
const comparator = (
valueA: string,
valueB: string,
nodeA: { data: Positions_party_positions },
nodeB: { data: Positions_party_positions },
isInverted: boolean
) =>
alphanumericComparator(
nodeA.data.market.tradableInstrument.instrument.name,
nodeB.data.market.tradableInstrument.instrument.name,
isInverted
) ||
alphanumericComparator(
nodeA.data.market.id,
nodeB.data.market.id,
isInverted
);
interface PositionsTableValueFormatterParams extends ValueFormatterParams { interface PositionsTableValueFormatterParams extends ValueFormatterParams {
data: Positions_party_positions; data: Positions_party_positions;
} }
export const PositionsTable = forwardRef<AgGridReact, PositionsTableProps>( export const PositionsTable = forwardRef<AgGridReact, PositionsTableProps>(
({ data }, ref) => { ({ data }, ref) => {
const sortedData = useMemo(() => {
return compact(data).sort(sortByName);
}, [data]);
return ( return (
<AgGrid <AgGrid
style={{ width: '100%', height: '100%' }} style={{ width: '100%', height: '100%' }}
overlayNoRowsTemplate="No positions" overlayNoRowsTemplate="No positions"
rowData={sortedData} rowData={data}
getRowNodeId={getRowNodeId} getRowNodeId={getRowNodeId}
ref={ref} ref={ref}
defaultColDef={{ defaultColDef={{
flex: 1, flex: 1,
resizable: true, resizable: true,
}} }}
onGridReady={(event) => {
event.columnApi.applyColumnState({
state: [
{
colId: 'market.tradableInstrument.instrument.code',
sort: 'asc',
},
],
});
}}
components={{ PriceCell }} components={{ PriceCell }}
> >
<AgGridColumn <AgGridColumn
headerName={t('Market')} headerName={t('Market')}
field="market.tradableInstrument.instrument.code" field="market.tradableInstrument.instrument.code"
comparator={comparator}
sortable
/> />
<AgGridColumn <AgGridColumn
headerName={t('Amount')} headerName={t('Amount')}