summaryrefslogtreecommitdiff
path: root/frontend/site/projects/museum/views/counter.js
blob: 9322bfa5c33cec567bd6794a1eaf97d3ff2b7417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, { Component, createRef } from 'react'

import { commatize } from "app/utils"

import "./counter.css"

export default class Counter extends Component {
  constructor(props) {
    super(props)
    this.ref = createRef()
    this.update = this.update.bind(this)
  }

  componentDidMount() {
    this.update()
  }

  componentWillUnmount() {
    clearTimeout(this.timeout)
  }

  update() {
    this.timeout = setTimeout(this.update, 250)
    const population = getPopulation()
    if (this.ref.current) {
      this.ref.current.innerHTML = commatize(population)
    }
  }

  render() {
    return (
      <div className="counter">
        <div className="countValue" ref={this.ref} />
        <div className="description">
          CURRENT GLOBAL POPULATION
        </div>
      </div>
    )
  }
}

const aprilFirst = new Date(2021, 3, 1)

function getPopulation() {
  const popAprilFirst = 7855013899
  const popChangePerDay = 219251.934066
  const secondsDelta = (new Date() - aprilFirst) / 86400000  // april = month 3
  const population = popAprilFirst + secondsDelta * popChangePerDay
  return Math.round(population)
}