Skip to content

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

FEATURE: Allow removal of Support category type (#38650)

Martin Brennan75ebc4f1c7b2
diff --git a/frontend/discourse/admin/components/edit-category-tab.gjs b/frontend/discourse/admin/components/edit-category-tab.gjs
index 1a3821f0dad..014c4d18743 100644
--- a/frontend/discourse/admin/components/edit-category-tab.gjs
+++ b/frontend/discourse/admin/components/edit-category-tab.gjs
@@ -49,6 +49,10 @@ export default class EditCategoryTab extends Component {
@action
select(event) {
event?.preventDefault();
+ if (this.selectedTab === this.tab) {
+ return;
+ }
+
this.set("selectedTab", this.tab);
if (this.newCategory) {
DiscourseURL.routeTo(getURL(`/new-category/${this.tab}`));
diff --git a/frontend/discourse/admin/controllers/edit-category/tabs.js b/frontend/discourse/admin/controllers/edit-category/tabs.js
index 77d011749a4..8f1f0eb7305 100644
--- a/frontend/discourse/admin/controllers/edit-category/tabs.js
+++ b/frontend/discourse/admin/controllers/edit-category/tabs.js
@@ -140,7 +140,7 @@ export default class EditCategoryTabsController extends Controller {
data.category_type_site_settings = {};
- Object.values(this.model.category_types ?? {}).forEach((categoryType) => {
+ Object.values(this.model.categoryTypes ?? {}).forEach((categoryType) => {
categoryType.configuration_schema.category_custom_fields?.forEach(
(field) => {
data.custom_fields[field.key] ??= field.default;
@@ -184,7 +184,7 @@ export default class EditCategoryTabsController extends Controller {
});
}
- const types = Object.values(this.model.category_types ?? {});
+ const types = Object.values(this.model.categoryTypes ?? {});
if (types.length > 0) {
return i18n("category.create_with_type", {
typeName: types[0].name.toLowerCase(),
@@ -205,6 +205,10 @@ export default class EditCategoryTabsController extends Controller {
@action
setSelectedTab(tab) {
+ if (this.selectedTab === tab) {
+ return;
+ }
+
this.selectedTab = tab;
this.showAdvancedTabs = this.showAdvancedTabs || tab !== "general";
}
diff --git a/frontend/discourse/admin/routes/edit-category/tabs.js b/frontend/discourse/admin/routes/edit-category/tabs.js
index 7d55dcce36a..92385a77850 100644
--- a/frontend/discourse/admin/routes/edit-category/tabs.js
+++ b/frontend/discourse/admin/routes/edit-category/tabs.js
@@ -1,10 +1,30 @@
+import { service } from "@ember/service";
import DiscourseRoute from "discourse/routes/discourse";
export default class EditCategoryTabs extends DiscourseRoute {
+ @service router;
+
model() {
return this.modelFor("editCategory");
}
+ activate() {
+ this.router.on("routeDidChange", this, this._syncTabFromParams);
+ }
+
+ deactivate() {
+ this.router.off("routeDidChange", this, this._syncTabFromParams);
+ }
+
+ _syncTabFromParams() {
+ if (this.router.currentRouteName?.startsWith("editCategory.tabs")) {
+ const tab = this.router.currentRoute?.params?.tab;
+ if (tab) {
+ this.controllerFor("edit-category.tabs").setSelectedTab(tab);
+ }
+ }
+ }
+
setupController(controller, model, transition) {
super.setupController(...arguments);
diff --git a/frontend/discourse/admin/routes/new-category/tabs.js b/frontend/discourse/admin/routes/new-category/tabs.js
index d7006ac814d..e80f4f901a0 100644
--- a/frontend/discourse/admin/routes/new-category/tabs.js
+++ b/frontend/discourse/admin/routes/new-category/tabs.js
@@ -38,7 +38,7 @@ export default class NewCategoryTabs extends DiscourseRoute {
name: result.typeName,
configuration_schema: result.typeSchema,
};
- model.set("category_types", initialTypes);
+ model.set("categoryTypes", initialTypes);
// Only want to prefill the general settings (name etc) if it's the
// first category of this type.
diff --git a/frontend/discourse/app/components/edit-category-type-schema-fields.gjs b/frontend/discourse/app/components/edit-category-type-schema-fields.gjs
index e22a82eeeac..65949d6ec00 100644
--- a/frontend/discourse/app/components/edit-category-type-schema-fields.gjs
+++ b/frontend/discourse/app/components/edit-category-type-schema-fields.gjs
@@ -114,6 +114,8 @@ export default class EditCategoryTypeSchemaFields extends Component {
</@form.Object>
</@form.Section>
+ {{yield to="beforeSiteSettings"}}
+
<@form.Emphasis
@title={{i18n "category.type_settings_schema.site_settings"}}
@subtitle={{i18n "category.settings_apply_to_all_of_type_warning"}}
diff --git a/frontend/discourse/app/models/category.js b/frontend/discourse/app/models/category.js
index 0f83610530b..48caf2986c9 100644
--- a/frontend/discourse/app/models/category.js
+++ b/frontend/discourse/app/models/category.js
@@ -332,6 +332,7 @@ export default class Category extends RestModel {
static _includePermissions(category, store, site) {
const model = site.updateCategory(category);
model.setupGroupsAndPermissions();
+ model.setupCategoryTypes();
return model;
}
@@ -485,6 +486,7 @@ export default class Category extends RestModel {
@tracked minimum_required_tags;
@tracked styleType = this.style_type;
@tracked allowed_tags;
+ @tracked categoryTypes;
@autoTrackedArray available_groups;
@autoTrackedArray permissions;
@autoTrackedArray required_tag_groups;
@@ -494,6 +496,10 @@ export default class Category extends RestModel {
this.setupGroupsAndPermissions();
}
+ setupCategoryTypes() {
+ this.categoryTypes = new TrackedObject(this.category_types);
+ }
+
setupGroupsAndPermissions() {
if (!this.available_groups) {
return;
@@ -849,8 +855,8 @@ export default class Category extends RestModel {
category_type_site_settings: this.category_type_site_settings,
};
- if (!id && this.category_types) {
- props.category_type = Object.keys(this.category_types)[0];
+ if (!id && this.categoryTypes) {
+ props.category_type = Object.keys(this.categoryTypes)[0];
}
return props;
@@ -907,11 +913,15 @@ export default class Category extends RestModel {
}
isType(type) {
- return Object.keys(this.category_types ?? {}).includes(type);
+ return Object.keys(this.categoryTypes ?? {}).includes(type);
}
getType(type) {
- return this.category_types?.[type];
+ return this.categoryTypes?.[type];
+ }
+
+ removeType(type) {
+ delete this.categoryTypes[type];
}
@computed("topics")
diff --git a/frontend/discourse/app/models/site.js b/frontend/discourse/app/models/site.js
index 5a6be256c62..04b1f9b348d 100644
--- a/frontend/discourse/app/models/site.js
+++ b/frontend/discourse/app/models/site.js
@@ -302,6 +302,7 @@ export default class Site extends RestModel {
if (existingCategory) {
existingCategory.setProperties(newCategory);
+ existingCategory.setupCategoryTypes();
return existingCategory;
} else {
newCategory = this.store.createRecord("category", newCategory);
diff --git a/plugins/discourse-solved/app/services/discourse_solved/categories/types/support.rb b/plugins/discourse-solved/app/services/discourse_solved/categories/types/support.rb
index 8f692c08d82..595b3571fb5 100644
--- a/plugins/discourse-solved/app/services/discourse_solved/categories/types/support.rb
+++ b/plugins/discourse-solved/app/services/discourse_solved/categories/types/support.rb
@@ -77,6 +77,7 @@ module DiscourseSolved
label: I18n.t("discourse_solved.category_type.allow_accepted_answers.label"),
required: true,
show_on_create: false,
+ show_on_edit: false,
},
:solved_topics_auto_close_hours => {
default: 48,
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/components/upsert-category-support.gjs b/plugins/discourse-solved/assets/javascripts/discourse/components/upsert-category-support.gjs
index 6762aff8cd5..4a91dd8505c 100644
--- a/plugins/discourse-solved/assets/javascripts/discourse/components/upsert-category-support.gjs
+++ b/plugins/discourse-solved/assets/javascripts/discourse/components/upsert-category-support.gjs
@@ -1,19 +1,74 @@
import Component from "@glimmer/component";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import DButton from "discourse/components/d-button";
import EditCategoryTypeSchemaFields from "discourse/components/edit-category-type-schema-fields";
+import { popupAjaxError } from "discourse/lib/ajax-error";
+import Category from "discourse/models/category";
import { i18n } from "discourse-i18n";
export default class UpsertCategorySupport extends Component {
+ @service dialog;
+ @service toasts;
+ @service router;
+
get isSupportCategory() {
return this.args.category.isType("support");
}
+ @action
+ removeSupportCategory() {
+ this.dialog.yesNoConfirm({
+ message: i18n("solved.category_type_support.confirm_remove_support_type"),
+ didConfirm: async () => {
+ this.args.category.set(
+ "custom_fields.enable_accepted_answers",
+ "false"
+ );
+ this.args.form.set("custom_fields.enable_accepted_answers", "false");
+ try {
+ await this.args.category.save();
+ this.args.category.removeType("support");
+ this.toasts.success({
+ duration: "short",
+ data: {
+ message: i18n("saved"),
+ },
+ });
+ this.router.transitionTo(
+ "editCategory.tabs",
+ Category.slugFor(this.args.category),
+ "general"
+ );
+ } catch (err) {
+ this.args.category.set(
+ "custom_fields.enable_accepted_answers",
+ "true"
+ );
+ popupAjaxError(err);
+ }
+ },
+ });
+ }
+
<template>
{{#if this.isSupportCategory}}
<EditCategoryTypeSchemaFields
@category={{@category}}
@categoryType="support"
@form={{@form}}
- />
+ >
+ <:beforeSiteSettings>
+ <div class="support-category--danger-zone">
+ <DButton
+ class="btn-small support-category__remove-type"
+ @action={{this.removeSupportCategory}}
+ @icon="link-slash"
+ @label="solved.category_type_support.remove_support_type"
+ />
+ </div>
+ </:beforeSiteSettings>
+ </EditCategoryTypeSchemaFields>
{{else}}
{{i18n "solved.category_type_support.not_support_type"}}
{{/if}}
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-type-tab.js b/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-type-tab.js
index 05283a5ed73..1c47aeea7a1 100644
--- a/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-type-tab.js
+++ b/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-type-tab.js
@@ -13,9 +13,12 @@ export default {
name: i18n("solved.category_type_support.title"),
primary: true,
component: UpsertCategorySupport,
- condition: ({ category }) =>
- category.isType("support") &&
- siteSettings.enable_support_category_type_setup,
+ condition: ({ category }) => {
+ return (
+ category.isType("support") &&
+ siteSettings.enable_support_category_type_setup
+ );
+ },
});
});
},
diff --git a/plugins/discourse-solved/config/locales/client.en.yml b/plugins/discourse-solved/config/locales/client.en.yml
index ed0721ac513..5c24d5de245 100644
--- a/plugins/discourse-solved/config/locales/client.en.yml
+++ b/plugins/discourse-solved/config/locales/client.en.yml
@@ -59,6 +59,8 @@ en:
category_type_support:
title: "Support"
not_support_type: "Heads up! You've disabled the ability to mark posts as solutions in this category. To turn that back on, you'll find the relevant settings in the Settings tab."
+ remove_support_type: "Remove Support type"
+ confirm_remove_support_type: "Are you sure you want to remove the ability to mark posts as solutions in this category? You can re-enable it in the Settings tab later."
topic_statuses:
solved:
diff --git a/plugins/discourse-solved/spec/system/support_category_type_setup_spec.rb b/plugins/discourse-solved/spec/system/support_category_type_setup_spec.rb
index ea0761d58f9..23da0b8e7b8 100644
--- a/plugins/discourse-solved/spec/system/support_category_type_setup_spec.rb
+++ b/plugins/discourse-solved/spec/system/support_category_type_setup_spec.rb
@@ -7,6 +7,7 @@ RSpec.describe "Support Category Type Setup", type: :system do
let(:form) { PageObjects::Components::FormKit.new(".form-kit") }
let(:category_type_card) { PageObjects::Components::CategoryTypeCard.new }
let(:banner) { PageObjects::Components::AdminChangesBanner.new }
+ let(:dialog) { PageObjects::Components::Dialog.new }
let(:toast) { PageObjects::Components::Toasts.new }
before do
@@ -90,9 +91,7 @@ RSpec.describe "Support Category Type Setup", type: :system do
it "can edit the settings of the support category in a tab" do
visit("/c/#{category.slug}/edit/support")
- page.find(".d-nav-submenu__tabs .edit-category-support").click
- expect(form.field("custom_fields.enable_accepted_answers").value).to be_truthy
expect(
form
.field("custom_fields.solved_topics_auto_close_hours")
@@ -121,6 +120,19 @@ RSpec.describe "Support Category Type Setup", type: :system do
expect(category.custom_fields["solved_topics_auto_close_hours"]).to eq("72")
expect(SiteSetting.show_who_marked_solved).to eq(true)
end
+
+ it "can remove the support type from the category" do
+ visit("/c/#{category.slug}/edit/support")
+ page.find(".support-category--danger-zone .support-category__remove-type").click
+ expect(dialog).to have_content(
+ I18n.t("js.solved.category_type_support.confirm_remove_support_type"),
+ )
+ dialog.click_yes
+ expect(toast).to have_success(I18n.t("js.saved"))
+ expect(page).to have_css(".edit-category-general.active")
+ expect(page).to have_current_path("/c/#{category.slug}/edit/general")
+ expect(category.reload.custom_fields["enable_accepted_answers"]).to eq("false")
+ end
end
context "when visiting the Support tab for a non-support category" do