Skip to content

A platform for community discussion. Free, open, simple.

FIX: Simplified category creation validation and state reset (#37567)

Régis Hanol6cd48ccd234a
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index d9b5f424b35..df77e2bf8c9 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4580,6 +4580,7 @@ en:
self_lockout: "You are about to change category permissions in a way that will remove your own access. Are you sure you want to proceed?"
validations:
emoji_required: "Select an emoji."
+ icon_required: "Select an icon."
name: "Category name"
untitled: "Untitled category"
description: "Description"
diff --git a/frontend/discourse/admin/components/upsert-category/general.gjs b/frontend/discourse/admin/components/upsert-category/general.gjs
index 99038f2bbd6..f40caf75e8c 100644
--- a/frontend/discourse/admin/components/upsert-category/general.gjs
+++ b/frontend/discourse/admin/components/upsert-category/general.gjs
@@ -145,8 +145,9 @@ export default class UpsertCategoryGeneral extends Component {
}
get categoryVisibility() {
- if (this.categoryVisibilityState) {
- return this.categoryVisibilityState;
+ const state = this.categoryVisibilityState;
+ if (state && state.categoryId === this.args.category.id) {
+ return state.value;
}
if (this.isParentRestricted) {
@@ -193,7 +194,10 @@ export default class UpsertCategoryGeneral extends Component {
}));
}
- this.categoryVisibilityState = value;
+ this.categoryVisibilityState = {
+ categoryId: this.args.category.id,
+ value,
+ };
if (value === "public") {
this.#setFormPermissions([EVERYONE_FULL_PERMISSION]);
@@ -279,6 +283,16 @@ export default class UpsertCategoryGeneral extends Component {
}
}
+ @action
+ validateIcon(name, value, { addError, data }) {
+ if (data.style_type === "icon" && !value) {
+ addError(name, {
+ title: i18n("category.icon"),
+ message: i18n("category.validations.icon_required"),
+ });
+ }
+ }
+
@action
goToSecurityTab(event) {
if (event.target.tagName === "A") {
@@ -392,6 +406,7 @@ export default class UpsertCategoryGeneral extends Component {
@title={{i18n "category.icon"}}
@showTitle={{false}}
@format="large"
+ @validate={{this.validateIcon}}
as |field|
>
<field.Custom>
diff --git a/frontend/discourse/admin/controllers/edit-category/tabs.js b/frontend/discourse/admin/controllers/edit-category/tabs.js
index 32dd859c5b2..5e8e990cf2a 100644
--- a/frontend/discourse/admin/controllers/edit-category/tabs.js
+++ b/frontend/discourse/admin/controllers/edit-category/tabs.js
@@ -128,19 +128,27 @@ export default class EditCategoryTabsController extends Controller {
let hasGeneralTabErrors = false;
if (!data.name) {
+ hasGeneralTabErrors = true;
addError("name", {
title: i18n("category.name"),
message: i18n("form_kit.errors.required"),
});
- hasGeneralTabErrors = true;
}
if (data.style_type === "emoji" && !data.emoji) {
+ hasGeneralTabErrors = true;
addError("emoji", {
title: i18n("category.emoji"),
message: i18n("category.validations.emoji_required"),
});
+ }
+
+ if (data.style_type === "icon" && !data.icon) {
hasGeneralTabErrors = true;
+ addError("icon", {
+ title: i18n("category.icon"),
+ message: i18n("category.validations.icon_required"),
+ });
}
if (hasGeneralTabErrors) {
diff --git a/spec/system/page_objects/components/select_kit.rb b/spec/system/page_objects/components/select_kit.rb
index f9ae2f73854..3c38f3c80ba 100644
--- a/spec/system/page_objects/components/select_kit.rb
+++ b/spec/system/page_objects/components/select_kit.rb
@@ -129,6 +129,10 @@ module PageObjects
def unselect_by_name(name)
expanded_component.find(".selected-choice[data-name='#{name}']").click
end
+
+ def clear
+ expanded_component.all(".selected-choice").each(&:click)
+ end
end
end
end
diff --git a/spec/system/page_objects/pages/category.rb b/spec/system/page_objects/pages/category.rb
index 8aeac4137c9..91f12462b8e 100644
--- a/spec/system/page_objects/pages/category.rb
+++ b/spec/system/page_objects/pages/category.rb
@@ -65,7 +65,6 @@ module PageObjects
def save_settings
find("#save-category").click
- expect(page).to have_css("#save-category", text: I18n.t("js.category.save"))
self
end
@@ -74,7 +73,6 @@ module PageObjects
self
end
- # Edit Category Page
def has_form_template_enabled?
find(".d-toggle-switch .toggle-template-type", visible: false)["aria-checked"] == "true"
end
diff --git a/spec/system/simplified_category_creation_spec.rb b/spec/system/simplified_category_creation_spec.rb
index 1a95d2012bd..9f15560619a 100644
--- a/spec/system/simplified_category_creation_spec.rb
+++ b/spec/system/simplified_category_creation_spec.rb
@@ -80,6 +80,21 @@ describe "Simplified Category Creation" do
expect(page).to have_content("Color is invalid")
end
+ it "shows error when icon is missing" do
+ category_page.visit_new_category
+
+ form.field("name").fill_in("Test Category")
+
+ icon_picker = PageObjects::Components::SelectKit.new(".form-kit__control-icon")
+ icon_picker.expand
+ icon_picker.clear
+ icon_picker.collapse
+
+ category_page.save_settings
+
+ expect(form.field("icon")).to have_errors(I18n.t("js.category.validations.icon_required"))
+ end
+
it "shows advanced tabs when toggled" do
category_page.visit_general(category)
@@ -104,6 +119,8 @@ describe "Simplified Category Creation" do
end
describe "Security Tab" do
+ fab!(:private_category) { Fabricate(:private_category, group:) }
+
before do
CategoryGroup.create!(
category:,
@@ -117,6 +134,14 @@ describe "Simplified Category Creation" do
)
end
+ it "resets security settings when navigating from edit to new category" do
+ category_page.visit_general(private_category)
+ expect(page).to have_css(".group-chooser")
+
+ category_page.visit_new_category
+ expect(page).to have_no_css(".group-chooser")
+ end
+
it "removes a group permission" do
category_page.visit_security(category)