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 12, 2024
1 parent 124fbbf commit a5b7c74
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 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,9 @@ 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';

export type IFieldGetter = () => IFieldDefinition;

export function getFieldDefinitions(): IFieldDefinitions {
const {gettext} = superdeskApi.localization;
Expand Down Expand Up @@ -63,7 +66,8 @@ export function getFieldDefinitions(): IFieldDefinitions {
return field;
},
},
getPlaceField(gettext),
getPlaceField(),
getCategoriesField(),
{
fieldId: 'coverages',
getField: ({id, required}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {IDropdownConfigVocabulary, IAuthoringFieldV2, IVocabularyItem} from 'superdesk-api';
import {superdeskApi} from '../../../superdeskApi';
import {IFieldGetter} from '.';

export const getPlaceField = (gettext: (val: string) => string) => ({
export const getPlaceField: IFieldGetter = () => ({
fieldId: 'place',
getField: ({id, required}) => {
const fieldConfig: IDropdownConfigVocabulary = {
Expand All @@ -13,7 +14,7 @@ export const getPlaceField = (gettext: (val: string) => string) => ({

const field: IAuthoringFieldV2 = {
id: id,
name: gettext('Place'),
name: superdeskApi.localization.gettext('Place'),
fieldType: 'dropdown',
fieldConfig: fieldConfig,
};
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 './profile';
import {getFieldDefinitions} from './field-definitions/index';

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

0 comments on commit a5b7c74

Please sign in to comment.