generated from hasibhassan/aws-one-touch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiveChartPage.js
130 lines (108 loc) · 3.22 KB
/
LiveChartPage.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
import React, { useState, useEffect, useRef } from 'react'
import LiveChart from '@sections/LiveChart/LiveChart'
import { liveChartFormatter } from '@utils/liveChartFormatter'
import styles from '@sections/LiveChart/LiveChart.module.css'
import Select from 'react-select'
import Head from 'next/head'
export default function LiveChartPage() {
const [currencies, setCurrencies] = useState([])
const [pair, setPair] = useState('')
const [price, setPrice] = useState('0.00')
const [pastData, setPastData] = useState({ label: '', datasets: '' })
const ws = useRef(null)
let first = useRef(false)
const url = 'https://api.pro.coinbase.com'
useEffect(() => {
ws.current = new WebSocket('wss://ws-feed.pro.coinbase.com')
let pairs = []
const fetchCurrencyPairs = async () => {
await fetch(url + '/products')
.then((res) => res.json())
.then((data) => {
pairs = data
})
let filtered = pairs.filter((pair) => {
if (pair.quote_currency === 'USD') {
return pair
}
})
filtered = filtered.sort((a, b) => {
if (a.base_currency < b.base_currency) {
return -1
}
if (a.base_currency > b.base_currency) {
return 1
}
return 0
})
let options = filtered.map((currency) => {
return { label: currency.base_currency, value: currency.id }
})
setCurrencies(options)
first.current = true
}
fetchCurrencyPairs()
}, [])
useEffect(() => {
if (!first.current) {
return
}
let msg = {
type: 'subscribe',
product_ids: [pair],
channels: ['ticker'],
}
let jsonMsg = JSON.stringify(msg)
ws.current.send(jsonMsg)
const fetchChartData = async () => {
let chartDataURL = `${url}/products/${pair}/candles?granularity=86400`
let dataArr = []
await fetch(chartDataURL)
.then((res) => res.json())
.then((data) => (dataArr = data))
let formattedData = liveChartFormatter(dataArr)
setPastData(formattedData)
console.log(formattedData)
}
// const addNewPriceToChart = async (newPrice) => {
// setPastData(() => {
// pastData?.datasets[0]?.data[299] = newPrice
// })
// }
fetchChartData()
ws.current.onmessage = (e) => {
let data = JSON.parse(e.data)
if (data.type !== 'ticker') {
return
}
if (data.product_id === pair) {
setPrice(data.price)
// if (pastData.datasets) {
// setPastData((oldData) => oldData.datasets[0].data.push(data.price))
// setPastData((oldData) => oldData.datasets[0].data.unshift())
// }
}
}
}, [pair])
const handleSelect = ({ value }) => {
let unsubMsg = {
type: 'unsubscribe',
product_ids: [pair],
channels: ['ticker'],
}
let unsub = JSON.stringify(unsubMsg)
ws.current.send(unsub)
setPair(value)
}
return (
<div className={styles.dashboard}>
<Head>
<title>Live Price</title>
</Head>
<div className={styles.selectContainer}>
<Select options={currencies} onChange={handleSelect} />
</div>
<LiveChart price={price} data={pastData} />
</div>
)
}