1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-29 15:34:19 +01:00
ipfs-boards/webapp/components/clock.jsx

38 lines
1021 B
React
Raw Normal View History

var React = require('react')
var Icon = require('icon.jsx')
module.exports = React.createClass({
getInitialState: function () {
2015-11-23 18:55:28 +01:00
return { moment: false, text: '...' }
},
componentDidMount: function () {
require.ensure(['moment'], _ => {
if (this.isMounted()) {
2015-11-23 18:55:28 +01:00
var moment = require('moment')
this.setState({
moment: moment,
interval: setInterval(this.upDate, 60 * 1000),
2015-11-23 18:55:28 +01:00
text: moment.unix(this.props.date).fromNow()
})
}
})
},
upDate: function () {
if (this.isMounted()) {
2015-11-23 18:55:28 +01:00
this.setState({ text: this.state.moment.unix(this.props.date).fromNow() })
} else {
2015-11-23 18:55:28 +01:00
clearInterval(this.state.interval)
}
2015-11-23 18:55:28 +01:00
},
getDate: function () {
if (this.state.moment) {
2015-11-23 18:55:28 +01:00
return this.state.text
} else {
2015-11-23 18:55:28 +01:00
return <Icon name="refresh" className="fa-spin" />
}
},
render: function () {
2015-11-26 19:04:47 +01:00
return <div className="inline"><Icon name="clock-o" className={this.props.className} /> {this.getDate()}</div>
}
})