feat(trading): store current tab selection

This commit is contained in:
Bartłomiej Głownia
2023-04-27 17:49:52 +02:00
parent 7fcd9e9bf3
commit 584e23e5dc
4 changed files with 39 additions and 54 deletions
@@ -7,15 +7,14 @@ describe('order data provider', () => {
const data = [
{
node: {
id: '1',
updatedAt: new Date('2022-01-31').toISOString(),
id: '2',
createdAt: new Date('2022-01-29').toISOString(),
},
},
{
node: {
id: '2',
createdAt: new Date('2022-01-30').toISOString(),
id: '1',
createdAt: new Date('2022-01-28').toISOString(),
},
},
] as Edge<OrderFieldsFragment>[];
@@ -24,47 +23,51 @@ describe('order data provider', () => {
// this one should be dropped because id don't exits and it's older than newest
{
id: '0',
createdAt: new Date('2022-01-30').toISOString(),
createdAt: new Date('2022-01-27').toISOString(),
},
// this one should be dropped because newer below
{
id: '1',
updatedAt: new Date('2022-02-01').toISOString(),
createdAt: new Date('2022-01-29').toISOString(),
createdAt: new Date('2022-01-28').toISOString(),
},
{
id: '1',
updatedAt: new Date('2022-02-02').toISOString(),
createdAt: new Date('2022-01-29').toISOString(),
updatedAt: new Date('2022-02-04').toISOString(),
createdAt: new Date('2022-01-28').toISOString(),
},
// this should be added
{
id: '4',
createdAt: new Date('2022-02-04').toISOString(),
},
// this should be move to top
{
id: '2',
updatedAt: new Date('2022-02-03').toISOString(),
createdAt: new Date('2022-01-29').toISOString(),
updatedAt: new Date('2022-02-04').toISOString(),
createdAt: new Date('2022-01-30').toISOString(),
},
// this should be added
{
id: '5',
createdAt: new Date('2022-02-05').toISOString(),
},
] as OrderUpdateFieldsFragment[];
const updatedData = update(data, delta, () => null, { partyId: '0x123' });
expect(
updatedData?.findIndex((edge) => edge.node.id === delta[0].id)
).toEqual(-1);
expect(updatedData && updatedData[2].node.id).toEqual(delta[2].id);
expect(updatedData && updatedData[2].node.updatedAt).toEqual(
expect(updatedData && updatedData[3].node.id).toEqual(delta[2].id);
expect(updatedData && updatedData[3].node.updatedAt).toEqual(
delta[2].updatedAt
);
expect(updatedData && updatedData[0].node.id).toEqual(delta[3].id);
expect(updatedData && updatedData[1].node.id).toEqual(delta[4].id);
expect(updatedData && updatedData[1].node.updatedAt).toEqual(
expect(updatedData && updatedData[0].node.id).toEqual(delta[5].id);
expect(updatedData && updatedData[1].node.id).toEqual(delta[3].id);
expect(updatedData && updatedData[2].node.id).toEqual(delta[4].id);
expect(updatedData && updatedData[2].node.updatedAt).toEqual(
delta[4].updatedAt
);
expect(update([], delta, () => null, { partyId: '0x123' })?.length).toEqual(
4
5
);
});
it('add only data matching date range filter', () => {
@@ -72,7 +75,6 @@ describe('order data provider', () => {
{
node: {
id: '1',
updatedAt: new Date('2022-01-31').toISOString(),
createdAt: new Date('2022-01-29').toISOString(),
},
},
@@ -90,12 +92,6 @@ describe('order data provider', () => {
id: '0',
createdAt: new Date('2022-02-02').toISOString(),
},
// this one should be removed because it does not match date range
{
id: '1',
updatedAt: new Date('2022-02-02').toISOString(),
createdAt: new Date('2022-01-29').toISOString(),
},
// this one should be updated
{
id: '2',
@@ -118,16 +114,13 @@ describe('order data provider', () => {
expect(
updatedData?.findIndex((edge) => edge.node.id === delta[0].id)
).toEqual(-1);
expect(
updatedData?.findIndex((edge) => edge.node.id === delta[1].id)
).toEqual(-1);
expect(updatedData && updatedData[0].node.id).toEqual(delta[2].id);
expect(updatedData && updatedData[0].node.updatedAt).toEqual(
delta[2].updatedAt
);
expect(updatedData && updatedData[1].node.id).toEqual(delta[3].id);
expect(updatedData && updatedData[1].node.updatedAt).toEqual(
delta[3].updatedAt
expect(updatedData && updatedData[2].node.id).toEqual(delta[1].id);
expect(updatedData && updatedData[2].node.updatedAt).toEqual(
delta[1].updatedAt
);
});
});
@@ -67,19 +67,13 @@ const orderMatchFilters = (
}
if (
variables?.filter?.dateRange?.start &&
!(
(order.updatedAt || order.createdAt) &&
variables.filter.dateRange.start < (order.updatedAt || order.createdAt)
)
!(order.createdAt && variables.filter.dateRange.start < order.createdAt)
) {
return false;
}
if (
variables?.filter?.dateRange?.end &&
!(
(order.updatedAt || order.createdAt) &&
variables.filter.dateRange.end > (order.updatedAt || order.createdAt)
)
!(order.createdAt && variables.filter.dateRange.end > order.createdAt)
) {
return false;
}
@@ -129,28 +123,25 @@ export const update = (
if (!data) {
return data;
}
return produce(data, (draft) => {
// A single update can contain the same order with multiple updates, so we need to find
// the latest version of the order and only update using that
const incoming = uniqBy(
// A single update can contain the same order with multiple updates, so we need to find
// the latest version of the order and only update using that
const incoming = orderBy(
uniqBy(
orderBy(delta, (order) => order.updatedAt || order.createdAt, 'desc'),
'id'
);
),
'createdAt'
);
return produce(data, (draft) => {
// Add or update incoming orders
incoming.reverse().forEach((node) => {
incoming.forEach((node) => {
const index = draft.findIndex((edge) => edge.node.id === node.id);
const newer =
draft.length === 0 ||
(node.updatedAt || node.createdAt) >=
(draft[0].node.updatedAt || draft[0].node.createdAt);
draft.length === 0 || node.createdAt >= draft[0].node.createdAt;
const doesFilterPass = !variables || orderMatchFilters(node, variables);
if (index !== -1) {
if (doesFilterPass) {
Object.assign(draft[index].node, node);
if (newer) {
draft.unshift(...draft.splice(index, 1));
}
} else {
draft.splice(index, 1);
}
@@ -219,7 +210,7 @@ export const allOrdersProvider = makeDerivedDataProvider<
!parts[0].isUpdate &&
subscriptions &&
subscriptions[0].load &&
orders?.length < 5000
orders?.length < 50000
) {
subscriptions[0].load();
}
@@ -20,6 +20,7 @@ import type { Order } from '../order-data-provider';
import { OrderStatus } from '@vegaprotocol/types';
export enum Filter {
// make filter value always truthy
'Open' = 1,
'Closed',
'Rejected',
@@ -238,6 +238,7 @@ export const OrderListTable = memo(
/>
<AgGridColumn
field="createdAt"
filter={DateRangeFilter}
cellRenderer={({
data,
value,
@@ -252,7 +253,6 @@ export const OrderListTable = memo(
/>
<AgGridColumn
field="updatedAt"
filter={DateRangeFilter}
cellRenderer={({
data,
value,