-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Planning authoring-react fields (#2147)
- Loading branch information
Showing
10 changed files
with
321 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
client/components/planning-editor-standalone/field-adapters/custom-vocabularies.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import {IDropdownConfigVocabulary, IAuthoringFieldV2, ISubject, IVocabularyItem} from 'superdesk-api'; | ||
import {superdeskApi} from '../../../superdeskApi'; | ||
import {IFieldDefinition} from '../profile'; | ||
import {getPlanningProfileFields} from '../profile-fields'; | ||
|
||
export const getCustomVocabularyFields = () => { | ||
const customVocabularyIds = getPlanningProfileFields() | ||
.filter((x) => x.type === 'custom_vocabulary') | ||
.map(({vocabularyId}) => vocabularyId); | ||
const result: Array<IFieldDefinition> = []; | ||
|
||
if ((customVocabularyIds?.length ?? 0) > 0) { | ||
const allVocabularies = superdeskApi.entities.vocabulary.getAll(); | ||
|
||
for (const id of customVocabularyIds) { | ||
const vocabulary = allVocabularies.get(id); | ||
|
||
result.push({ | ||
fieldId: id, | ||
getField: ({required, id: _id}) => { | ||
const fieldConfig: IDropdownConfigVocabulary = { | ||
source: 'vocabulary', | ||
vocabularyId: id, | ||
multiple: true, | ||
required: required, | ||
}; | ||
|
||
const field: IAuthoringFieldV2 = { | ||
id: _id, | ||
name: vocabulary.display_name, | ||
fieldType: 'dropdown', | ||
fieldConfig: fieldConfig, | ||
}; | ||
|
||
return field; | ||
}, | ||
storageAdapter: { | ||
storeValue: (item, operationalValue: IVocabularyItem['qcode']) => { | ||
const vocabulary = allVocabularies.get(id); | ||
const vocabItems = vocabulary.items.filter((x) => operationalValue.includes(x.qcode)); | ||
|
||
// Subfield values | ||
const itemsToSubject: Array<ISubject> = vocabItems.map((x) => ({ | ||
name: x.name, | ||
qcode: x.qcode, | ||
scheme: vocabulary._id, | ||
})); | ||
|
||
// Remove values that don't match the "subfield" ID, so there's no item duplication | ||
const restOfValues = (item.subject ?? []).filter((x) => x.scheme !== id); | ||
|
||
return { | ||
...item, | ||
subject: [ | ||
...itemsToSubject, | ||
...restOfValues, | ||
], | ||
}; | ||
}, | ||
retrieveStoredValue: (item) => { | ||
return (item.subject ?? []).filter((x) => x.scheme === id).map((x) => x.qcode); | ||
}, | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
client/components/planning-editor-standalone/profile-fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {flatMap} from 'lodash'; | ||
import {planningApi} from '../../superdeskApi'; | ||
import {getEditorFormGroupsFromProfile} from '../../utils/contentProfiles'; | ||
|
||
interface IBaseField<T> { | ||
type: T; | ||
fieldId: string; | ||
required: boolean; | ||
} | ||
|
||
interface ICustomVocabularyField extends IBaseField<'custom_vocabulary'> { | ||
vocabularyId: string; | ||
} | ||
|
||
type IFieldConverted = IBaseField<'normal'> | ICustomVocabularyField; | ||
|
||
/** | ||
* 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 convertedFieldIds: Array<IFieldConverted> = []; | ||
|
||
for (const fieldId of planningFieldIds) { | ||
const fieldSchema = planningProfile.schema[fieldId]; | ||
|
||
if (fieldSchema?.type === 'list' && ((fieldSchema.vocabularies ?? []).length > 0)) { | ||
for (const vocabId of fieldSchema.vocabularies) { | ||
convertedFieldIds.push({ | ||
type: 'custom_vocabulary', | ||
fieldId: vocabId, | ||
required: fieldSchema.required ?? false, | ||
vocabularyId: vocabId, | ||
}); | ||
} | ||
} else { | ||
convertedFieldIds.push({ | ||
type: 'normal', | ||
fieldId: fieldId, | ||
required: fieldSchema.required ?? false, | ||
}); | ||
} | ||
} | ||
|
||
return convertedFieldIds; | ||
}; |
Oops, something went wrong.