import React from 'react'; import PropTypes from 'prop-types'; import {connect} from 'react-redux'; import {get, cloneDeep, isEmpty} from 'lodash'; import * as actions from '../../../actions'; import * as selectors from '../../../selectors'; import {eventUtils, gettext} from '../../../utils'; import {EVENTS} from '../../../constants'; import {EventScheduleSummary, EventUpdateMethods} from '../../Events'; import {UpdateMethodSelection} from '../UpdateMethodSelection'; import {Row} from '../../UI/Preview'; import {TextAreaInput} from '../../UI/Form'; import {formProfile} from '../../../validators'; import '../style.scss'; export class CancelEventComponent extends React.Component { constructor(props) { super(props); this.state = { eventUpdateMethod: EventUpdateMethods[0], reason: '', relatedEvents: [], relatedPlannings: [], errors: {}, }; this.onEventUpdateMethodChange = this.onEventUpdateMethodChange.bind(this); this.onReasonChange = this.onReasonChange.bind(this); } componentWillMount() { this.updatePlanningList(this.state.eventUpdateMethod); // Enable save so that the user can action on this event. get(this.props, 'formProfile.schema.reason.required', false) ? this.props.disableSaveInModal() : this.props.enableSaveInModal(); } updatePlanningList(updateMethod) { const event = eventUtils.getRelatedEventsForRecurringEvent( this.props.original, updateMethod ); this.setState({ eventUpdateMethod: updateMethod, relatedEvents: event._events, relatedPlannings: event._relatedPlannings, }); } submit() { const reason = this.state.reason ? (gettext('Event Cancelled: ') + this.state.reason) : this.state.reason; return this.props.onSubmit( this.props.original, { update_method: this.state.eventUpdateMethod, reason: reason, } ); } onEventUpdateMethodChange(field, option) { this.updatePlanningList(option); } onReasonChange(field, reason) { const errors = cloneDeep(this.state.errors); let errorMessages = []; if (this.props.formProfile) { formProfile( { field: field, value: reason, profile: this.props.formProfile, errors: errors, messages: errorMessages, } ); if (get(errorMessages, 'length', 0) > 0 || (get(this.props.formProfile, 'schema.reason.required', false) && isEmpty(reason))) { this.props.disableSaveInModal(); } else { this.props.enableSaveInModal(); } } this.setState({ reason, errors, }); } render() { const {original, submitting} = this.props; const isRecurring = !!original.recurrence_id; const numEvents = this.state.relatedEvents.length + 1; const numPlannings = this.state.relatedPlannings.length; return (