Skip to content

Commit

Permalink
Refactor structure & categories field (#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc authored Dec 13, 2024
1 parent 8bd4c20 commit d0803e2
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 215 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {IAgenda} from 'interfaces';
import {IDropdownConfigManualSource, IAuthoringFieldV2} from 'superdesk-api';
import {planningApi} from 'superdeskApi';
import {gettext} from 'utils';
import {IFieldDefinition} from './interfaces';

export const getAgendasField = (): IFieldDefinition => ({
fieldId: 'agendas',
getField: ({id, required}) => {
const fieldConfig: IDropdownConfigManualSource = {
source: 'manual-entry',
options: ((planningApi.redux.store.getState().agenda.agendas ?? []) as Array<IAgenda>)
.filter((item) => item.is_enabled)
.map((item) => ({
id: item._id,
label: item.name,
})),
roundCorners: true,
type: 'text',
multiple: true,
required: required,
};

const field: IAuthoringFieldV2 = {
id: id,
name: gettext('Agendas'),
fieldType: 'dropdown',
fieldConfig: fieldConfig,
};

return field;
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {IAuthoringFieldV2, IDateTimeFieldConfig} from 'superdesk-api';

export function getDateTimeField(options: {id: string; label: string, required: boolean}): IAuthoringFieldV2 {
const config: IDateTimeFieldConfig = {
allowSeconds: false,
};

const field: IAuthoringFieldV2 = {
id: options.id,
name: options.label,
fieldType: 'datetime',
fieldConfig: {
...config,
required: options.required,
},
};

return field;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {
IAttachmentsFieldConfig,
} from '../../../planning-extension/src/authoring-react-fields/planning-attachments/interfaces';
import {
IAuthoringFieldV2,
ICommonFieldConfig,
} from 'superdesk-api';
import {superdeskApi} from '../../../superdeskApi';
import {getCustomVocabularyFields} from '../field-adapters/custom-vocabularies';
import {getDateTimeField} from './date-time-config';
import {IFieldDefinitions, IFieldDefinition} from './interfaces';
import {getTextFieldConfig} from './text-field-config';
import {getPlaceField} from './place-field';
import {getAgendasField} from './agendas-field';

export function getFieldDefinitions(): IFieldDefinitions {
const {gettext} = superdeskApi.localization;
const result: Array<IFieldDefinition> = [
{
fieldId: 'ednote',
getField: ({required, id}) => getTextFieldConfig({id: id, label: gettext('Ed Note'), required: required}),
},
{
fieldId: 'internal_note',
getField: ({required, id}) =>
getTextFieldConfig({id: id, label: gettext('Internal Note'), required: required}),
},
{
fieldId: 'name',
getField: ({required, id}) => getTextFieldConfig({id: id, label: gettext('Name'), required: required}),
},
{
fieldId: 'slugline',
getField: ({required, id}) => getTextFieldConfig({id: id, label: gettext('Slugline'), required: required}),
},
{
fieldId: 'description_text',
getField: ({required, id}) =>
getTextFieldConfig({id: id, label: gettext('Description'), required: required}),
},
{
fieldId: 'headline',
getField: ({required, id}) => getTextFieldConfig({id: id, label: gettext('Headline'), required: required}),
},
{
fieldId: 'planning_date',
getField: ({required, id}) =>
getDateTimeField({id: id, label: gettext('Planning date'), required: required}),
},
{
fieldId: 'files',
getField: ({required, id}) => {
const fieldConfig: IAttachmentsFieldConfig = {
required,
};

const field: IAuthoringFieldV2 = {
id: id,
name: gettext('Attached files'),
fieldType: 'files',
fieldConfig: fieldConfig,
};

return field;
},
},
getPlaceField(),
getAgendasField(),
{
fieldId: 'coverages',
getField: ({id, required}) => {
const fieldConfig: ICommonFieldConfig = {
required,
};

const field: IAuthoringFieldV2 = {
id: id,
name: gettext('Coverages'),
fieldType: 'coverages',
fieldConfig: fieldConfig,
};

return field;
},
}
];

result.push(
...getCustomVocabularyFields(),
);

const resultObj = result.reduce((acc, item) => {
acc[item.fieldId] = item;

return acc;
}, {} as IFieldDefinitions);

return resultObj;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {IAuthoringFieldV2} from 'superdesk-api';

export interface IFieldDefinition {
fieldId: string;
getField: (options: {required: boolean, id: string}) => IAuthoringFieldV2;
storageAdapter?: {
storeValue: <T extends IPlanningItem>(item: T, operationalValue: unknown) => T; // returns stored value
retrieveStoredValue:
<T extends IPlanningItem>(item: T, fieldId: string) => unknown; // returns operational value
};
}

export type IFieldDefinitions = {[fieldId: string]: IFieldDefinition};
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 {IFieldDefinition} from './interfaces';

export const getPlaceField = (): IFieldDefinition => ({
fieldId: 'place',
getField: ({id, required}) => {
const fieldConfig: IDropdownConfigVocabulary = {
source: 'vocabulary',
vocabularyId: 'locators',
multiple: true,
required: required,
};

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

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

return {
...item,
place: operationalValue.map((qcode) => vocabularyItems.get(qcode)),
};
},
retrieveStoredValue: (item, fieldId) => item[fieldId].map(({qcode}) => qcode),
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {IAuthoringFieldV2, IEditor3Config} from 'superdesk-api';

export function getTextFieldConfig(options: {id: string; label: string, required: boolean}): IAuthoringFieldV2 {
const editor3ConfigWithoutFormatting: IEditor3Config = {
editorFormat: [],
minLength: undefined,
maxLength: undefined,
cleanPastedHtml: false,
singleLine: true,
disallowedCharacters: [],
showStatistics: false,
width: 100,
};

const field: IAuthoringFieldV2 = {
id: options.id,
name: options.label,
fieldType: 'editor3',
fieldConfig: {
...editor3ConfigWithoutFormatting,
required: options.required,
},
};

return field;
}
Loading

0 comments on commit d0803e2

Please sign in to comment.