contentful-management
Version:
Client for Contentful's Content Management API
46 lines (44 loc) • 1.4 kB
JavaScript
import copy from 'fast-copy';
import { freezeSys, toPlainObject } from 'contentful-sdk-core';
import enhanceWithMethods from '../enhance-with-methods';
import { wrapCollection } from '../common-utils';
/**
* @private
*/
function createEditorInterfaceApi(makeRequest) {
return {
update: function () {
const self = this;
const raw = self.toPlainObject();
return makeRequest({
entityType: 'EditorInterface',
action: 'update',
params: {
spaceId: self.sys.space.sys.id,
environmentId: self.sys.environment.sys.id,
contentTypeId: self.sys.contentType.sys.id
},
payload: raw
}).then(response => wrapEditorInterface(makeRequest, response));
},
getControlForField: function (fieldId) {
const self = this;
const result = (self.controls || []).filter(control => {
return control.fieldId === fieldId;
});
return result && result.length > 0 ? result[0] : null;
}
};
}
/**
* @private
*/
export function wrapEditorInterface(makeRequest, data) {
const editorInterface = toPlainObject(copy(data));
const editorInterfaceWithMethods = enhanceWithMethods(editorInterface, createEditorInterfaceApi(makeRequest));
return freezeSys(editorInterfaceWithMethods);
}
/**
* @private
*/
export const wrapEditorInterfaceCollection = wrapCollection(wrapEditorInterface);