Skip to content

Commit

Permalink
feat(crypto): auto-refresh after 30sec
Browse files Browse the repository at this point in the history
  • Loading branch information
balthazar committed Mar 18, 2017
1 parent 50d1c89 commit 1b2a880
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Widget extends Component {
)}

{(edit || !loading && loaded) && (
<W onSave={::this.configureWidget} edit={edit && editMode} data={widget} />
<W id={id} onSave={::this.configureWidget} edit={edit && editMode} data={widget} />
)}

</div>
Expand Down
25 changes: 18 additions & 7 deletions src/components/widgets/Crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React, { Component } from 'react'
import { connect } from 'react-redux'

import { toggleLock } from 'actions/mode'
import { fetchWidget } from 'actions/widgets'
import TextInput from 'components/TextInput'

@connect(null, { toggleLock })
@connect(null, { toggleLock, fetchWidget })
class Crypto extends Component {

state = { now: new Date() }
Expand All @@ -13,16 +14,25 @@ class Crypto extends Component {
this._int = setInterval(() => this.setState({ now: new Date() }), 1E3)
}

componentWillUpdate () {
const { fetchWidget, id } = this.props
if (this.getDiff() > 30) {
fetchWidget(id)
}
}

componentWillUnmount () {
clearInterval(this._int)
}

getDiff = date => {
getDiff = () => {
const { now } = this.state
const { values: { timestamp } } = this.props.data
const date = new Date(timestamp)

const diff = Math.abs(now.getTime() - date.getTime())
const secs = Math.ceil(diff / 1000)
if (secs === 1) { return `${secs} sec` }
return `${secs} secs`
return secs
}

savePair = e => {
Expand All @@ -36,8 +46,9 @@ class Crypto extends Component {

render () {
const { edit } = this.props
const { values, config: { pair } } = this.props.data
const timestamp = new Date(values.timestamp)
const { config: { pair }, values } = this.props.data
const diff = this.getDiff()
const text = diff === 1 ? '1 sec' : `${diff} secs`

return (
<div className='w-crypto'>
Expand All @@ -54,7 +65,7 @@ class Crypto extends Component {
<span>{pair}</span>
</div>
<div className='crypto--update'>
{`updated ${this.getDiff(timestamp)} ago`}
{`updated ${text} ago`}
</div>
<div className='crypto--values'>
<span>
Expand Down

0 comments on commit 1b2a880

Please sign in to comment.