Add component category edit to allow renaming

This commit is contained in:
Alexandre Bodin 2019-10-29 14:43:47 +01:00
parent d405285fae
commit 7d0c798d90
7 changed files with 115 additions and 6 deletions

View File

@ -29,4 +29,4 @@
"via": "menusections"
}
}
}
}

View File

@ -7,7 +7,10 @@
},
"options": {
"increments": true,
"timestamps": ["created_at", "updated_at"],
"timestamps": [
"created_at",
"updated_at"
],
"comment": ""
},
"attributes": {
@ -35,12 +38,21 @@
"collection": "category"
},
"price_range": {
"enum": ["very_cheap", "cheap", "average", "expensive", "very_expensive"],
"enum": [
"very_cheap",
"cheap",
"average",
"expensive",
"very_expensive"
],
"type": "enumeration"
},
"body": {
"type": "dynamiczone",
"components": ["default.closingperiod", "default.restaurantservice"]
"components": [
"default.closingperiod",
"default.restaurantservice"
]
},
"description": {
"type": "richtext",
@ -71,4 +83,4 @@
"type": "text"
}
}
}
}

View File

@ -22,4 +22,4 @@
"type": "component"
}
}
}
}

View File

@ -87,6 +87,14 @@
"config": {
"policies": []
}
},
{
"method": "PUT",
"path": "/component-categories/:name",
"handler": "ComponentCategories.editCategory",
"config": {
"policies": []
}
}
]
}

View File

@ -0,0 +1,27 @@
'use strict';
const validateComponentCategory = require('./validation/componentCategory');
module.exports = {
async editCategory(ctx) {
const { body } = ctx.request;
const service =
strapi.plugins['content-type-builder'].services.componentcategories;
try {
await validateComponentCategory(body);
} catch (error) {
return ctx.send({ error }, 400);
}
const { name } = ctx.params;
strapi.reload.isWatching = false;
await service.editCategory(name, body);
strapi.reload();
ctx.send({ name: body.name });
},
};

View File

@ -0,0 +1,25 @@
'use strict';
const yup = require('yup');
const formatYupErrors = require('./yup-formatter');
const { isValidName } = require('./common');
module.exports = data => {
return componentCategorySchema
.validate(data, {
strict: true,
abortEarly: false,
})
.catch(error => Promise.reject(formatYupErrors(error)));
};
const componentCategorySchema = yup
.object({
name: yup
.string()
.min(3)
.test(isValidName)
.required('name.required'),
})
.noUnknown();

View File

@ -0,0 +1,37 @@
'use strict';
const { join } = require('path');
const fse = require('fs-extra');
module.exports = {
async editCategory(name, infos) {
const componentsDir = join(strapi.dir, 'components');
// don't do anything the name doesn't change
if (name === infos.name) return;
if (await fse.pathExists(join(componentsDir, infos.name))) {
throw strapi.errors.badRequest('Name already taken');
}
const componentService =
strapi.plugins['content-type-builder'].services.components;
const promises = Object.keys(strapi.components)
.filter(uid => {
const [category] = uid.split('.');
return category === name;
})
.map(uid => {
const [, name] = uid.split('.');
return componentService.updateComponentInModels(
uid,
`${infos.name}.${name}`
);
});
await Promise.all(promises);
await fse.move(join(componentsDir, name), join(componentsDir, infos.name));
},
};