import moment from 'moment'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import * as calendarViews from 'Calendar/calendarViews'; import Icon from 'Components/Icon'; import Button from 'Components/Link/Button'; import LoadingIndicator from 'Components/Loading/LoadingIndicator'; import Menu from 'Components/Menu/Menu'; import MenuButton from 'Components/Menu/MenuButton'; import MenuContent from 'Components/Menu/MenuContent'; import ViewMenuItem from 'Components/Menu/ViewMenuItem'; import { align, icons } from 'Helpers/Props'; import translate from 'Utilities/String/translate'; import CalendarHeaderViewButton from './CalendarHeaderViewButton'; import styles from './CalendarHeader.css'; function getTitle(time, start, end, view, longDateFormat) { const timeMoment = moment(time); const startMoment = moment(start); const endMoment = moment(end); if (view === 'day') { return timeMoment.format(longDateFormat); } else if (view === 'month') { return timeMoment.format('MMMM YYYY'); } else if (view === 'agenda') { return `Agenda: ${startMoment.format('MMM D')} - ${endMoment.format('MMM D')}`; } let startFormat = 'MMM D YYYY'; let endFormat = 'MMM D YYYY'; if (startMoment.isSame(endMoment, 'month')) { startFormat = 'MMM D'; endFormat = 'D YYYY'; } else if (startMoment.isSame(endMoment, 'year')) { startFormat = 'MMM D'; endFormat = 'MMM D YYYY'; } return `${startMoment.format(startFormat)} \u2014 ${endMoment.format(endFormat)}`; } // TODO Convert to a stateful Component so we can track view internally when changed class CalendarHeader extends Component { // // Lifecycle constructor(props, context) { super(props, context); this.state = { view: props.view }; } componentDidUpdate(prevProps) { const view = this.props.view; if (prevProps.view !== view) { this.setState({ view }); } } // // Listeners onViewChange = (view) => { this.setState({ view }, () => { this.props.onViewChange(view); }); } // // Render render() { const { isFetching, time, start, end, longDateFormat, isSmallScreen, collapseViewButtons, onTodayPress, onPreviousPress, onNextPress } = this.props; const view = this.state.view; const title = getTitle(time, start, end, view, longDateFormat); return (
{ isSmallScreen &&
{title}
}
{ !isSmallScreen &&
{title}
}
{ isFetching && } { collapseViewButtons ? { isSmallScreen ? null : Month } Week Forecast {translate('Day')} {translate('Agenda')} :
}
); } } CalendarHeader.propTypes = { isFetching: PropTypes.bool.isRequired, time: PropTypes.string.isRequired, start: PropTypes.string.isRequired, end: PropTypes.string.isRequired, view: PropTypes.oneOf(calendarViews.all).isRequired, isSmallScreen: PropTypes.bool.isRequired, collapseViewButtons: PropTypes.bool.isRequired, longDateFormat: PropTypes.string.isRequired, onViewChange: PropTypes.func.isRequired, onTodayPress: PropTypes.func.isRequired, onPreviousPress: PropTypes.func.isRequired, onNextPress: PropTypes.func.isRequired }; export default CalendarHeader;