Init nested component in component

This commit is contained in:
Alexandre Bodin 2019-11-08 15:16:17 +01:00
parent f2b3fec654
commit f61afa45f4
2 changed files with 42 additions and 21 deletions

View File

@ -57,7 +57,7 @@ module.exports = {
return ctx.send({ error }, 400); return ctx.send({ error }, 400);
} }
const uid = componentService.createComponentUID(body); const uid = componentService.createComponentUID(body.component);
if (_.has(strapi.components, uid)) { if (_.has(strapi.components, uid)) {
return ctx.send({ error: 'component.alreadyExists' }, 400); return ctx.send({ error: 'component.alreadyExists' }, 400);
@ -67,7 +67,7 @@ module.exports = {
const newComponent = await componentService.createComponent({ const newComponent = await componentService.createComponent({
uid, uid,
infos: body, infos: body.component,
}); });
strapi.reload(); strapi.reload();
@ -91,12 +91,12 @@ module.exports = {
} }
try { try {
await validateUpdateComponentInput(body); await validateUpdateComponentInput(body.component);
} catch (error) { } catch (error) {
return ctx.send({ error }, 400); return ctx.send({ error }, 400);
} }
const newUID = componentService.editComponentUID(body); const newUID = componentService.editComponentUID(body.component);
if (newUID !== uid && _.has(strapi.components, newUID)) { if (newUID !== uid && _.has(strapi.components, newUID)) {
return ctx.send({ error: 'new.component.alreadyExists' }, 400); return ctx.send({ error: 'new.component.alreadyExists' }, 400);
} }
@ -105,7 +105,7 @@ module.exports = {
const updatedComponent = await componentService.updateComponent({ const updatedComponent = await componentService.updateComponent({
uid, uid,
infos: body, infos: body.component,
}); });
await componentService.updateComponentInModels( await componentService.updateComponentInModels(

View File

@ -32,19 +32,28 @@ const VALID_TYPES = [
'component', 'component',
]; ];
const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, {
modelType: modelTypes.COMPONENT,
}).shape({
icon: yup
.string()
.nullable()
.test(isValidName)
.required(),
category: yup
.string()
.nullable()
.test(isValidName)
.required(),
});
const createComponentSchema = () => { const createComponentSchema = () => {
return createSchema(VALID_TYPES, VALID_RELATIONS, { return yup
modelType: modelTypes.COMPONENT, .object({
}).shape({ component: componentSchema.required().noUnknown(),
icon: yup components: yup.array().of(componentSchema),
.string() })
.nullable() .noUnknown();
.test(isValidName),
category: yup
.string()
.nullable()
.test(isValidName),
});
}; };
const validateComponentInput = data => { const validateComponentInput = data => {
@ -58,10 +67,22 @@ const validateComponentInput = data => {
const validateUpdateComponentInput = data => { const validateUpdateComponentInput = data => {
// convert zero length string on default attributes to undefined // convert zero length string on default attributes to undefined
if (_.has(data, 'attributes')) { if (_.has(data, ['component', 'attributes'])) {
Object.keys(data.attributes).forEach(attribute => { Object.keys(data.component.attributes).forEach(attribute => {
if (data.attributes[attribute].default === '') { if (data.component.attributes[attribute].default === '') {
data.attributes[attribute].default = undefined; data.component.attributes[attribute].default = undefined;
}
});
}
if (_.has(data, 'components') && Array.isArray(data.components)) {
data.components.forEach(data => {
if (_.has(data, 'attributes') && _.has(data, 'uid')) {
Object.keys(data.attributes).forEach(attribute => {
if (data.attributes[attribute].default === '') {
data.attributes[attribute].default = undefined;
}
});
} }
}); });
} }