import * as PlusIcon from 'material-design-icons/content/svg/production/ic_add_circle_outline_24px.svg'; // Libs import * as React from 'react'; // Misc import { Time } from '../../time/Time'; import OpeningHoursUtil from '../utils/OpeningHoursUtil'; import TimeUtil from '../../utils/TimeUtil'; import InputGroup from '../../inputGroup/InputGroup'; import { BaseInputProps, BaseInputState, BaseInput, defaultBaseTranslations, } from '../../base/input/BaseInput'; import { Button } from '../../button/Button'; import { withFormContext } from '../../form/withFormContext'; import classNames from 'classnames'; import moment, { Duration } from 'moment'; import NumberInput from '../../numberInput/NumberInput'; import { withThemeContext } from '../../themeProvider/withThemeContext'; export interface OpeningHoursPeriodObj { opens: Duration; closes: Duration; capacity?: number; } export interface OpeningHoursDayObj { times: OpeningHoursPeriodObj[]; } export const defaultDayTranslations = { opens: 'Opens', closes: 'Closes', capacity: 'Capacity', add: 'Add new time range', midnight: 'Midnight', ...defaultBaseTranslations, }; export type OpeningHoursDayTranslations = Partial< typeof defaultDayTranslations >; export interface OpeningHoursDayProps extends BaseInputProps { onChange?: (e: React.ChangeEvent) => void; onOpeningHoursChange: (openingHours: OpeningHoursDayObj) => void; openingHours: OpeningHoursDayObj; label?: JSX.Element | string; maxOpenCloseTimes?: number; useCapacity?: boolean; } export interface OpeningHoursState extends BaseInputState {} export class OpeningHoursDayRaw extends BaseInput< OpeningHoursDayProps, OpeningHoursState, never, OpeningHoursDayTranslations > { public static defaultProps = Object.assign({}, BaseInput.defaultProps, { onChange: undefined, type: 'openingHours', allowMultiple: false, maxOpenCloseTimes: 10, }); private fullDayMilliseconds: number = 24 * 60 * 60 * 1000; constructor(props: OpeningHoursDayProps) { super(props); } public render() { const translations = this.getTranslations(defaultDayTranslations); return (
{this.props.openingHours && this.props.openingHours.times && this.props.openingHours.times.map((item, index) => { const times = [item.opens, item.closes]; return (
{times.map((time, indexJ) => { const localIndex = index * 2 + indexJ; const previousTime = indexJ > 0 ? times[indexJ - 1].clone() : index === 0 ? this.getTime(0, 0) : this.props.openingHours.times[ index - 1 ].closes.clone(); let nextTime = indexJ === 0 ? times[1] : this.props.openingHours.times.length - 1 > index ? this.props.openingHours.times[ index + 1 ].opens.clone() : this.getTime(23, 59, true); if (localIndex % 2 === 0) { nextTime = moment.duration( Math.min.apply(null, [ this.getTime(23, 59, false).asMilliseconds(), nextTime.asMilliseconds(), ]), 'millisecond' ); } return (
); })} {this.props.maxOpenCloseTimes > this.props.openingHours.times.length && ( )}
{this.renderDefaultValidation()} {this.props.openingHours && this.props.label && ( {this.renderLabel(true)} )}
{this.getBottomBorder(translations)}
); } private isAddTimeClickDisabled() { if ( this.props.openingHours && this.props.openingHours.times && this.props.openingHours.times.length ) { const dayDiff = TimeUtil.dayDiff( this.props.openingHours.times[0].opens, this.props.openingHours.times[this.props.openingHours.times.length - 1] .closes ); if (dayDiff > 0) { return true; } } return false; } private addTimeClick = () => { let newTime = this.props.openingHours && this.props.openingHours.times && this.props.openingHours.times.length ? this.props.openingHours.times[ this.props.openingHours.times.length - 1 ].closes.clone() : this.getTime(8, 0); if (newTime.hours() < 23) { newTime = newTime.add(1, 'hour'); } let closeTime = newTime.clone().add(1, 'hour'); this.props.onOpeningHoursChange({ ...this.props.openingHours, times: this.props.openingHours.times.concat({ opens: newTime, closes: closeTime, capacity: this.props.useCapacity ? 1 : undefined, }), }); }; private removeTimeClick = (index: number) => () => this.props.onOpeningHoursChange({ ...this.props.openingHours, times: this.props.openingHours.times.filter( (time, itemIndex) => itemIndex < index || itemIndex > index + 1 ), }); private timeChanged = (index: number, indexJ: number) => (time: Duration) => { let newOpeningHours: OpeningHoursDayObj = { ...this.props.openingHours, times: this.props.openingHours.times.slice(0), }; if (indexJ === 0) { newOpeningHours.times[index] = { ...newOpeningHours.times[index], opens: time, }; } else { newOpeningHours.times[index] = { ...newOpeningHours.times[index], closes: time, }; } this.props.onOpeningHoursChange(newOpeningHours); }; private getBottomBorder(translations: OpeningHoursDayTranslations) { const times = ( (this.props.openingHours && this.props.openingHours.times) || [] ).reduce((a, b) => a.concat([b.opens, b.closes]), []); let parts = []; let totalTime = this.fullDayMilliseconds; if (!times.length) { parts = parts.concat([1]); } else { totalTime = Math.max( this.fullDayMilliseconds, OpeningHoursUtil.getTimeFromMidnight(times[times.length - 1], times[0]) ); parts = parts.concat([ OpeningHoursUtil.getTimeFromMidnight(times[0]) / totalTime, ]); for (var index = 0; index < times.length - 1; index++) { let start = OpeningHoursUtil.getTimeFromMidnight( times[index], times[0] ); let end = OpeningHoursUtil.getTimeFromMidnight( times[index + 1], times[0] ); let diff = (end - start) / totalTime; parts = parts.concat([diff]); } parts = parts.concat([ (totalTime - OpeningHoursUtil.getTimeFromMidnight( times[times.length - 1], times[0] )) / totalTime, ]); } return (
{parts.map((part, i) => (
))} {totalTime > this.fullDayMilliseconds && (
)}
); } private getTime( hours: number, minutes: number, nextDay: boolean = false ): Duration { let time = moment .duration(0, 'millisecond') .add(hours, 'hours') .add(minutes, 'minutes'); if (nextDay) { time = time.add(1, 'day'); } return time; } } export const OpeningHoursDay = withThemeContext< OpeningHoursDayProps, InstanceType >(withFormContext(OpeningHoursDayRaw), 'openingHoursDay'); export default OpeningHoursDay;