summaryrefslogtreecommitdiff
path: root/src/components/group-heading/index.js
blob: 2ba89a26b05069a9b8672696be27089bb07afa5a (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
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react';
import moment from 'moment';
import PropTypes from 'prop-types';

import './styles.css';

class GroupHeading extends React.Component {
  getTitle() {
    const startMoment = moment(this.props.start);
    const currentMoment = moment();
    const yesterdayMoment = moment().subtract(1, 'day');

    // For "day" jumps return the day name
    if (this.props.dateJump === 'day') {
      if (startMoment.isSame(currentMoment, 'day')) {
        return 'Today';
      } else if (startMoment.isSame(yesterdayMoment, 'day')) {
        return 'Yesterday';
      }

      return startMoment.format('dddd');
    }

    // Return the relative date
    const formattedDate = startMoment.fromNow();
    if (formattedDate === 'a year ago') {
      return 'Last Year';
    }

    return formattedDate;
  }

  getSubtitle() {
    const startMoment = moment(this.props.start);
    const endMoment = moment(this.props.end);

    if (startMoment.isSame(endMoment, 'day') || this.props.dateJump === 'day') {
      return startMoment.format('MMMM D, YYYY');
    }

    return startMoment.format('MMMM D, YYYY') + ' – ' + endMoment.format('MMMM D, YYYY');
  }

  render() {
    return (
      <div className="group-heading">
        <h4>
          <span className="text-capitalizes">{ this.getTitle() }</span>
          <span className="small text-muted ml-2">
            { this.getSubtitle() }
          </span>
        </h4>
      </div>
    );
  }
}

GroupHeading.propTypes = {
  start: PropTypes.string,
  end: PropTypes.string,
  dateJump: PropTypes.oneOf(['week', 'month', 'year', 'day'])
};

export default GroupHeading;