Skip to content

Commit

Permalink
Fix ingest config (#2120)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaskikutis committed Nov 5, 2024
1 parent 4914e29 commit d7abac0
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as React from 'react';
import {
IIngestRuleHandlerEditorProps,
ILiveResourcesProps,
IRestApiResponse,
IVocabulary,
IVocabularyItem
} from 'superdesk-api';
import {IAgenda} from '../../../interfaces';
Expand All @@ -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,
Expand All @@ -22,12 +19,27 @@ interface IExtraAttributes {

type IProps = IIngestRuleHandlerEditorProps<IExtraAttributes>;

export class AutopostIngestRuleEditor extends React.PureComponent<IProps> {
interface IStateLoading {
loading: true;
}

interface IStateLoaded {
loading: false;
agendas: Array<IAgenda>;
}

type IState = IStateLoading | IStateLoaded;

export class AutopostIngestRuleEditor extends React.PureComponent<IProps, IState> {
constructor(props: IProps) {
super(props);

this.updateAttributes = this.updateAttributes.bind(this);
this.updateAutopostValue = this.updateAutopostValue.bind(this);

this.state = {
loading: true,
};
}

updateAttributes<T extends keyof IExtraAttributes>(field: T, value: IExtraAttributes[T]) {
Expand All @@ -47,12 +59,26 @@ export class AutopostIngestRuleEditor extends React.PureComponent<IProps> {
this.updateAttributes('autopost', value);
}

componentDidMount(): void {
superdesk.httpRequestJsonLocal<IRestApiResponse<IAgenda>>({
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 (
<div>
Expand All @@ -61,38 +87,31 @@ export class AutopostIngestRuleEditor extends React.PureComponent<IProps> {
value={this.props.rule.actions.extra?.autopost === true}
onChange={this.updateAutopostValue}
/>
<WithLiveResources resources={resources}>{(resourcesResponse) => {
const calendars = resourcesResponse[0] as IRestApiResponse<IVocabulary>;
const agendas = resourcesResponse[1] as IRestApiResponse<IAgenda>;

return (
<React.Fragment>
<EditorFieldVocabulary
item={this.props.rule.actions.extra ?? {}}
field="calendars"
label={gettext('Calendars')}
defaultValue={[]}
onChange={this.updateAttributes}
options={calendars._items[0].items.filter((item) => (
item.is_active !== false
))}
valueAsString={true}
/>
<EditorFieldVocabulary
item={this.props.rule.actions.extra ?? {}}
field="agendas"
label={gettext('Agendas')}
defaultValue={[]}
onChange={this.updateAttributes}
options={agendas._items.filter((item) => (
item.is_enabled !== false
))}
valueAsString={true}
valueKey="_id"
/>
</React.Fragment>
);
}}</WithLiveResources>

<EditorFieldVocabulary
item={this.props.rule.actions.extra ?? {}}
field="calendars"
label={gettext('Calendars')}
defaultValue={[]}
onChange={this.updateAttributes}
options={calendars.filter((item) => (
item.is_active !== false
))}
valueAsString={true}
/>

<EditorFieldVocabulary
item={this.props.rule.actions.extra ?? {}}
field="agendas"
label={gettext('Agendas')}
defaultValue={[]}
onChange={this.updateAttributes}
options={agendas.filter((item) => (
item.is_enabled !== false
))}
valueAsString={true}
valueKey="_id"
/>
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<{
Expand All @@ -13,70 +12,94 @@ type IProps = IIngestRuleHandlerPreviewProps<{
calendars?: Array<string>;
}>;

export class AutopostIngestRulePreview extends React.PureComponent<IProps> {
interface IStateLoading {
loading: true;
}

interface IStateLoaded {
loading: false;
agendas: Array<IAgenda>;
}

type IState = IStateLoading | IStateLoaded;

export class AutopostIngestRulePreview extends React.PureComponent<IProps, IState> {
constructor(props: IProps) {
super(props);

this.state = {
loading: true,
};
}

componentDidMount(): void {
superdesk.httpRequestJsonLocal<IRestApiResponse<IAgenda>>({
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 (
<WithLiveResources resources={resources}>
{(resourcesResponse) => {
const calendars = resourcesResponse[0] as IRestApiResponse<IVocabulary>;
const agendas = resourcesResponse[1] as IRestApiResponse<IAgenda>;
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 (
<React.Fragment>
<div className="list-row">
<span className="text-label text-label--auto">
{gettext('Post Items')}:
</span>
<span className="list-row__item">
{this.props.rule.actions.extra?.autopost === true ?
gettext('On') :
gettext('Off')
}
</span>
</div>
<div className="list-row">
<span className="text-label text-label--auto">
{gettext('Agendas')}:
</span>
<span className="list-row__item">
{agendaNames}
</span>
</div>
<div className="list-row">
<span className="text-label text-label--auto">
{gettext('Calendars')}:
</span>
<span className="list-row__item">
{calendarsNames}
</span>
</div>
</React.Fragment>
);
}}
</WithLiveResources>
return (
<React.Fragment>
<div className="list-row">
<span className="text-label text-label--auto">
{gettext('Post Items')}:
</span>
<span className="list-row__item">
{this.props.rule.actions.extra?.autopost === true ?
gettext('On') :
gettext('Off')
}
</span>
</div>
<div className="list-row">
<span className="text-label text-label--auto">
{gettext('Agendas')}:
</span>
<span className="list-row__item">
{agendaNames}
</span>
</div>
<div className="list-row">
<span className="text-label text-label--auto">
{gettext('Calendars')}:
</span>
<span className="list-row__item">
{calendarsNames}
</span>
</div>
</React.Fragment>
);
}
}

0 comments on commit d7abac0

Please sign in to comment.