import React from 'react'; import {connect} from 'react-redux'; import {get, some, isEqual} from 'lodash'; import {IDesk, ISubject, IUser, IVocabulary} from 'superdesk-api'; import {superdeskApi} from '../../../superdeskApi'; import { IEventFormProfile, IEventItem, IEventOccurStatus, ICalendar, IANPACategory, IPlanningItem, IFile, IFormNavigation, IFormItemManager, ILocator, } from '../../../interfaces'; import * as selectors from '../../../selectors'; import * as actions from '../../../actions'; import {editorMenuUtils, getItemId, getFileDownloadURL} from '../../../utils'; import {TO_BE_CONFIRMED_FIELD} from '../../../constants'; import {ContentBlock} from '../../UI/SidePanel'; import { TextInput, SelectInput, SelectMetaTermsInput, TextAreaInput, ExpandableTextAreaInput, FileInput, InputArray, LinkInput, Field, } from '../../UI/Form'; import {ContactField} from '../../Contacts'; import {ToggleBox, IconButton} from '../../UI'; import {RelatedPlannings} from '../../RelatedPlannings'; import {EventScheduleInput, EventScheduleSummary} from '../'; import {GeoLookupInput} from '../../index'; import {EventEditorHeader} from './EventEditorHeader'; import CustomVocabulariesFields from '../../CustomVocabulariesFields'; import {getUsersDefaultLanguage} from '../../../utils/users'; const toggleDetails = [ 'anpa_category', 'subject', 'definition_long', 'internal_note', 'ednote', ]; interface IProps { item?: IEventItem; diff: Partial; itemExists: boolean; onChangeHandler(field: string, value: any): void; formProfile: IEventFormProfile; occurStatuses: Array; languages: Array; enabledCalendars: Array; defaultCalendar: Array; locators: Array; categories: Array; subjects: Array; users: Array; desks: Array; readOnly: boolean; submitting: boolean; submitFailed: boolean; dirty: boolean; errors: {[key: string]: string}; plannings: Array; navigation?: IFormNavigation; fetchEventFiles(event: IEventItem): Promise; customVocabularies: Array; files: Array; uploadFiles(files: Array>): Promise>; removeFile(file: IFile): Promise; popupContainer(): HTMLElement; onPopupOpen(): void; onPopupClose(): void; itemManager: IFormItemManager; original?: IEventItem; notifyValidationErrors(errors: Array): void; } interface IState { uploading: boolean; } const mapStateToProps = (state) => ({ formProfile: selectors.forms.eventProfile(state), languages: selectors.vocabs.getLanguages(state), occurStatuses: selectors.vocabs.eventOccurStatuses(state), enabledCalendars: selectors.events.enabledCalendars(state), defaultCalendar: selectors.events.defaultCalendarValue(state), locators: selectors.vocabs.locators(state), categories: selectors.vocabs.categories(state), subjects: selectors.vocabs.subjects(state), users: selectors.general.users(state), desks: selectors.general.desks(state), customVocabularies: state.customVocabularies, files: selectors.general.files(state), }); const mapDispatchToProps = (dispatch) => ({ fetchEventFiles: (event) => dispatch(actions.events.api.fetchEventFiles(event)), uploadFiles: (files) => dispatch(actions.events.api.uploadFiles({files: files})), removeFile: (file) => dispatch(actions.events.api.removeFile(file)), }); export class EventEditorComponent extends React.Component { dom: { initialFocus: any; top: any; contacts: any; }; constructor(props) { super(props); this.dom = { initialFocus: null, top: null, contacts: null, }; this.state = {uploading: false}; this.onAddFiles = this.onAddFiles.bind(this); this.onRemoveFile = this.onRemoveFile.bind(this); } componentWillMount() { this.props.fetchEventFiles({...this.props.item, ...this.props.diff}); } componentWillUpdate(nextProps: Readonly) { if (getItemId(this.props.item) !== getItemId(nextProps.item)) { this.props.fetchEventFiles({...nextProps.item, ...nextProps.diff}); } else if (get(this.props, 'diff.files') !== get(nextProps, 'diff.files')) { this.props.fetchEventFiles({...nextProps.item, ...nextProps.diff}); } } componentDidMount() { if (!get(this.props, 'navigation.scrollToViewItem') && this.dom.initialFocus) { this.dom.initialFocus.focus(); var tempValue = get(this.dom.initialFocus, 'value', ''); this.dom.initialFocus.value = ''; this.dom.initialFocus.value = tempValue; } // scroll to contacts if (editorMenuUtils.forceScroll(this.props.navigation, 'contacts')) { this.dom.contacts.scrollIntoView(); } } componentDidUpdate(prevProps: Readonly) { const prevItemId = getItemId(prevProps.item); const currentItemId = getItemId(this.props.item); // If item changed or it got locked for editing if (((prevItemId !== currentItemId) || (!get(prevProps, 'diff.lock_user') && get(this.props, 'diff.lock_user'))) && this.dom.initialFocus) { this.dom.initialFocus.focus(); } if (get(prevProps, 'navigation.scrollToViewItem') !== get(this.props, 'navigation.scrollToViewItem')) { // scroll to new position if (editorMenuUtils.forceScroll(this.props.navigation, 'event')) { this.dom.top.scrollIntoView(); } // scroll to contacts if (editorMenuUtils.forceScroll(this.props.navigation, 'contacts')) { this.dom.contacts.scrollIntoView(); } } if (this.dom.top) { this.dom.top.scrollTop = 150; } } componentWillReceiveProps(nextProps: Readonly) { // If 'Create Planning Item' was actioned while open in the editor // Then force update the initial values with the new list of ids if (!isEqual( get(this.props, 'original.planning_ids'), get(nextProps, 'original.planning_ids')) ) { this.props.itemManager.forceUpdateInitialValues({ planning_ids: get(nextProps, 'original.planning_ids'), }); } } getRelatedPlanningsForEvent(): Array { const {plannings, item} = this.props; const itemId = getItemId(item); if (plannings.filter((p) => p.event_item === itemId).length > 0) { return plannings; } } onAddFiles(fileList: FileList) { const files = Array.from(fileList).map((f) => [f]); this.setState({uploading: true}); this.props.uploadFiles(files) .then((newFiles) => { this.props.onChangeHandler('files', [ ...get(this.props, 'diff.files', []), ...newFiles.map((f) => f._id), ]); this.setState({uploading: false}); }, () => { this.props.notifyValidationErrors(['Failed to upload files']); this.setState({uploading: false}); }); } onRemoveFile(file: IFile) { const promise = !get(this.props, 'item.files', []).includes(file._id) ? this.props.removeFile(file) : Promise.resolve(); promise.then(() => this.props.onChangeHandler('files', get(this.props, 'diff.files', []).filter((f) => f !== file._id)) ); } render() { const {gettext} = superdeskApi.localization; const { item, diff, occurStatuses, languages, enabledCalendars, locators, categories, subjects, users, desks, readOnly = false, formProfile, submitFailed = false, dirty, errors, onChangeHandler, navigation, itemExists, customVocabularies, files, onPopupOpen, onPopupClose, } = this.props; const detailsErrored = some(toggleDetails, (field) => !!get(errors, field)); const relatedPlannings = this.getRelatedPlanningsForEvent(); const onFocusEvent = editorMenuUtils.onItemFocus(this.props.navigation, 'event'); const onFocusContacts = editorMenuUtils.onItemFocus(this.props.navigation, 'contacts'); const onFocusDetails = editorMenuUtils.onItemFocus(this.props.navigation, 'details'); const onFocusFiles = editorMenuUtils.onItemFocus(this.props.navigation, 'files'); const onFocusLinks = editorMenuUtils.onItemFocus(this.props.navigation, 'links'); const onFocusPlannings = editorMenuUtils.onItemFocus(this.props.navigation, 'planning'); const fieldProps = { item: item, diff: diff, readOnly: readOnly, onChange: onChangeHandler, formProfile: formProfile, errors: errors, showErrors: submitFailed, }; const popupProps = { onPopupOpen, onPopupClose, }; const getCountOfProperty = (propertyName) => { const count = get(this.props, `diff.${propertyName}.length`, 0); return count > 0 ? count : null; }; const AddLinkButton = ({onAdd}) => ( ); return (
this.dom.top = node} > {itemExists && ( )} this.dom.initialFocus = node : undefined} /> this.dom.initialFocus = node : undefined} {...fieldProps} onFocus={onFocusEvent} /> this.dom.initialFocus = node} {...fieldProps} onFocus={onFocusEvent} /> this.dom.contacts = node} defaultValue={[]} {...fieldProps} onFocus={onFocusContacts} paddingTop={!!onFocusContacts} {...popupProps} /> {!!get(formProfile, 'editor.subject.enabled') && ( )}
{ !this.state.uploading && ( )}
{relatedPlannings && (

{gettext('Related Planning Items')}

)} {get(relatedPlannings, 'length', 0) > 0 && ( )}
); } } export const EventEditor = connect(mapStateToProps, mapDispatchToProps)(EventEditorComponent);