mirror of
https://github.com/strapi/strapi.git
synced 2025-08-04 14:56:22 +00:00
Init nested component in component
This commit is contained in:
parent
f2b3fec654
commit
f61afa45f4
@ -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(
|
||||||
|
@ -32,19 +32,28 @@ const VALID_TYPES = [
|
|||||||
'component',
|
'component',
|
||||||
];
|
];
|
||||||
|
|
||||||
const createComponentSchema = () => {
|
const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, {
|
||||||
return createSchema(VALID_TYPES, VALID_RELATIONS, {
|
|
||||||
modelType: modelTypes.COMPONENT,
|
modelType: modelTypes.COMPONENT,
|
||||||
}).shape({
|
}).shape({
|
||||||
icon: yup
|
icon: yup
|
||||||
.string()
|
.string()
|
||||||
.nullable()
|
.nullable()
|
||||||
.test(isValidName),
|
.test(isValidName)
|
||||||
|
.required(),
|
||||||
category: yup
|
category: yup
|
||||||
.string()
|
.string()
|
||||||
.nullable()
|
.nullable()
|
||||||
.test(isValidName),
|
.test(isValidName)
|
||||||
});
|
.required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const createComponentSchema = () => {
|
||||||
|
return yup
|
||||||
|
.object({
|
||||||
|
component: componentSchema.required().noUnknown(),
|
||||||
|
components: yup.array().of(componentSchema),
|
||||||
|
})
|
||||||
|
.noUnknown();
|
||||||
};
|
};
|
||||||
|
|
||||||
const validateComponentInput = data => {
|
const validateComponentInput = data => {
|
||||||
@ -58,13 +67,25 @@ 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.component.attributes).forEach(attribute => {
|
||||||
|
if (data.component.attributes[attribute].default === '') {
|
||||||
|
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 => {
|
Object.keys(data.attributes).forEach(attribute => {
|
||||||
if (data.attributes[attribute].default === '') {
|
if (data.attributes[attribute].default === '') {
|
||||||
data.attributes[attribute].default = undefined;
|
data.attributes[attribute].default = undefined;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return createComponentSchema()
|
return createComponentSchema()
|
||||||
.validate(data, {
|
.validate(data, {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user