Skip to content

Commit

Permalink
Categories field implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc committed Dec 13, 2024
1 parent 0e8f972 commit f0f16ae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {IDropdownConfigVocabulary, IAuthoringFieldV2, IVocabularyItem} from 'superdesk-api';
import {superdeskApi} from '../../../superdeskApi';
import {IFieldGetter} from '.';

export const getCategoriesField: IFieldGetter = () => ({
fieldId: 'anpa_category',
getField: ({id, required}) => {
const fieldConfig: IDropdownConfigVocabulary = {
source: 'vocabulary',
vocabularyId: 'categories',
multiple: true,
required: required,
};

const field: IAuthoringFieldV2 = {
id: id,
name: superdeskApi.localization.gettext('Categories'),
fieldType: 'dropdown',
fieldConfig: fieldConfig,
};

return field;
},
storageAdapter: {
storeValue: (item, operationalValue: Array<string>) => {
const vocabulary = superdeskApi.entities.vocabulary.getAll().get('categories');
const vocabularyItems = new Map<IVocabularyItem['qcode'], IVocabularyItem>(
vocabulary.items.map((item) => [item.qcode, item]),
);

return {
...item,
anpa_category: operationalValue.map((qcode) => vocabularyItems.get(qcode)),
};
},
retrieveStoredValue: (item, fieldId) => (item[fieldId] ?? []).map(({qcode}) => qcode),
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {getDateTimeField} from './date-time-config';
import {IFieldDefinitions, IFieldDefinition} from './interfaces';
import {getTextFieldConfig} from './text-field-config';
import {getPlaceField} from './place-field';
import {getCategoriesField} from './category-field';
import {getAgendasField} from './agendas-field';

export function getFieldDefinitions(): IFieldDefinitions {
Expand Down Expand Up @@ -66,6 +67,7 @@ export function getFieldDefinitions(): IFieldDefinitions {
},
getPlaceField(),
getAgendasField(),
getCategoriesField(),
{
fieldId: 'coverages',
getField: ({id, required}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {flatMap} from 'lodash';
import {planningApi} from '../../superdeskApi';
import {getEditorFormGroupsFromProfile} from '../../utils/contentProfiles';

Expand All @@ -14,13 +13,18 @@ interface ICustomVocabularyField extends IBaseField<'custom_vocabulary'> {

type IFieldConverted = IBaseField<'normal'> | ICustomVocabularyField;

const FIELDS_TO_FILTER = [
'associated_event',
];

/**
* A function that handles planning profile field types so they can be used in authoring react.
*/
export const getPlanningProfileFields = (): Array<IFieldConverted> => {
const planningProfile = planningApi.contentProfiles.get('planning');
const planningGroups = getEditorFormGroupsFromProfile(planningProfile);
const planningFieldIds = Object.values(planningGroups).flatMap((x) => x.fields);
const planningFieldIds = Object.values(planningGroups).flatMap((x) => x.fields)
.filter((x) => !FIELDS_TO_FILTER.includes(x));
const convertedFieldIds: Array<IFieldConverted> = [];

for (const fieldId of planningFieldIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
IStorageAdapter,
} from 'superdesk-api';
import {superdeskApi} from '../../superdeskApi';
import {getFieldDefinitions} from './field-definitions';
import {getFieldDefinitions} from './field-definitions/index';

export const storageAdapterPlanningItem: IStorageAdapter<IPlanningItem> = {
storeValue: (value, fieldId, item, config, fieldType) => {
Expand Down

0 comments on commit f0f16ae

Please sign in to comment.