File src/components/detail/Detail.js changed (mode: 100644) (index 14ab892..31abd80) |
1 |
1 |
import React from 'react'; |
import React from 'react'; |
|
2 |
|
import { API_URL} from "../../config" |
|
3 |
|
import { handleResponse, renderChangePercent } from "../../helpers"; |
|
4 |
|
import Loading from "../common/Loading"; |
2 |
5 |
import './Detail.css'; |
import './Detail.css'; |
3 |
6 |
|
|
4 |
7 |
|
|
5 |
8 |
class Detail extends React.Component { |
class Detail extends React.Component { |
6 |
9 |
constructor(props) { |
constructor(props) { |
7 |
10 |
super(props); |
super(props); |
8 |
|
this.state = { }; |
|
|
11 |
|
this.state = { |
|
12 |
|
currency: {}, |
|
13 |
|
loading: false, |
|
14 |
|
error: null |
|
15 |
|
}; |
9 |
16 |
} |
} |
|
17 |
|
|
|
18 |
|
componentDidMount(){ |
|
19 |
|
this.setState({ loading: true }); |
|
20 |
|
|
|
21 |
|
const currencyId = this.props.match.params.id; |
|
22 |
|
|
|
23 |
|
fetch(`${API_URL}/cryptocurrencies/${currencyId}`) |
|
24 |
|
.then(handleResponse) |
|
25 |
|
.then((currency) => { |
|
26 |
|
this.setState({ |
|
27 |
|
loading: false, |
|
28 |
|
error: null, |
|
29 |
|
currency |
|
30 |
|
}); |
|
31 |
|
}) |
|
32 |
|
.catch((error) => { |
|
33 |
|
this.setState({ |
|
34 |
|
loading: false, |
|
35 |
|
error: error.errorMessage |
|
36 |
|
}); |
|
37 |
|
}); |
|
38 |
|
} |
|
39 |
|
|
10 |
40 |
render() { |
render() { |
|
41 |
|
const { loading, error, currency} = this.state; |
|
42 |
|
|
|
43 |
|
if (loading) { |
|
44 |
|
return <div className="loading-container"><Loading /></div> |
|
45 |
|
} |
|
46 |
|
|
|
47 |
|
if (error) { |
|
48 |
|
return <div className="error">{error}</div> |
|
49 |
|
} |
|
50 |
|
|
11 |
51 |
return ( |
return ( |
12 |
|
<div>Detail</div> |
|
|
52 |
|
<div className="Detail"> |
|
53 |
|
<h1 className="Detail-heading"> |
|
54 |
|
{currency.name} ({currency.symbol}) |
|
55 |
|
</h1> |
|
56 |
|
<div className="Detail-container"> |
|
57 |
|
<div className="Detail-item"> |
|
58 |
|
Price |
|
59 |
|
<span className="Detail-value"> |
|
60 |
|
${currency.price} |
|
61 |
|
</span> |
|
62 |
|
</div> |
|
63 |
|
<div className="Detail-item"> |
|
64 |
|
Rank |
|
65 |
|
<span className="Detail-value"> |
|
66 |
|
{currency.rank} |
|
67 |
|
</span> |
|
68 |
|
</div> |
|
69 |
|
<div className="Detail-item"> |
|
70 |
|
24h Change |
|
71 |
|
<span className="Detail-value"> |
|
72 |
|
{renderChangePercent(currency.percentChange24h)} |
|
73 |
|
</span> |
|
74 |
|
</div> |
|
75 |
|
<div className="Detail-item"> |
|
76 |
|
<span className="Detail-title"> |
|
77 |
|
Market cap |
|
78 |
|
</span> |
|
79 |
|
<span className="Detail-dollar"> |
|
80 |
|
$ |
|
81 |
|
</span> |
|
82 |
|
{currency.marketCap} |
|
83 |
|
</div> |
|
84 |
|
<div className="Detail-item"> |
|
85 |
|
<span className="Detail-title"> |
|
86 |
|
24H Volume |
|
87 |
|
</span> |
|
88 |
|
<span className="Detail-dollar"> |
|
89 |
|
$ |
|
90 |
|
</span> |
|
91 |
|
{currency.volume24h} |
|
92 |
|
</div> |
|
93 |
|
<div className="Detail-item"> |
|
94 |
|
<span className="Detail-title"> |
|
95 |
|
Total supply |
|
96 |
|
</span> |
|
97 |
|
{currency.totalSupply} |
|
98 |
|
</div> |
|
99 |
|
</div> |
|
100 |
|
</div> |
13 |
101 |
); |
); |
14 |
102 |
} |
} |
15 |
103 |
} |
} |
File src/components/list/List.js changed (mode: 100644) (index bbe3e52..3cdb01e) |
... |
... |
class List extends React.Component { |
47 |
47 |
}); |
}); |
48 |
48 |
} |
} |
49 |
49 |
|
|
50 |
|
renderChangePercent(percent){ |
|
51 |
|
if (percent > 0) { |
|
52 |
|
return <span className="percent-raised">{percent}% ↑</span> |
|
53 |
|
} |
|
54 |
|
else if (percent < 0) { |
|
55 |
|
return <span className="percent-fallen">{percent}% ↓</span> |
|
56 |
|
} |
|
57 |
|
else { |
|
58 |
|
return <span>{percent}%</span> |
|
59 |
|
} |
|
60 |
|
} |
|
|
50 |
|
|
61 |
51 |
|
|
62 |
52 |
handlePaginationClick(direction){ |
handlePaginationClick(direction){ |
63 |
53 |
let nextPage = this.state.page; |
let nextPage = this.state.page; |
|
... |
... |
class List extends React.Component { |
91 |
81 |
<div> |
<div> |
92 |
82 |
<Table |
<Table |
93 |
83 |
currencies={currencies} |
currencies={currencies} |
94 |
|
renderChangePercent={this.renderChangePercent} |
|
95 |
84 |
/> |
/> |
96 |
85 |
<Pagination |
<Pagination |
97 |
86 |
totalPages={totalPages} |
totalPages={totalPages} |
File src/components/list/Table.js changed (mode: 100644) (index 09f8c7a..4e0efaa) |
1 |
1 |
import React from 'react'; |
import React from 'react'; |
2 |
2 |
import PropTypes from 'prop-types'; |
import PropTypes from 'prop-types'; |
3 |
3 |
import { withRouter } from "react-router-dom"; |
import { withRouter } from "react-router-dom"; |
|
4 |
|
import { renderChangePercent } from '../../helpers'; |
4 |
5 |
import './Table.css'; |
import './Table.css'; |
5 |
6 |
|
|
6 |
7 |
|
|
7 |
8 |
const Table = (props) => { |
const Table = (props) => { |
8 |
|
const { currencies, renderChangePercent, history } = props; |
|
|
9 |
|
const { currencies, history } = props; |
9 |
10 |
|
|
10 |
11 |
return( |
return( |
11 |
12 |
<div className="Table-container"> |
<div className="Table-container"> |
|
... |
... |
const Table = (props) => { |
55 |
56 |
|
|
56 |
57 |
Table.propTypes = { |
Table.propTypes = { |
57 |
58 |
currencies: PropTypes.array.isRequired, |
currencies: PropTypes.array.isRequired, |
58 |
|
renderChangePercent: PropTypes.func.isRequired, |
|
59 |
59 |
history: PropTypes.object.isRequired, |
history: PropTypes.object.isRequired, |
60 |
60 |
}; |
}; |
61 |
61 |
|
|
File src/helpers.js changed (mode: 100644) (index 25b2340..0515f7f) |
|
1 |
|
import React from 'react'; |
|
2 |
|
|
1 |
3 |
/** |
/** |
2 |
4 |
|-------------------------------------------------- |
|-------------------------------------------------- |
3 |
5 |
| Fetch error helper |
| Fetch error helper |
|
... |
... |
export const handleResponse = (response) => { |
10 |
12 |
return response.json().then(json => { |
return response.json().then(json => { |
11 |
13 |
return response.ok ? json : Promise.reject(json); |
return response.ok ? json : Promise.reject(json); |
12 |
14 |
}); |
}); |
13 |
|
}; |
|
|
15 |
|
}; |
|
16 |
|
|
|
17 |
|
/** |
|
18 |
|
|-------------------------------------------------- |
|
19 |
|
| Render currency percent |
|
20 |
|
| |
|
21 |
|
| @param {string} percent |
|
22 |
|
|-------------------------------------------------- |
|
23 |
|
*/ |
|
24 |
|
|
|
25 |
|
export const renderChangePercent = (percent) => { |
|
26 |
|
if (percent > 0) { |
|
27 |
|
return <span className="percent-raised">{percent}% ↑</span> |
|
28 |
|
} |
|
29 |
|
else if (percent < 0) { |
|
30 |
|
return <span className="percent-fallen">{percent}% ↓</span> |
|
31 |
|
} |
|
32 |
|
else { |
|
33 |
|
return <span>{percent}%</span> |
|
34 |
|
} |
|
35 |
|
} |