2022-04-06 17:48:05 +00:00
|
|
|
import { useRef, useCallback, useMemo } from 'react';
|
|
|
|
import { produce } from 'immer';
|
|
|
|
import merge from 'lodash/merge';
|
|
|
|
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import { useDataProvider, addSummaryRows } from '@vegaprotocol/react-helpers';
|
|
|
|
import type { AccountSubscribe_accounts } from './__generated__/AccountSubscribe';
|
|
|
|
import type { Accounts_party_accounts } from './__generated__/Accounts';
|
|
|
|
|
|
|
|
import type { AgGridReact } from 'ag-grid-react';
|
|
|
|
import {
|
|
|
|
AccountsTable,
|
|
|
|
getGroupId,
|
|
|
|
getGroupSummaryRow,
|
|
|
|
} from './accounts-table';
|
|
|
|
import { accountsDataProvider, getId } from './accounts-data-provider';
|
|
|
|
|
|
|
|
interface AccountsManagerProps {
|
|
|
|
partyId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const AccountsManager = ({ partyId }: AccountsManagerProps) => {
|
|
|
|
const gridRef = useRef<AgGridReact | null>(null);
|
|
|
|
const variables = useMemo(() => ({ partyId }), [partyId]);
|
|
|
|
const update = useCallback(
|
2022-07-05 13:33:50 +00:00
|
|
|
({ delta }: { delta: AccountSubscribe_accounts }) => {
|
2022-04-06 17:48:05 +00:00
|
|
|
const update: Accounts_party_accounts[] = [];
|
|
|
|
const add: Accounts_party_accounts[] = [];
|
2022-09-02 20:53:00 +00:00
|
|
|
if (!gridRef.current?.api) {
|
2022-04-06 17:48:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const rowNode = gridRef.current.api.getRowNode(getId(delta));
|
|
|
|
if (rowNode) {
|
|
|
|
const updatedData = produce<Accounts_party_accounts>(
|
|
|
|
rowNode.data,
|
|
|
|
(draft: Accounts_party_accounts) => {
|
|
|
|
merge(draft, delta);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (updatedData !== rowNode.data) {
|
|
|
|
update.push(updatedData);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
add.push(delta);
|
|
|
|
}
|
|
|
|
if (update.length || add.length) {
|
|
|
|
gridRef.current.api.applyTransactionAsync({
|
|
|
|
update,
|
|
|
|
add,
|
|
|
|
addIndex: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (add.length) {
|
|
|
|
addSummaryRows(
|
|
|
|
gridRef.current.api,
|
|
|
|
gridRef.current.columnApi,
|
|
|
|
getGroupId,
|
|
|
|
getGroupSummaryRow
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[gridRef]
|
|
|
|
);
|
|
|
|
const { data, error, loading } = useDataProvider<
|
|
|
|
Accounts_party_accounts[],
|
|
|
|
AccountSubscribe_accounts
|
2022-07-05 13:33:50 +00:00
|
|
|
>({ dataProvider: accountsDataProvider, update, variables });
|
2022-04-06 17:48:05 +00:00
|
|
|
return (
|
|
|
|
<AsyncRenderer loading={loading} error={error} data={data}>
|
2022-04-26 15:26:28 +00:00
|
|
|
<AccountsTable ref={gridRef} data={data} />
|
2022-04-06 17:48:05 +00:00
|
|
|
</AsyncRenderer>
|
|
|
|
);
|
|
|
|
};
|