import React, {Component} from 'react'
import moment from 'common/moment'
import {cancelable_timeout} from 'common/cancelable'
import auto_bind from 'common/auto_bind'

export default class TimeAgo extends Component {
  constructor(props) {
    super(props)
    auto_bind(this)
  }

  componentDidMount() {
    this.update()
  }

  update() {
    this.forceUpdate()
    this.cancelable_update = cancelable_timeout(this.update, 60 * 1000)
  }

  componentWillUnmount() {
    this.cancelable_update.cancel()
  }

  render() {
    return (
      <span>{moment(this.props.children).fromNow()}</span>
    )
  }
}
