feat: ability to update category handles

#12434
This commit is contained in:
Julian Lam
2024-03-22 12:59:24 -04:00
parent 3cc99a178e
commit 9dc20d0c54
4 changed files with 33 additions and 0 deletions

View File

@@ -7,6 +7,8 @@
"privileges": "Privileges",
"back-to-categories": "Back to categories",
"name": "Category Name",
"handle": "Category Handle",
"handle.help": "Your category handle is used as a representation of this category across other networks, similar to a username. A category handle must not match an existing username or user group.",
"description": "Category Description",
"bg-color": "Background Colour",
"text-color": "Text Colour",

View File

@@ -264,6 +264,7 @@
"topic-event-unrecognized": "Topic event '%1' unrecognized",
"category.handle-taken": "Category handle is already taken, please choose another.",
"cant-set-child-as-parent": "Can't set child as parent category",
"cant-set-self-as-parent": "Can't set self as parent category",

View File

@@ -49,6 +49,8 @@ module.exports = function (Categories) {
return await updateTagWhitelist(cid, value);
} else if (key === 'name') {
return await updateName(cid, value);
} else if (key === 'handle') {
return await updateHandle(cid, value);
} else if (key === 'order') {
return await updateOrder(cid, value);
}
@@ -142,4 +144,22 @@ module.exports = function (Categories) {
await db.sortedSetAdd('categories:name', 0, `${newName.slice(0, 200).toLowerCase()}:${cid}`);
await db.setObjectField(`category:${cid}`, 'name', newName);
}
async function updateHandle(cid, handle) {
const existing = await Categories.getCategoryField(cid, 'handle');
if (existing === handle) {
return;
}
const taken = await meta.slugTaken(handle);
if (taken) {
throw new Error('[[error:category.handle-taken]]');
}
await Promise.all([
db.setObjectField(`category:${cid}`, 'handle', handle),
db.sortedSetRemove('categoryhandle:cid', existing),
db.sortedSetAdd('categoryhandle:cid', cid, handle),
]);
}
};

View File

@@ -19,6 +19,16 @@
<input id="cid-{category.cid}-name" type="text" class="form-control" data-name="name" value="{category.name}" />
</div>
<div class="mb-3">
<label class="form-label" for="cid-{category.cid}-handle">
[[admin/manage/categories:handle]]
</label>
<input id="cid-{category.cid}-handle" type="text" class="form-control" data-name="handle" value="{category.handle}" />
<p class="form-text">
[[admin/manage/categories:handle.help]]
</p>
</div>
<div class="mb-3">
<label class="form-label" for="cid-{category.cid}-description">
[[admin/manage/categories:description]]