A platform for community discussion. Free, open, simple.
DEV: Add created flags to the site as post action types (#45059)
Sérgio Saquetimcb4eb8bbfe2b
diff --git a/frontend/discourse/admin/components/admin-flags-form.gjs b/frontend/discourse/admin/components/admin-flags-form.gjsindex ae727d59f9b..c3565745657 100644--- a/frontend/discourse/admin/components/admin-flags-form.gjs+++ b/frontend/discourse/admin/components/admin-flags-form.gjs@@ -9,6 +9,7 @@ import Form from "discourse/components/form";import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";import { bind } from "discourse/lib/decorators";+import PostActionType from "discourse/models/post-action-type";import MultiSelect from "discourse/select-kit/components/multi-select";import { i18n } from "discourse-i18n";@@ -96,7 +97,7 @@ export default class AdminFlagsForm extends Component {type: "POST",data,});- this.site.flagTypes.push(response.flag);+ this.site.flagTypes.push(PostActionType.create(response.flag));this.router.transitionTo("adminConfig.flags");} catch (error) {popupAjaxError(error);diff --git a/frontend/discourse/tests/integration/components/admin-flags-form-test.gjs b/frontend/discourse/tests/integration/components/admin-flags-form-test.gjsnew file mode 100644index 00000000000..36cb12f5c02--- /dev/null+++ b/frontend/discourse/tests/integration/components/admin-flags-form-test.gjs@@ -0,0 +1,64 @@+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import sinon from "sinon";+import AdminFlagsForm from "discourse/admin/components/admin-flags-form";+import PostActionType from "discourse/models/post-action-type";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import pretender, { response } from "discourse/tests/helpers/create-pretender";+import form from "discourse/tests/helpers/form-kit-helper";++module("Integration | Component | AdminFlagsForm", function (hooks) {+ setupRenderingTest(hooks);++ hooks.beforeEach(function () {+ this.site = this.owner.lookup("service:site");+ this.site.set("valid_flag_applies_to_types", ["Post"]);+ this.site.set("post_action_types", [+ PostActionType.create({+ id: 3,+ name: "Off-Topic",+ name_key: "off_topic",+ is_flag: true,+ system: true,+ }),+ ]);+ sinon.stub(this.owner.lookup("service:router"), "transitionTo");+ });++ test("a created flag joins the site's flag types as a post action type", async function (assert) {+ pretender.post("/admin/config/flags", () =>+ response(200, {+ flag: {+ id: 2001,+ name: "Custom flag",+ name_key: "custom_custom_flag",+ description: "A custom flag",+ short_description: "",+ applies_to: ["Post"],+ position: 2,+ require_message: false,+ enabled: true,+ is_flag: true,+ is_used: false,+ auto_action_type: false,+ system: false,+ },+ })+ );++ await render(<template><AdminFlagsForm /></template>);++ await form().field("name").fillIn("Custom flag");+ await form().field("description").fillIn("A custom flag");+ await form().field("appliesTo").select("Post");+ await form().submit();++ const created = this.site.flagTypes.find((flag) => flag.id === 2001);+ assert.strictEqual(created?.name, "Custom flag", "the new flag is listed");+ assert.true(+ created instanceof PostActionType,+ "the new flag is a post action type, like the flags the site loaded"+ );+ assert.false(created.isIllegal);+ });+});