import React from 'react'; import PropTypes from 'prop-types'; import {connect} from 'react-redux'; import {get, isEqual, cloneDeep, omit, isEmpty} from 'lodash'; import moment from 'moment'; import {appConfig} from 'appConfig'; import * as actions from '../../../actions'; import {formProfile, validateItem} from '../../../validators'; import * as selectors from '../../../selectors'; import {gettext, eventUtils, getDateTimeString, updateFormValues, timeUtils} from '../../../utils'; import {EVENTS, ITEM_TYPE, TIME_COMPARISON_GRANULARITY, TO_BE_CONFIRMED_FIELD} from '../../../constants'; import {EventScheduleSummary, EventScheduleInput} from '../../Events'; import {RelatedPlannings} from '../../'; import {Row} from '../../UI/Preview'; import {TextAreaInput, Field} from '../../UI/Form'; import '../style.scss'; export class RescheduleEventComponent extends React.Component { constructor(props) { super(props); this.state = { diff: null, reason: '', errors: {}, multiDayChanged: false, reasonInvalid: false, datesInvalid: false, }; this.onReasonChange = this.onReasonChange.bind(this); this.onDatesChange = this.onDatesChange.bind(this); this.getPopupContainer = this.getPopupContainer.bind(this); this.enableDisableSave = this.enableDisableSave.bind(this); this.dom = {popupContainer: null}; } componentWillMount() { const dates = cloneDeep(this.props.original.dates); const isRemoteTimeZone = timeUtils.isEventInDifferentTimeZone(this.props.original || {}); if (isRemoteTimeZone) { dates.start = timeUtils.getDateInRemoteTimeZone(dates.start, dates.tz); dates.end = timeUtils.getDateInRemoteTimeZone(dates.end, dates.tz); } let reasonInvalid = false; if (get(this.props.rescheduleProfile, 'schema.reason.required', false)) { reasonInvalid = true; } this.setState({ diff: { dates: dates, _startTime: cloneDeep(dates.start), _endTime: cloneDeep(dates.end), [TO_BE_CONFIRMED_FIELD]: this.props.original[TO_BE_CONFIRMED_FIELD], }, reasonInvalid: reasonInvalid, }); } onReasonChange(field, reason) { const errors = cloneDeep(this.state.errors); let errorMessages = []; let reasonInvalid = false; if (this.props.rescheduleProfile) { formProfile( { field: field, value: reason, profile: this.props.rescheduleProfile, errors: errors, messages: errorMessages, } ); if (get(errorMessages, 'length', 0) > 0 || (get(this.props.rescheduleProfile, 'schema.reason.required', false) && isEmpty(reason))) { reasonInvalid = true; } else { reasonInvalid = false; } } this.setState({ reason, errors, reasonInvalid, }, this.enableDisableSave); } enableDisableSave() { if (this.state.reasonInvalid || this.state.datesInvalid) { this.props.disableSaveInModal(); } else { this.props.enableSaveInModal(); } } onDatesChange(field, val) { const diff = cloneDeep(get(this.state, 'diff') || {}); const original = this.props.original; let datesInvalid = false; if (typeof diff.dates === 'object' && !diff.dates.tz) { // if no timezone use default one diff.dates.tz = appConfig.defaultTimezone; } if (field === 'dates.recurring_rule' && !val) { delete diff.dates.recurring_rule; } else { updateFormValues(diff, field, val); } const errors = cloneDeep(this.state.errors); let errorMessages = []; const fieldsToValidate = Object.keys(diff); this.props.onValidate( omit(diff, 'dates.recurring_rule'), // Omit recurring rules as we reschedule only single instance this.props.formProfiles, errors, errorMessages, fieldsToValidate // Validate only those fields which can change while rescheduling. ); const multiDayChanged = eventUtils.isEventSameDay(original.dates.start, original.dates.end) && !eventUtils.isEventSameDay(diff.dates.start, diff.dates.end); if ((!diff[TO_BE_CONFIRMED_FIELD] && eventUtils.eventsDatesSame(diff, original, TIME_COMPARISON_GRANULARITY.MINUTE)) || (diff.dates.recurring_rule && !diff.dates.recurring_rule.until && !diff.dates.recurring_rule.count) || !isEqual(errorMessages, []) ) { datesInvalid = true; } else { datesInvalid = false; } this.setState({ diff, errors, multiDayChanged, datesInvalid, }, this.enableDisableSave); } submit() { const reason = this.state.reason ? (gettext('Event Rescheduled: ') + this.state.reason) : this.state.reason; return this.props.onSubmit( this.props.original, { ...this.state.diff, reason: reason, }, get(this.props, 'modalProps.onCloseModal') ); } getPopupContainer() { return this.dom.popupContainer; } render() { const {original, formProfiles, submitting} = this.props; let reasonLabel = gettext('Reason for rescheduling this event:'); const numPlannings = get(original, '_plannings.length'); const afterUntil = moment.isMoment(get(original, 'dates.recurring_rule.until')) && moment.isMoment(get(this.state, 'diff.dates.start')) && this.state.diff.dates.start.isAfter(original.dates.recurring_rule.until); const timeZone = get(original, 'dates.tz') || appConfig.defaultTimezone; const dateFormat = appConfig.planning.dateformat; const timeFormat = appConfig.planning.timeformat; return (