Skip to content

Commit

Permalink
feat: ability to update category handles
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Mar 22, 2024
1 parent 3cc99a1 commit 9dc20d0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions public/language/en-GB/admin/manage/categories.json
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions public/language/en-GB/error.json
Expand Up @@ -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",

Expand Down
20 changes: 20 additions & 0 deletions src/categories/update.js
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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),
]);
}
};
10 changes: 10 additions & 0 deletions src/views/admin/manage/category.tpl
Expand Up @@ -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]]
Expand Down

0 comments on commit 9dc20d0

Please sign in to comment.