All files / date-picker/components/DatePicker Title.jsx

46.67% Statements 7/15
12.5% Branches 1/8
33.33% Functions 1/3
50% Lines 7/14
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            1x     1x   1x                   1x   1x     1x           1x              
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
 
import Text from '@bufferapp/components/Text';
 
const formatDate = (date, dateFormat = 'MM/DD/YY') =>
  moment.unix(date).format(dateFormat);
 
const Title = ({ presets, loading, startDate, endDate }) => {
  let title;
  Iif (!loading) {
    const selectedRange = presets.find(preset => preset.selected);
    if (selectedRange.label === 'Custom' && startDate && endDate) {
      const from = formatDate(startDate);
      const to = formatDate(endDate);
      title = `From: ${from} - To: ${to}`;
    } else {
      title = selectedRange.label;
    }
  } else {
    title = 'Loading...';
  }
  return (<Text color="outerSpace" size="small" weight="medium">{title}</Text>);
};
 
Title.defaultProps = {
  loading: false,
  startDate: 0,
  endDate: 0,
};
 
Title.propTypes = {
  startDate: PropTypes.number,
  endDate: PropTypes.number,
  loading: PropTypes.bool,
};
 
export default Title;