forked from jaredpalmer/react-parcel-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (133 loc) · 3.23 KB
/
index.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
import React, { PureComponent } from "react";
import ReactDOM from "react-dom";
import "./index.scss";
const NOOP = () => {};
const REFRESH_PERIOD = 3000;
const apiUrl = "https://blockchain.info/ticker";
const defaultCurrencies = ["USD", "GBP", "EUR"];
const apiProcessor = data => data;
const defaultCurrency = "GBP";
// components
const Price = ({ last, symbol, buy, sell, amount = 1 }) => (
<div>
<p>
<label>Buy price</label> {symbol}
{(sell * amount).toLocaleString()}
</p>{" "}
<p>
<label>Sell price</label> {symbol}
{(sell * amount).toLocaleString()}
</p>
</div>
);
const Currencies = ({
name = "currencies",
currencies,
selectedValue = null,
onChange = NOOP,
}) => (
<select name={name} value={selectedValue} onChange={onChange}>
{currencies.map(currency => (
<option key={currency} value={currency}>
{currency}
</option>
))}
</select>
);
const Input = ({
value = "",
placeholder = "enter amount",
onChange = NOOP,
}) => (
<input
tabIndex="0"
defaultValue={value}
id={name}
name={name}
onChange={onChange}
onKeyDown={onChange}
onKeyPress={onChange}
placeholder={placeholder}
type="text"
/>
);
class App extends PureComponent {
constructor(props) {
super(props);
this.state = Object.assign(
{},
{
amount: 1,
prices: [],
result: false,
currencies: defaultCurrencies,
currency: defaultCurrency,
},
localStorage,
);
}
fetch = () => {
fetch(apiUrl)
.then(response => response.json())
.then(responseData => {
const prices = apiProcessor(responseData);
const currencies = Object.keys(prices);
this.setState({ result: true, prices, currencies });
})
.catch(error => {
console.log("Error fetching data", error);
});
};
componentDidMount() {
this._interval = setInterval(this.fetch, REFRESH_PERIOD);
}
componentWillUnMount() {
clearInterval(this._interval);
}
onChangeCurrency = event => {
const currency = event.currentTarget.value;
localStorage.setItem("currency", currency);
this.setState({
currency,
});
};
onSetAmount = event => {
const amount = event.currentTarget.value;
localStorage.setItem("amount", amount);
this.setState({
amount,
});
};
render() {
const { amount, prices, result, currency, currencies } = this.state;
return (
<section className="app">
<h1>Personal Bitcoin Price Tracker</h1>
<article>
{result ? (
[
<h3>{amount} BTC</h3>,
<Price
key={prices[currency].symbol}
{...prices[currency]}
amount={amount}
/>,
]
) : (
<h2>Loading please wait</h2>
)}
<Currencies
currencies={currencies}
selectedValue={currency}
onChange={this.onChangeCurrency}
/>
<Input name="amount" value={amount} onChange={this.onSetAmount} />
</article>
</section>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
if (module.hot) {
module.hot.accept();
}