From abe10c57103e3fa1dd6dedc4488e6106be7dad89 Mon Sep 17 00:00:00 2001 From: Tomas Kikutis Date: Tue, 5 Nov 2024 11:07:03 +0100 Subject: [PATCH] Fix ingest config (#2120) --- .../AutopostIngestRuleEditor.tsx | 99 +++++++----- .../AutopostIngestRulePreview.tsx | 147 ++++++++++-------- 2 files changed, 144 insertions(+), 102 deletions(-) diff --git a/client/planning-extension/src/ingest_rule_autopost/AutopostIngestRuleEditor.tsx b/client/planning-extension/src/ingest_rule_autopost/AutopostIngestRuleEditor.tsx index 66b9a7d99..7ce07ccee 100644 --- a/client/planning-extension/src/ingest_rule_autopost/AutopostIngestRuleEditor.tsx +++ b/client/planning-extension/src/ingest_rule_autopost/AutopostIngestRuleEditor.tsx @@ -1,9 +1,7 @@ import * as React from 'react'; import { IIngestRuleHandlerEditorProps, - ILiveResourcesProps, IRestApiResponse, - IVocabulary, IVocabularyItem } from 'superdesk-api'; import {IAgenda} from '../../../interfaces'; @@ -12,7 +10,6 @@ import {superdesk} from '../superdesk'; import {extensionBridge} from '../extension_bridge'; const {EditorFieldVocabulary} = extensionBridge.ui.components; -const {WithLiveResources} = superdesk.components; interface IExtraAttributes { autopost: boolean, @@ -22,12 +19,27 @@ interface IExtraAttributes { type IProps = IIngestRuleHandlerEditorProps; -export class AutopostIngestRuleEditor extends React.PureComponent { +interface IStateLoading { + loading: true; +} + +interface IStateLoaded { + loading: false; + agendas: Array; +} + +type IState = IStateLoading | IStateLoaded; + +export class AutopostIngestRuleEditor extends React.PureComponent { constructor(props: IProps) { super(props); this.updateAttributes = this.updateAttributes.bind(this); this.updateAutopostValue = this.updateAutopostValue.bind(this); + + this.state = { + loading: true, + }; } updateAttributes(field: T, value: IExtraAttributes[T]) { @@ -47,12 +59,26 @@ export class AutopostIngestRuleEditor extends React.PureComponent { this.updateAttributes('autopost', value); } + componentDidMount(): void { + superdesk.httpRequestJsonLocal>({ + method: 'GET', + path: '/agenda', + }).then((res) => { + this.setState({ + loading: false, + agendas: res._items, + }); + }); + } + render() { + if (this.state.loading) { + return null; + } + + const {agendas} = this.state; + const calendars = superdesk.entities.vocabulary.getAll().get('event_calendars').items; const {gettext} = superdesk.localization; - const resources: ILiveResourcesProps['resources'] = [ - {resource: 'vocabularies', ids: ['event_calendars']}, - {resource: 'agenda'}, - ]; return (
@@ -61,38 +87,31 @@ export class AutopostIngestRuleEditor extends React.PureComponent { value={this.props.rule.actions.extra?.autopost === true} onChange={this.updateAutopostValue} /> - {(resourcesResponse) => { - const calendars = resourcesResponse[0] as IRestApiResponse; - const agendas = resourcesResponse[1] as IRestApiResponse; - - return ( - - ( - item.is_active !== false - ))} - valueAsString={true} - /> - ( - item.is_enabled !== false - ))} - valueAsString={true} - valueKey="_id" - /> - - ); - }} + + ( + item.is_active !== false + ))} + valueAsString={true} + /> + + ( + item.is_enabled !== false + ))} + valueAsString={true} + valueKey="_id" + />
); } diff --git a/client/planning-extension/src/ingest_rule_autopost/AutopostIngestRulePreview.tsx b/client/planning-extension/src/ingest_rule_autopost/AutopostIngestRulePreview.tsx index 05806a06d..e0103f661 100644 --- a/client/planning-extension/src/ingest_rule_autopost/AutopostIngestRulePreview.tsx +++ b/client/planning-extension/src/ingest_rule_autopost/AutopostIngestRulePreview.tsx @@ -1,10 +1,9 @@ import * as React from 'react'; -import {IIngestRuleHandlerPreviewProps, ILiveResourcesProps, IRestApiResponse, IVocabulary} from 'superdesk-api'; +import {IIngestRuleHandlerPreviewProps, IRestApiResponse} from 'superdesk-api'; import {superdesk} from '../superdesk'; import {IAgenda} from '../../../interfaces'; import {extensionBridge} from '../extension_bridge'; -const {WithLiveResources} = superdesk.components; const {getUserInterfaceLanguageFromCV, getVocabularyItemFieldTranslated} = extensionBridge.ui.utils; type IProps = IIngestRuleHandlerPreviewProps<{ @@ -13,70 +12,94 @@ type IProps = IIngestRuleHandlerPreviewProps<{ calendars?: Array; }>; -export class AutopostIngestRulePreview extends React.PureComponent { +interface IStateLoading { + loading: true; +} + +interface IStateLoaded { + loading: false; + agendas: Array; +} + +type IState = IStateLoading | IStateLoaded; + +export class AutopostIngestRulePreview extends React.PureComponent { + constructor(props: IProps) { + super(props); + + this.state = { + loading: true, + }; + } + + componentDidMount(): void { + superdesk.httpRequestJsonLocal>({ + method: 'GET', + path: '/agenda', + }).then((res) => { + this.setState({ + loading: false, + agendas: res._items, + }); + }); + } + render() { - const {gettext} = superdesk.localization; - const resources: ILiveResourcesProps['resources'] = [ - {resource: 'vocabularies', ids: ['event_calendars']}, - {resource: 'agenda'}, - ]; + if (this.state.loading) { + return null; + } - return ( - - {(resourcesResponse) => { - const calendars = resourcesResponse[0] as IRestApiResponse; - const agendas = resourcesResponse[1] as IRestApiResponse; + const {agendas} = this.state; + const calendars = superdesk.entities.vocabulary.getAll().get('event_calendars').items; + const {gettext} = superdesk.localization; - const calendarsNames: string = calendars._items[0].items - .filter((calendar) => ( - (this.props.rule.actions.extra?.calendars ?? []).includes(calendar.qcode) - )) - .map((calendar) => getVocabularyItemFieldTranslated( - calendar, - 'name', - getUserInterfaceLanguageFromCV() - )) - .join(', '); - const agendaNames: string = agendas._items - .filter((agenda) => ( - (this.props.rule.actions.extra?.agendas ?? []).includes(agenda._id) - )) - .map((agenda) => agenda.name) - .join(', '); + const calendarsNames: string = calendars + .filter((calendar) => ( + (this.props.rule.actions.extra?.calendars ?? []).includes(calendar.qcode) + )) + .map((calendar) => getVocabularyItemFieldTranslated( + calendar, + 'name', + getUserInterfaceLanguageFromCV() + )) + .join(', '); + const agendaNames: string = agendas + .filter((agenda) => ( + (this.props.rule.actions.extra?.agendas ?? []).includes(agenda._id) + )) + .map((agenda) => agenda.name) + .join(', '); - return ( - -
- - {gettext('Post Items')}: - - - {this.props.rule.actions.extra?.autopost === true ? - gettext('On') : - gettext('Off') - } - -
-
- - {gettext('Agendas')}: - - - {agendaNames} - -
-
- - {gettext('Calendars')}: - - - {calendarsNames} - -
-
- ); - }} -
+ return ( + +
+ + {gettext('Post Items')}: + + + {this.props.rule.actions.extra?.autopost === true ? + gettext('On') : + gettext('Off') + } + +
+
+ + {gettext('Agendas')}: + + + {agendaNames} + +
+
+ + {gettext('Calendars')}: + + + {calendarsNames} + +
+
); } }