generated from finos-labs/project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathDatatable.tsx
More file actions
99 lines (89 loc) · 3.69 KB
/
Datatable.tsx
File metadata and controls
99 lines (89 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React, { useCallback, useEffect, useState } from 'react';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid-community/styles/ag-grid.css';
import 'ag-grid-community/styles/ag-theme-alpine.css';
import { SelectChangeEvent } from '@mui/material';
import { socket } from '../socket';
import { GetPositions, GetTrades } from '../hooks';
import { CreateAccount, CreateAccountUser, CreateTradeButton } from '../ActionButtons';
import { ColDef } from 'ag-grid-community';
import { PositionData, TradeData } from './types';
import { AccountsDropdown } from '../AccountsDropdown';
const PUBLISH='publish';
const SUBSCRIBE='subscribe';
const UNSUBSCRIBE='unsubscribe';
export const Datatable = () => {
const [tradeRowData, setTradeRowData] = useState<TradeData[]>([]);
const [tradeColumnDefs, setTradeColumnDefs] = useState<ColDef[]>([]);
const [positionRowData, setPositionRowData] = useState<PositionData[]>([]);
const [positionColumnDefs, setPositionColumnDefs] = useState<ColDef[]>([]);
const [selectedId, setSelectedId] = useState<number>(0);
const [currentAccount, setCurrentAccount] = useState<string>('');
const positionData = GetPositions(selectedId);
const tradeData = GetTrades(selectedId);
const handleChange = useCallback((event:SelectChangeEvent<any>) => {
socket.off(PUBLISH);
if (selectedId !== 0){
socket.emit(UNSUBSCRIBE,`/accounts/${selectedId}/trades`);
socket.emit(UNSUBSCRIBE,`/accounts/${selectedId}/positions`);
}
setSelectedId(event.target.value);
setCurrentAccount(event.target.value);
socket.emit(SUBSCRIBE,`/accounts/${event.target.value}/trades`);
socket.emit(SUBSCRIBE,`/accounts/${event.target.value}/positions`);
socket.on(PUBLISH, (data:any) => {
if (data.topic === `/accounts/${event.target.value}/trades`) {
console.log("INCOMING TRADE DATA: ", data);
setTradeRowData((current: TradeData[]) => [...current, data.payload]);
}
if (data.topic === `/accounts/${event.target.value}/positions`) {
console.log("INCOMING POSITION DATA: ", data);
setPositionRowData((current: PositionData[]) => {
const existingIndex = current.findIndex(
(pos: PositionData) => pos.security === data.payload.security
);
if (existingIndex >= 0) {
const updated = [...current];
updated[existingIndex] = data.payload;
return updated;
}
return [...current, data.payload];
});
}
});
}, [selectedId])
useEffect(() => {
const positionKeys = ['security','quantity','updated'];
const tradeKeys = ['security','quantity','side','state','updated'];
setPositionRowData(positionData);
setTradeRowData(tradeData);
setPositionColumnDefs([])
setTradeColumnDefs([]);
positionKeys.forEach((key:string) => setPositionColumnDefs((current: ColDef<PositionData>[]) => [...current, {field: key}]));
tradeKeys.forEach((key:string) => setTradeColumnDefs((current: ColDef<TradeData>[]) => [...current, {field: key}]));
}, [positionData, tradeData, selectedId, currentAccount])
return (
<>
<div className="accounts-dropdown">
<AccountsDropdown currentAccount={currentAccount} handleChange={handleChange} />
</div>
<div className="action-buttons" style={{width: "100%", display: "flex"}}>
<CreateTradeButton accountId={selectedId} />
<CreateAccount />
<CreateAccountUser accountId={selectedId} />
</div>
<div className="ag-theme-alpine" style={{height: "80vh", width: "50%", float: "left"}}>
<AgGridReact
rowData={tradeRowData}
columnDefs={tradeColumnDefs}>
</AgGridReact>
</div>
<div className="ag-theme-alpine" style={{height: "80vh", width: "50%", float: "right"}}>
<AgGridReact
rowData={positionRowData}
columnDefs={positionColumnDefs}>
</AgGridReact>
</div>
</>
);
}