Skip to content

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

DEV: Migrate post-voting category settings outlet to FormKit (#38752)

Régis Hanol3f3a31199ec5
diff --git a/plugins/discourse-post-voting/assets/javascripts/discourse/connectors/category-custom-settings/post-voting-category-settings.gjs b/plugins/discourse-post-voting/assets/javascripts/discourse/connectors/category-custom-settings/post-voting-category-settings.gjs
index 064236dd225..f65b218cfb5 100644
--- a/plugins/discourse-post-voting/assets/javascripts/discourse/connectors/category-custom-settings/post-voting-category-settings.gjs
+++ b/plugins/discourse-post-voting/assets/javascripts/discourse/connectors/category-custom-settings/post-voting-category-settings.gjs
@@ -1,34 +1,82 @@
-/* eslint-disable ember/no-classic-components */
-import Component, { Input } from "@ember/component";
-import { tagName } from "@ember-decorators/component";
+import Component from "@glimmer/component";
+import { on } from "@ember/modifier";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
import { i18n } from "discourse-i18n";
-@tagName("")
export default class PostVotingCategorySettings extends Component {
+ @service siteSettings;
+
+ @action
+ onChangeCreateAsPostVotingDefault(event) {
+ this.args.outletArgs.category.set(
+ "custom_fields.create_as_post_voting_default",
+ event.target.checked
+ );
+ }
+
+ @action
+ onChangeOnlyPostVotingInThisCategory(event) {
+ this.args.outletArgs.category.set(
+ "custom_fields.only_post_voting_in_this_category",
+ event.target.checked
+ );
+ }
+
<template>
- <div
- class="category-custom-settings-outlet post-voting-category-settings"
- ...attributes
- >
- <h3>{{i18n "category.post_voting_settings_heading"}}</h3>
- <section class="field">
- <label>
- <Input
- id="create-as-post-voting-default"
+ {{#if this.siteSettings.enable_simplified_category_creation}}
+ <@outletArgs.form.Section
+ @title={{i18n "category.post_voting_settings_heading"}}
+ class="category-custom-settings-outlet post-voting-category-settings"
+ >
+ <@outletArgs.form.Object @name="custom_fields" as |object|>
+ <object.Field
+ @name="create_as_post_voting_default"
+ @title={{i18n "category.create_as_post_voting_default"}}
+ @format="max"
@type="checkbox"
- @checked={{this.category.custom_fields.create_as_post_voting_default}}
- />
- {{i18n "category.create_as_post_voting_default"}}
- </label>
- <label>
- <Input
- id="only-post-voting-in-this-category"
+ as |field|
+ >
+ <field.Control />
+ </object.Field>
+
+ <object.Field
+ @name="only_post_voting_in_this_category"
+ @title={{i18n "category.only_post_voting_in_this_category"}}
+ @format="max"
@type="checkbox"
- @checked={{this.category.custom_fields.only_post_voting_in_this_category}}
- />
- {{i18n "category.only_post_voting_in_this_category"}}
- </label>
- </section>
- </div>
+ as |field|
+ >
+ <field.Control />
+ </object.Field>
+ </@outletArgs.form.Object>
+ </@outletArgs.form.Section>
+ {{else}}
+ <div
+ class="category-custom-settings-outlet post-voting-category-settings"
+ >
+ <h3>{{i18n "category.post_voting_settings_heading"}}</h3>
+ <section class="field">
+ <label class="checkbox-label">
+ <input
+ id="create-as-post-voting-default"
+ type="checkbox"
+ checked={{@outletArgs.category.custom_fields.create_as_post_voting_default}}
+ {{on "change" this.onChangeCreateAsPostVotingDefault}}
+ />
+ {{i18n "category.create_as_post_voting_default"}}
+ </label>
+ <label class="checkbox-label">
+ <input
+ id="only-post-voting-in-this-category"
+ type="checkbox"
+ checked={{@outletArgs.category.custom_fields.only_post_voting_in_this_category}}
+ {{on "change" this.onChangeOnlyPostVotingInThisCategory}}
+ />
+ {{i18n "category.only_post_voting_in_this_category"}}
+ </label>
+ </section>
+ </div>
+ {{/if}}
</template>
}
diff --git a/plugins/discourse-post-voting/spec/system/category_settings_spec.rb b/plugins/discourse-post-voting/spec/system/category_settings_spec.rb
new file mode 100644
index 00000000000..744523fa54b
--- /dev/null
+++ b/plugins/discourse-post-voting/spec/system/category_settings_spec.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+RSpec.describe "Post Voting Category Settings" do
+ fab!(:admin)
+ fab!(:category)
+
+ let(:category_page) { PageObjects::Pages::Category.new }
+ let(:form) { PageObjects::Components::FormKit.new(".form-kit") }
+ let(:banner) { PageObjects::Components::AdminChangesBanner.new }
+ let(:toasts) { PageObjects::Components::Toasts.new }
+
+ before { sign_in(admin) }
+
+ context "when simplified category creation is enabled" do
+ before { SiteSetting.enable_simplified_category_creation = true }
+
+ it "can toggle post voting custom fields via FormKit" do
+ category_page.visit_settings(category)
+
+ form.field("custom_fields.create_as_post_voting_default").toggle
+ form.field("custom_fields.only_post_voting_in_this_category").toggle
+ banner.click_save
+
+ expect(toasts).to have_success(I18n.t("js.saved"))
+ category.reload
+ expect(category.custom_fields["create_as_post_voting_default"]).to eq(true)
+ expect(category.custom_fields["only_post_voting_in_this_category"]).to eq(true)
+ end
+ end
+
+ context "when simplified category creation is disabled" do
+ before { SiteSetting.enable_simplified_category_creation = false }
+
+ it "can toggle post voting custom fields via legacy form" do
+ category_page.visit_settings(category)
+
+ find("#create-as-post-voting-default").click
+ find("#only-post-voting-in-this-category").click
+ category_page.save_settings
+
+ expect(toasts).to have_success(I18n.t("js.saved"))
+ category.reload
+ expect(category.custom_fields["create_as_post_voting_default"]).to eq(true)
+ expect(category.custom_fields["only_post_voting_in_this_category"]).to eq(true)
+ end
+ end
+end