2019-07-19 09:58:38 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-07-19 18:14:13 +02:00
|
|
|
const { createModelConfigurationSchema } = require('./validation');
|
2019-11-14 12:21:21 +01:00
|
|
|
const contentTypeService = require('../services/ContentTypes');
|
|
|
|
const componentService = require('../services/Components');
|
2019-07-19 18:14:13 +02:00
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
module.exports = {
|
|
|
|
/**
|
2019-10-22 18:01:03 +02:00
|
|
|
* Returns the list of available components
|
2019-07-19 09:58:38 +02:00
|
|
|
*/
|
2019-10-22 18:01:03 +02:00
|
|
|
async listComponents(ctx) {
|
|
|
|
const data = Object.keys(strapi.components).map(uid => {
|
2019-11-14 12:21:21 +01:00
|
|
|
return contentTypeService.formatContentType(strapi.components[uid]);
|
2019-07-24 11:51:35 +02:00
|
|
|
});
|
2019-11-14 12:21:21 +01:00
|
|
|
|
2019-07-19 18:14:13 +02:00
|
|
|
ctx.body = { data };
|
|
|
|
},
|
2019-07-19 09:58:38 +02:00
|
|
|
/**
|
2019-10-22 18:01:03 +02:00
|
|
|
* Returns a component configuration.
|
2019-07-19 09:58:38 +02:00
|
|
|
* It includes
|
|
|
|
* - schema
|
|
|
|
* - content-manager layouts (list,edit)
|
|
|
|
* - content-manager settings
|
|
|
|
* - content-manager metadata (placeholders, description, label...)
|
|
|
|
*/
|
2019-10-22 18:01:03 +02:00
|
|
|
async findComponent(ctx) {
|
2019-07-19 18:14:13 +02:00
|
|
|
const { uid } = ctx.params;
|
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
const component = strapi.components[uid];
|
2019-07-19 18:14:13 +02:00
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
if (!component) {
|
|
|
|
return ctx.notFound('component.notFound');
|
2019-07-19 18:14:13 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 12:21:21 +01:00
|
|
|
const data = await componentService.getComponentInformations(uid);
|
2019-07-19 18:14:13 +02:00
|
|
|
|
|
|
|
ctx.body = { data };
|
|
|
|
},
|
2019-07-19 09:58:38 +02:00
|
|
|
/**
|
2019-10-22 18:01:03 +02:00
|
|
|
* Updates a component configuration
|
2019-07-19 09:58:38 +02:00
|
|
|
* You can only update the content-manager settings: (use the content-type-builder to update attributes)
|
|
|
|
* - content-manager layouts (list,edit)
|
|
|
|
* - content-manager settings
|
|
|
|
* - content-manager metadata (placeholders, description, label...)
|
|
|
|
*/
|
2019-10-22 18:01:03 +02:00
|
|
|
async updateComponent(ctx) {
|
2019-07-19 18:14:13 +02:00
|
|
|
const { uid } = ctx.params;
|
|
|
|
const { body } = ctx.request;
|
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
const component = strapi.components[uid];
|
2019-07-19 18:14:13 +02:00
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
if (!component) {
|
|
|
|
return ctx.notFound('component.notFound');
|
2019-07-19 18:14:13 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 12:21:21 +01:00
|
|
|
const schema = contentTypeService.formatContentTypeSchema(component);
|
2019-07-19 18:14:13 +02:00
|
|
|
let input;
|
|
|
|
try {
|
2019-10-22 18:01:03 +02:00
|
|
|
input = await createModelConfigurationSchema(component, schema).validate(
|
2019-07-25 08:02:10 +02:00
|
|
|
body,
|
|
|
|
{
|
|
|
|
abortEarly: false,
|
|
|
|
stripUnknown: true,
|
|
|
|
strict: true,
|
|
|
|
}
|
|
|
|
);
|
2019-07-19 18:14:13 +02:00
|
|
|
} catch (error) {
|
|
|
|
return ctx.badRequest(null, {
|
|
|
|
name: 'validationError',
|
|
|
|
errors: error.errors,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
await componentService.setConfiguration(uid, input);
|
2019-07-19 18:14:13 +02:00
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
const configurations = await componentService.getConfiguration(uid);
|
2019-07-19 18:14:13 +02:00
|
|
|
|
|
|
|
const data = {
|
|
|
|
uid,
|
2019-07-25 08:02:10 +02:00
|
|
|
schema,
|
2019-07-19 18:14:13 +02:00
|
|
|
...configurations,
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.body = { data };
|
|
|
|
},
|
2019-07-19 09:58:38 +02:00
|
|
|
};
|