import React from 'react'; import PropTypes from 'prop-types'; import {connect} from 'react-redux'; import {get, cloneDeep, isEmpty} from 'lodash'; import * as selectors from '../../../selectors'; import {formProfile} from '../../../validators'; import {gettext} from '../../../utils'; import {PLANNING, WORKFLOW_STATE} from '../../../constants'; import {Row} from '../../UI/Preview'; import {TextAreaInput} from '../../UI/Form'; import '../style.scss'; export class CancelCoverageComponent extends React.Component { constructor(props) { super(props); this.state = {reason: '', errors: {}}; this.onReasonChange = this.onReasonChange.bind(this); } componentWillMount() { // Enable save so that the user can action on this event. get(this.props.coverageCancelProfile, 'schema.reason.required', false) ? this.props.disableSaveInModal() : this.props.enableSaveInModal(); } onReasonChange(field, reason) { const errors = cloneDeep(this.state.errors); let errorMessages = []; if (this.props.coverageCancelProfile) { formProfile( { field: field, value: reason, profile: this.props.coverageCancelProfile, errors: errors, messages: errorMessages, } ); if (get(errorMessages, 'length', 0) > 0 || (get(this.props.coverageCancelProfile, 'schema.reason.required', false) && isEmpty(reason))) { this.props.disableSaveInModal(); } else { this.props.enableSaveInModal(); } } this.setState({ reason, errors, }); } submit() { const forScheduledUpdate = !!this.props.modalProps.scheduledUpdate; let coverage = forScheduledUpdate ? cloneDeep(this.props.modalProps.scheduledUpdate) : cloneDeep(this.props.modalProps.coverage); coverage.news_coverage_status = PLANNING.NEWS_COVERAGE_CANCELLED_STATUS; coverage.planning.workflow_status_reason = this.state.reason; coverage.workflow_status = WORKFLOW_STATE.CANCELLED; if (get(coverage, 'assigned_to.state')) { coverage.assigned_to.state = WORKFLOW_STATE.CANCELLED; } return this.props.onSubmit( this.props.original, forScheduledUpdate ? null : coverage, this.props.modalProps.index, forScheduledUpdate ? coverage : null, this.props.modalProps.scheduledUpdateIndex); } render() { const label = this.props.modalProps.scheduledUpdate ? gettext('Reason for cancelling the scheduled update') : gettext('Reason for cancelling the coverage'); return (