Skip to content

Commit

Permalink
subject field implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc committed Dec 13, 2024
1 parent 2a71ecf commit e9761ca
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +67,7 @@ export function getFieldDefinitions(): IFieldDefinitions {
},
getPlaceField(),
getAgendasField(),
getSubjectField(),
{
fieldId: 'coverages',
getField: ({id, required}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ISubjectCode['qcode']>) => {
interface IStorageFormat {
qcode: string;
name: string;
parent?: string;
}

const allSubjects: Array<ISubjectCode> = (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;
}),
};
},
}
};
}

0 comments on commit e9761ca

Please sign in to comment.