aboutsummaryrefslogtreecommitdiff
path: root/src/client/react/components/container/WeekSelector.js
blob: d5ba752bee66eddbe8dc7fdcc1c2ec3511492370 (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
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import queryString from 'query-string';
import { withRouter } from 'react-router-dom';

import ArrowBackIcon from 'react-icons/lib/md/arrow-back';
import ArrowForwardIcon from 'react-icons/lib/md/arrow-forward';

import purifyWeek from '../../lib/purifyWeek';

const WeekSelector = ({ urlWeek, location, history }) => {
  const updateWeek = (change) => {
    const newWeek = purifyWeek(urlWeek + change);
    const isCurrentWeek = moment().week() === newWeek;

    const query = queryString.stringify({
      week: isCurrentWeek ? undefined : newWeek,
    });
    history.push(`${location.pathname}?${query}`);
  };

  return (
    <div className="week-selector">
      <button onClick={() => updateWeek(-1)}><ArrowBackIcon /></button>
      <div className="text">Week {urlWeek}</div>
      <button onClick={() => updateWeek(+1)}><ArrowForwardIcon /></button>
    </div>
  );
};

WeekSelector.propTypes = {
  urlWeek: PropTypes.number.isRequired,
  history: PropTypes.shape({
    push: PropTypes.func.isRequired,
  }).isRequired,
  location: PropTypes.shape({
    pathname: PropTypes.string.isRequired,
  }).isRequired,
};

export default withRouter(WeekSelector);