This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add price charts to Converter and Auction screens
Closes #414
- Loading branch information
Pablo Enrici
committed
Nov 21, 2018
1 parent
0d6e0fa
commit 17e5def
Showing
10 changed files
with
445 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, { Component } from 'react' | ||
import { ChartCard } from './common' | ||
import shrinkArray from 'shrink-array' | ||
import { connect } from 'react-redux' | ||
import config from '../config' | ||
import moment from 'moment' | ||
import last from 'shrink-array/last' | ||
|
||
const sevenDaysAgo = () => | ||
moment() | ||
.subtract({ days: 7 }) | ||
.unix() | ||
|
||
class AuctionChartCard extends Component { | ||
state = { | ||
chartStatus: 'pending', | ||
chartData: [] | ||
} | ||
|
||
// eslint-disable-next-line arrow-body-style | ||
retrieveData = () => { | ||
this.setState({ chartStatus: 'pending', chartData: [] }) | ||
|
||
const from = sevenDaysAgo() | ||
const now = moment().unix() | ||
|
||
fetch(`${config.MET_API_URL}/history?from=${from}&to=${now}`) | ||
.then(response => response.json()) | ||
.then(chartData => { | ||
if (!this._isMounted) return | ||
this.setState({ | ||
chartStatus: 'success', | ||
chartData: chartData.map(point => ({ | ||
x: point.timestamp, | ||
y: parseInt(point.currentAuctionPrice, 10) | ||
})) | ||
}) | ||
}) | ||
.catch(() => { | ||
if (!this._isMounted) return | ||
this.setState({ chartStatus: 'failure', chartData: [] }) | ||
}) | ||
} | ||
|
||
componentDidMount() { | ||
this._isMounted = true | ||
this.retrieveData() | ||
} | ||
|
||
componentWillUnmount() { | ||
this._isMounted = false | ||
} | ||
|
||
static getDerivedStateFromProps(props, state) { | ||
const point = { | ||
y: parseInt(props.auction.currentPrice, 10), | ||
x: moment().unix() | ||
} | ||
const from = sevenDaysAgo() | ||
const newChartData = state.chartData.concat(point).filter(p => p.x >= from) | ||
|
||
return { | ||
chartData: | ||
newChartData.length > 500 | ||
? shrinkArray(newChartData, 500, last) | ||
: newChartData | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<ChartCard | ||
chartStatus={this.state.chartStatus} | ||
chartLabel="Auction Price (last 7 days)" | ||
chartData={this.state.chartData} | ||
onRetry={this.retrieveData} | ||
/> | ||
) | ||
} | ||
} | ||
|
||
const mapStateToProps = state => ({ | ||
auction: state.auction.status | ||
}) | ||
|
||
export default connect(mapStateToProps)(AuctionChartCard) |
Oops, something went wrong.