import React from 'react'; import {connect} from 'react-redux'; import {get, forEach} from 'lodash'; import moment from 'moment'; import {appConfig} from 'appConfig'; import {superdeskApi} from '../../../superdeskApi'; import {IArticle, IDesk} from 'superdesk-api'; import { ICoverageFormProfile, ICoverageScheduledUpdate, IPlanningCoverageItem, IPlanningItem, IPlanningNewsCoverageStatus, IG2ContentType, IGenre, IKeyword, IFile, } from '../../../interfaces'; import * as selectors from '../../../selectors'; import * as actions from '../../../actions'; import {getItemInArrayById, planningUtils, generateTempId, assignmentUtils} from '../../../utils'; import {WORKFLOW_STATE, TO_BE_CONFIRMED_FIELD} from '../../../constants'; import {Button} from '../../UI'; import {Row, Label, LineInput, FileInput} from '../../UI/Form'; import {ScheduledUpdate} from '../ScheduledUpdate'; import { TextInput, TextAreaInput, ExpandableTextAreaInput, SelectInput, DateTimeInput, SelectTagInput, Field, ToggleInput, } from '../../UI/Form'; import {InternalNoteLabel} from '../../'; import {ContactField, ContactsPreviewList} from '../../Contacts'; import {getUsersDefaultLanguage} from '../../../utils/users'; import '../style.scss'; interface IProps { // Values field: string; value: IPlanningCoverageItem; readOnly: boolean; message: string | {[key: string]: any}; item: IPlanningItem; diff: Partial; formProfile: ICoverageFormProfile; errors: {[key: string]: any} showErrors: boolean; hasAssignment: boolean; addNewsItemToPlanning: IArticle; index: number; defaultDesk: IDesk; files: Array; // Functions onChange(field: string, value: any): void; popupContainer(): HTMLElement; onFieldFocus(): void; onPopupOpen(): void; onPopupClose(): void; onRemoveAssignment( coverage: IPlanningCoverageItem, index: number, scheduledUpdate: any, scheduledUpdateIndex: number ): void; uploadFiles(files: Array>): Promise>; createUploadLink(file: IFile): void; removeFile(file: IFile): Promise; notifyValidationErrors(errors: Array): void; // Redux States newsCoverageStatus: Array; contentTypes: Array; languages: Array; genres: Array; keywords: Array; preferredCoverageDesks: {[key: string]: string}; planningAllowScheduledUpdates: boolean; // Redux Dispatches setCoverageDefaultDesk(coverage: IPlanningCoverageItem): void; } interface IState { openScheduledUpdates: Array; uploading: boolean; } const mapStateToProps = (state) => ({ newsCoverageStatus: selectors.general.newsCoverageStatus(state), contentTypes: selectors.general.contentTypes(state), languages: selectors.vocabs.getLanguages(state), genres: state.genres, keywords: selectors.general.keywords(state), preferredCoverageDesks: selectors.general.preferredCoverageDesks(state)?.desks ?? {}, planningAllowScheduledUpdates: selectors.forms.getPlanningAllowScheduledUpdates(state), }); const mapDispatchToProps = (dispatch) => ({ setCoverageDefaultDesk: (coverage) => dispatch(actions.users.setCoverageDefaultDesk(coverage)), }); export class CoverageFormComponent extends React.Component { fullFilePath: string; xmpFullFilePath: string; dom: { contentType?: any; popupContainer?: any; }; constructor(props) { super(props); this.onScheduleChanged = this.onScheduleChanged.bind(this); this.onTimeToBeConfirmed = this.onTimeToBeConfirmed.bind(this); this.onAddScheduledUpdate = this.onAddScheduledUpdate.bind(this); this.onRemoveScheduledUpdate = this.onRemoveScheduledUpdate.bind(this); this.onScheduledUpdateClose = this.onScheduledUpdateClose.bind(this); this.onScheduledUpdateOpen = this.onScheduledUpdateOpen.bind(this); this.onAddFiles = this.onAddFiles.bind(this); this.onRemoveFile = this.onRemoveFile.bind(this); this.onAddXmpFile = this.onAddXmpFile.bind(this); this.onRemoveXmpFile = this.onRemoveXmpFile.bind(this); this.dom = { contentType: null, popupContainer: null, }; this.state = { openScheduledUpdates: [], uploading: false, }; this.fullFilePath = `coverages[${this.props.index}].planning.files`; this.xmpFullFilePath = `coverages[${this.props.index}].planning.xmp_file`; } componentDidUpdate(prevProps: Readonly) { if (!prevProps.hasAssignment && this.props.hasAssignment) { this.dom.contentType.focus(); } } onTimeToBeConfirmed() { const {onChange, index} = this.props; onChange(`coverages[${index}].${TO_BE_CONFIRMED_FIELD}`, true); } getCoverageFiles(): Array { return this.props.value?.planning?.files ?? []; } onScheduleChanged(field: string, newValue: moment.Moment) { const {value, onChange, index} = this.props; const hasSchedule = value?.planning?.scheduled != null; let finalValue = newValue; let fieldStr: string; let relatedFieldStr: string; // We will be updating scheduled and _scheduledTime together // relatedFieldStr will be '_scheduledTime' if date gets changed and vice versa // Update time only if date is already set if (field.endsWith('.date')) { fieldStr = field.slice(0, -5); relatedFieldStr = field.replace('scheduled.date', '_scheduledTime'); // If there is no current scheduled date, then set the time value to end of the day if (!get(value, 'planning.scheduled')) { finalValue = newValue.add(1, 'hour').startOf('hour'); relatedFieldStr = null; } } else if (field.endsWith('._scheduledTime')) { // If there is no current scheduled date, then set the date to today relatedFieldStr = field.replace('_scheduledTime', 'scheduled'); fieldStr = field; onChange(`coverages[${index}].${TO_BE_CONFIRMED_FIELD}`, false); if (!get(value, 'planning.scheduled')) { finalValue = moment().hour(newValue.hour()) .minute(newValue.minute()); } else { // Set the date from the original date finalValue = moment(value.planning.scheduled) .clone() .hour(newValue.hour()) .minute(newValue.minute()); } } else { onChange(field, newValue); return; } onChange(fieldStr, finalValue); if (relatedFieldStr) { onChange(relatedFieldStr, finalValue); } } onAddScheduledUpdate() { let defaultScheduledUpdate = { coverage_id: get(this.props, 'value.coverage_id'), scheduled_update_id: generateTempId(), planning: { internal_note: get(this.props, 'value.planning.internal_note'), genre: ((get(this.props, 'genres') || []).find((g) => g.qcode === 'Update') || get(this.props, 'value.planning.genre')), }, news_coverage_status: this.props.newsCoverageStatus[0], workflow_status: WORKFLOW_STATE.DRAFT, }; // Set default desks for coverage type planningUtils.setDefaultAssignment(defaultScheduledUpdate, this.props.preferredCoverageDesks, get(this.props, 'value.planning.g2_content_type'), this.props.defaultDesk); this.props.onChange(`${this.props.field}.scheduled_updates`, [ ...get(this.props, 'value.scheduled_updates', []), defaultScheduledUpdate, ]); this.setState({openScheduledUpdates: [ ...this.state.openScheduledUpdates, defaultScheduledUpdate.scheduled_update_id, ]}); } onRemoveScheduledUpdate(indexToRemove: number) { // Remove the scheduled update at the index this.props.onChange(`${this.props.field}.scheduled_updates`, this.props.value.scheduled_updates.filter( (_, index) => index !== indexToRemove) ); } onScheduledUpdateOpen(scheduledUpdate: ICoverageScheduledUpdate) { if (!this.state.openScheduledUpdates.includes(scheduledUpdate.scheduled_update_id)) { this.setState({openScheduledUpdates: [ ...this.state.openScheduledUpdates, scheduledUpdate.scheduled_update_id, ]}); } } onScheduledUpdateClose(scheduledUpdate: ICoverageScheduledUpdate) { this.setState({ openScheduledUpdates: this.state.openScheduledUpdates.filter((s) => s !== scheduledUpdate.scheduled_update_id )}); } onAddXmpFile(fileList: FileList) { const {gettext} = superdeskApi.localization; if (get(fileList, 'length', 0) > 1) { this.props.notifyValidationErrors([gettext('You can associate only one XMP file')]); return; } let error; forEach(fileList, (f) => { if (!get(f, 'name').toLowerCase() .endsWith('.xmp')) { error = true; } }); if (error) { this.props.notifyValidationErrors([gettext('Only one XMP files are accepted')]); return; } this.onAddFiles(fileList, true); } onRemoveXmpFile(file: IFile) { this.onRemoveFile(file, true); } onAddFiles(fileList: FileList, xmpFile: boolean = false) { const files = Array.from(fileList).map((f) => [f]); const changeFullFilePath = xmpFile ? this.xmpFullFilePath : this.fullFilePath; this.setState({uploading: true}); this.props.uploadFiles(files) .then((newFiles) => { const value = xmpFile ? get(newFiles, '[0]._id') : [ ...this.getCoverageFiles(), ...newFiles.map((f) => f._id), ]; this.props.onChange(changeFullFilePath, value); this.setState({uploading: false}); }, () => { this.props.notifyValidationErrors(['Failed to upload files']); this.setState({uploading: false}); }); } onRemoveFile(file: IFile, xmpFile: boolean = false) { const coverageFiles = this.getCoverageFiles(); const promise = (xmpFile ? file : !coverageFiles.includes(file._id)) ? this.props.removeFile(file) : Promise.resolve(); const changeFullFilePath = xmpFile ? this.xmpFullFilePath : this.fullFilePath; const value = xmpFile ? null : coverageFiles.filter((f) => f !== file._id); promise.then(() => this.props.onChange(changeFullFilePath, value)); } render() { const {gettext} = superdeskApi.localization; const { field, value, index, onChange, newsCoverageStatus, contentTypes, languages, genres, keywords, readOnly, item, diff, formProfile, errors, showErrors, hasAssignment, addNewsItemToPlanning, popupContainer, onFieldFocus, onPopupOpen, onPopupClose, planningAllowScheduledUpdates, onRemoveAssignment, setCoverageDefaultDesk, createUploadLink, files, ...props } = this.props; const contentTypeQcode = get(value, 'planning.g2_content_type') || null; const contentType = contentTypeQcode ? getItemInArrayById(contentTypes, contentTypeQcode, 'qcode') : null; const onContentTypeChange = (f, v) => { if (v) { onChange(f, get(v, 'qcode') || null); onChange(`${field}.planning.genre`, null); } }; const defaultGenre = (appConfig.default_genre || [{}])[0]; if (contentTypeQcode === 'text' && !get(value, 'planning.genre')) { value.planning.genre = defaultGenre; } const fieldProps = { item: item, diff: diff, onChange: onChange, formProfile: formProfile, errors: errors, showErrors: showErrors, onFocus: onFieldFocus, }; const roFields = planningUtils.getCoverageReadOnlyFields( value, readOnly, newsCoverageStatus, addNewsItemToPlanning ); const canCreateScheduledUpdate = !addNewsItemToPlanning && !get(diff, `${field}.flags.no_content_linking`); const contactLabel = assignmentUtils.getContactLabel(get(diff, field)); const showXmpFileInput = planningUtils.showXMPFileUIControl(value); const hideXMPFileInput = this.props.value?.planning?.xmp_file != null; return (
{get(diff, `${field}.assigned_to.contact`) ? ( ) : ( )} this.dom.contentType = ref} language={value.planning.language} /> {showXmpFileInput && (
{!this.state.uploading && ( )}
)} {get(formProfile, 'editor.files.enabled') && (
{!this.state.uploading && ( )}
)} {planningAllowScheduledUpdates && contentTypeQcode === 'text' && ( {(value.scheduled_updates || []).map((s, i) => ( ))} {canCreateScheduledUpdate && (
); } } export const CoverageForm = connect(mapStateToProps, mapDispatchToProps)(CoverageFormComponent);