import * as React from 'react'; import {connect} from 'react-redux'; import {get} from 'lodash'; import {superdeskApi} from '../../../superdeskApi'; import {IDesk} from 'superdesk-api'; import {IEditorFieldProps, IContentTemplate} from '../../../interfaces'; import {EditorFieldSelect} from './base/select'; import {templates as getTemplates} from '../../../selectors/general'; interface IProps extends IEditorFieldProps { templates: Array; deskId?: IDesk['_id']; clearable?: boolean; } const mapStatToProps = (state) => ({ templates: getTemplates(state), }); export class EditorFieldContentTemplateComponent extends React.PureComponent { getFieldName(): string { return this.props.field ?? 'content_template'; } getAvailableTemplates(): Array { return this.props.deskId == null ? this.props.templates : this.props.templates.filter( (template) => ( (template.template_desks ?? []).includes(this.props.deskId) ) ); } isCurrentTemplateAvailable(): boolean { const value: IContentTemplate['_id'] = get( this.props.item, this.getFieldName(), this.props.defaultValue ); if (value == null) { return true; } const index = this.getAvailableTemplates() .findIndex((template) => template._id === value); return index !== -1; } componentDidUpdate(prevProps: Readonly, prevState: Readonly<{}>, snapshot?: any) { if (prevProps.deskId !== this.props.deskId && !this.isCurrentTemplateAvailable()) { // If the desk changes and the currently selected template is no longer available // then set the value to `null` this.props.onChange(this.getFieldName(), null); } } render() { const {gettext} = superdeskApi.localization; const templates = this.getAvailableTemplates(); if (!templates.length) { return ( ); } return ( ); } } export const EditorFieldContentTemplate = connect(mapStatToProps)(EditorFieldContentTemplateComponent);