superdesk/superdesk-client-core

View on GitHub
scripts/apps/authoring-react/field-adapters/package_items.ts

Summary

Maintainability
A
50 mins
Test Coverage
import {IArticle, IAuthoringFieldV2, IFieldAdapter, IEditor3Config, IPackageItemsValueOperational} from 'superdesk-api';
import {gettext} from 'core/utils';
import {PACKAGE_ITEMS_FIELD_ID} from '../fields/package-items';

export const package_items: IFieldAdapter<IArticle> = {
    getFieldV2: () => {
        const fieldV2: IAuthoringFieldV2 = {
            id: 'groups',
            name: gettext('Package items'),
            fieldType: PACKAGE_ITEMS_FIELD_ID,
            fieldConfig: {},
        };

        return fieldV2;
    },
    retrieveStoredValue: (item: IArticle): IPackageItemsValueOperational => {
        return item.groups[1].refs;
    },
    storeValue: (value: IPackageItemsValueOperational, article: IArticle, config: IEditor3Config) => {
        return {
            ...article,
            groups: [
                {
                    'role': 'grpRole:NEP',
                    'refs': [
                        {
                            'idRef': 'main',
                            'label': 'main',
                        },
                    ],
                    'id': 'root',
                },
                {
                    refs: value,
                    'id': 'main',
                    'role': 'grpRole:main',
                },
            ],
        };
    },
};