From e9761ca96eb6011006b99d53d194fcb7fa68a76c Mon Sep 17 00:00:00 2001 From: Konstantin Markov Date: Fri, 13 Dec 2024 15:34:39 +0200 Subject: [PATCH] subject field implementation --- .../field-definitions/index.ts | 2 + .../field-definitions/subject.ts | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 client/components/planning-editor-standalone/field-definitions/subject.ts diff --git a/client/components/planning-editor-standalone/field-definitions/index.ts b/client/components/planning-editor-standalone/field-definitions/index.ts index b71d36fe6..60b13581c 100644 --- a/client/components/planning-editor-standalone/field-definitions/index.ts +++ b/client/components/planning-editor-standalone/field-definitions/index.ts @@ -12,6 +12,7 @@ import {IFieldDefinitions, IFieldDefinition} from './interfaces'; import {getTextFieldConfig} from './text-field-config'; import {getPlaceField} from './place-field'; import {getAgendasField} from './agendas-field'; +import {getSubjectField} from './subject'; export function getFieldDefinitions(): IFieldDefinitions { const {gettext} = superdeskApi.localization; @@ -66,6 +67,7 @@ export function getFieldDefinitions(): IFieldDefinitions { }, getPlaceField(), getAgendasField(), + getSubjectField(), { fieldId: 'coverages', getField: ({id, required}) => { diff --git a/client/components/planning-editor-standalone/field-definitions/subject.ts b/client/components/planning-editor-standalone/field-definitions/subject.ts new file mode 100644 index 000000000..6281ab72f --- /dev/null +++ b/client/components/planning-editor-standalone/field-definitions/subject.ts @@ -0,0 +1,63 @@ +import { + IAuthoringFieldV2, + ISubjectCode, + IDropdownConfigManualSource, +} from 'superdesk-api'; +import {gettext} from 'core/utils'; +import {IFieldDefinition} from './interfaces'; +import {planningApi} from '../../../superdeskApi'; + +export function getSubjectField(): IFieldDefinition { + return { + fieldId: 'subject', + getField: ({id, required}) => { + const fieldConfig: IDropdownConfigManualSource = { + source: 'manual-entry', + options: (planningApi.redux.store.getState().subjects ?? []) + .map((x) => ({id: x.qcode, label: x.name, parent: x.parent})), + roundCorners: true, + type: 'text', + canSelectBranchWithChildren: true, + multiple: true, + required: required, + }; + + const fieldV2: IAuthoringFieldV2 = { + id: 'subject', + name: gettext('Subject'), + fieldType: 'dropdown', + fieldConfig: fieldConfig, + }; + + return fieldV2; + }, + storageAdapter: { + retrieveStoredValue: (item) => { + return (item.subject ?? []).map(({qcode}) => qcode); + }, + storeValue: (item, operationalValue: Array) => { + interface IStorageFormat { + qcode: string; + name: string; + parent?: string; + } + + const allSubjects: Array = (planningApi.redux.store.getState().subjects ?? []) + .filter((x) => operationalValue.includes(x.qcode)); + + return { + ...item, + subject: allSubjects.map(({qcode, name, parent}) => { + var itemToStore: IStorageFormat = {qcode, name, parent}; + + if (parent != null) { + itemToStore.parent = parent; + } + + return itemToStore; + }), + }; + }, + } + }; +}