-
Notifications
You must be signed in to change notification settings - Fork 4
/
PriceTableStream.js
190 lines (180 loc) · 5.31 KB
/
PriceTableStream.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import numeral from 'numeral';
import { Box, Text } from 'grommet';
import { PagingTable } from 'grommet-controls';
import { longDate } from 'grommet-controls/utils';
import { subscribeLastPrices, unSubscribeLastPrices } from '../../sockets/price_stream/IEXPrices';
import { valueToColor } from '../utils/formatters';
export default class PriceTableStream extends Component {
constructor(props, context) {
super(props, context);
this.state = { priceStream: null };
}
onPriceStream = (data) => {
if (!this.unMounted) {
const lastData = this.state.priceStream || data;
this.setState({ priceStream: data, lastData });
}
};
componentDidMount() {
const { symbol } = this.props;
this.unMounted = false;
subscribeLastPrices({
symbol, callback: this.onPriceStream,
});
}
componentWillReceiveProps(nextProps) {
const { symbol } = nextProps;
if (symbol !== this.props.symbol) {
unSubscribeLastPrices({ symbol, callback: this.onPriceStream });
subscribeLastPrices({ symbol, callback: this.onPriceStream });
}
}
componentWillUnmount() {
const { symbol } = this.props;
this.unMounted = true;
unSubscribeLastPrices({
symbol, callback: this.onPriceStream,
});
}
render() {
const { priceStream, lastData } = this.state;
let priceTable;
if (priceStream) {
const rows = [
{
label: 'Last sale ',
value: (
<Box direction='row' justify='end'>
<Text>
<strong>
{longDate(priceStream.lastSaleTime)}
</strong>
</Text>
</Box>
),
},
{
label: 'Prev sale ',
value: (
<Box direction='row' justify='end'>
<Text>
<strong>
{numeral(lastData.lastSalePrice).format('$0,0.00')}
</strong>
</Text>
{' / '}
<Text>
<strong>{numeral(lastData.lastSaleSize).format('0,0.00')}</strong>
</Text>
</Box>
),
},
{
label: 'Ask price',
value: (
<Box direction='row' justify='end'>
<Text color={valueToColor(priceStream.askPrice - lastData.askPrice)}>
<strong>
{numeral(priceStream.askPrice).format('$0,0.00')}
</strong>
</Text>
</Box>
),
},
{
label: 'Ask size',
value: (
<Box direction='row' justify='end'>
<Text color={valueToColor(priceStream.askSize - lastData.askSize)}>
<strong>
{numeral(priceStream.askSize).format('0,0.00')}
</strong>
</Text>
</Box>
),
},
{
label: 'Bid price',
value: (
<Box direction='row' justify='end'>
<Text color={valueToColor(priceStream.bidPrice - lastData.bidPrice)}>
<strong>
{numeral(priceStream.bidPrice).format('$0,0.00')}
</strong>
</Text>
</Box>
),
},
{
label: 'Bid size',
value: (
<Box direction='row' justify='end'>
<Text color={valueToColor(priceStream.bidSize - lastData.bidSize)}>
<strong>
{numeral(priceStream.bidSize).format('0,0.00')}
</strong>
</Text>
</Box>
),
},
{
label: 'Volume',
value: (
<Box direction='row' justify='end'>
<Text>
<strong>
{numeral(priceStream.volume).format('a')}
</strong>
</Text>
</Box>
),
},
{
label: 'Market %',
value: (
<Box direction='row' justify='end'>
<Text>
<strong>
{numeral(priceStream.marketPercent / 100).format('0.00%')}
</strong>
</Text>
</Box>
),
},
];
priceTable = (
<Box align='center'>
<Box margin={{ vertical: 'medium' }} direction='row' gap='xsmall' align='baseline'>
<Text weight='bold' size='xlarge' color={valueToColor(priceStream.lastSalePrice - lastData.lastSalePrice)} >
{numeral(priceStream.lastSalePrice).format('$0,0.00')}
</Text>
<Text weight='bold' color={valueToColor(priceStream.lastSaleSize - lastData.lastSaleSize)}>
<strong>{numeral(priceStream.lastSaleSize).format('0,0.00')}</strong>
</Text>
</Box>
<Box fill={true}>
<PagingTable
sortable={false}
resizable={false}
data={rows}
columns={[
{ accessor: 'label', maxWidth: 100 },
{ accessor: 'value' },
]}
/>
</Box>
</Box>
);
}
return (
<Box fill={true}>
{priceTable}
</Box>
);
}
}
PriceTableStream.propTypes = {
symbol: PropTypes.string.isRequired,
};