import React , {Component} from 'react';
import './styles/styles.scss';
import {toTimeSeconds} from 'helpers/helpers';

let time = +new Date();

export default class CurrentTime extends Component{
    constructor(props){
        super(props);
        this.state = {
            time: time,
            timeText: toTimeSeconds(time / 1000)
        }
    }

    componentDidMount(){
        this.timer = setInterval(() => {
            let time = +new Date();
            if(this.refs.time){
                this.setState({
                    time: time,
                    timeText: toTimeSeconds(time / 1000)
                })
            }
        }, 1000);
    }

    componentWillUnmount(){
        clearInterval(this.timer);
    }

    render(){
        return(
            <div ref="time" className='current-time'>
                {this.state.timeText}
            </div>
        )
    }
}
