From aaf0b0f1cc50f4b60b145d6d64a5f07f37900c18 Mon Sep 17 00:00:00 2001
From: Isaac Janzen <50783505+janzenisaac@users.noreply.github.com>
Date: Sat, 26 Aug 2023 09:52:19 -0500
Subject: [PATCH] DEV: Convert `feature-topic-on-profile` modal to
component-based API (#23275)
# Before
# After
---
.../modal/feature-topic-on-profile.hbs | 26 +++++++++++++
.../modal/feature-topic-on-profile.js | 37 +++++++++++++++++++
.../controllers/feature-topic-on-profile.js | 37 -------------------
.../app/controllers/preferences/profile.js | 26 +++++++------
.../discourse/app/services/modal.js | 1 -
.../modal/feature-topic-on-profile.hbs | 24 ------------
6 files changed, 77 insertions(+), 74 deletions(-)
create mode 100644 app/assets/javascripts/discourse/app/components/modal/feature-topic-on-profile.hbs
create mode 100644 app/assets/javascripts/discourse/app/components/modal/feature-topic-on-profile.js
delete mode 100644 app/assets/javascripts/discourse/app/controllers/feature-topic-on-profile.js
delete mode 100644 app/assets/javascripts/discourse/app/templates/modal/feature-topic-on-profile.hbs
diff --git a/app/assets/javascripts/discourse/app/components/modal/feature-topic-on-profile.hbs b/app/assets/javascripts/discourse/app/components/modal/feature-topic-on-profile.hbs
new file mode 100644
index 00000000000..c88a2bcc165
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/components/modal/feature-topic-on-profile.hbs
@@ -0,0 +1,26 @@
+
+ <:body>
+
+
+ <:footer>
+
+
+
+
\ No newline at end of file
diff --git a/app/assets/javascripts/discourse/app/components/modal/feature-topic-on-profile.js b/app/assets/javascripts/discourse/app/components/modal/feature-topic-on-profile.js
new file mode 100644
index 00000000000..8b289119d75
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/components/modal/feature-topic-on-profile.js
@@ -0,0 +1,37 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { action } from "@ember/object";
+import { ajax } from "discourse/lib/ajax";
+import { popupAjaxError } from "discourse/lib/ajax-error";
+
+export default class FeatureTopicOnProfile extends Component {
+ @tracked newFeaturedTopic = null;
+ @tracked saving = false;
+
+ get noTopicSelected() {
+ return !this.newFeaturedTopic;
+ }
+
+ @action
+ async save() {
+ try {
+ this.saving = true;
+ await ajax(`/u/${this.args.model.user.username}/feature-topic`, {
+ type: "PUT",
+ data: { topic_id: this.newFeaturedTopic.id },
+ });
+
+ this.args.model.setFeaturedTopic(this.newFeaturedTopic);
+ this.args.closeModal();
+ } catch (error) {
+ popupAjaxError(error);
+ } finally {
+ this.saving = false;
+ }
+ }
+
+ @action
+ newTopicSelected(topic) {
+ this.newFeaturedTopic = topic;
+ }
+}
diff --git a/app/assets/javascripts/discourse/app/controllers/feature-topic-on-profile.js b/app/assets/javascripts/discourse/app/controllers/feature-topic-on-profile.js
deleted file mode 100644
index 946a4551066..00000000000
--- a/app/assets/javascripts/discourse/app/controllers/feature-topic-on-profile.js
+++ /dev/null
@@ -1,37 +0,0 @@
-import Controller from "@ember/controller";
-import ModalFunctionality from "discourse/mixins/modal-functionality";
-import { ajax } from "discourse/lib/ajax";
-import { none } from "@ember/object/computed";
-import { popupAjaxError } from "discourse/lib/ajax-error";
-
-export default Controller.extend(ModalFunctionality, {
- newFeaturedTopic: null,
- saving: false,
- noTopicSelected: none("newFeaturedTopic"),
-
- onClose() {
- this.set("newFeaturedTopic", null);
- },
-
- onShow() {
- this.set("modal.modalClass", "choose-topic-modal");
- },
-
- actions: {
- save() {
- return ajax(`/u/${this.model.username}/feature-topic`, {
- type: "PUT",
- data: { topic_id: this.newFeaturedTopic.id },
- })
- .then(() => {
- this.model.set("featured_topic", this.newFeaturedTopic);
- this.send("closeModal");
- })
- .catch(popupAjaxError);
- },
-
- newTopicSelected(topic) {
- this.set("newFeaturedTopic", topic);
- },
- },
-});
diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/profile.js b/app/assets/javascripts/discourse/app/controllers/preferences/profile.js
index 303bfe32348..147e5c4800e 100644
--- a/app/assets/javascripts/discourse/app/controllers/preferences/profile.js
+++ b/app/assets/javascripts/discourse/app/controllers/preferences/profile.js
@@ -1,5 +1,5 @@
import Controller from "@ember/controller";
-import EmberObject from "@ember/object";
+import EmberObject, { action } from "@ember/object";
import I18n from "I18n";
import { ajax } from "discourse/lib/ajax";
import { cookAsync } from "discourse/lib/text";
@@ -7,11 +7,12 @@ import discourseComputed from "discourse-common/utils/decorators";
import { isEmpty } from "@ember/utils";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { readOnly } from "@ember/object/computed";
-import showModal from "discourse/lib/show-modal";
import { inject as service } from "@ember/service";
+import FeatureTopicOnProfileModal from "discourse/components/modal/feature-topic-on-profile";
export default Controller.extend({
dialog: service(),
+ modal: service(),
subpageTitle: I18n.t("user.preferences_nav.profile"),
init() {
this._super(...arguments);
@@ -69,17 +70,18 @@ export default Controller.extend({
"model.can_upload_user_card_background"
),
- actions: {
- showFeaturedTopicModal() {
- const modal = showModal("feature-topic-on-profile", {
- model: this.model,
- title: "user.feature_topic_on_profile.title",
- });
- modal.set("onClose", () => {
- document.querySelector(".feature-topic-on-profile-btn")?.focus();
- });
- },
+ @action
+ async showFeaturedTopicModal() {
+ await this.modal.show(FeatureTopicOnProfileModal, {
+ model: {
+ user: this.model,
+ setFeaturedTopic: (v) => this.set("model.featured_topic", v),
+ },
+ });
+ document.querySelector(".feature-topic-on-profile-btn")?.focus();
+ },
+ actions: {
clearFeaturedTopicFromProfile() {
this.dialog.yesNoConfirm({
message: I18n.t("user.feature_topic_on_profile.clear.warning"),
diff --git a/app/assets/javascripts/discourse/app/services/modal.js b/app/assets/javascripts/discourse/app/services/modal.js
index 9e0faf9824b..70661bfeb23 100644
--- a/app/assets/javascripts/discourse/app/services/modal.js
+++ b/app/assets/javascripts/discourse/app/services/modal.js
@@ -18,7 +18,6 @@ const KNOWN_LEGACY_MODALS = [
"create-account",
"create-invite-bulk",
"create-invite",
- "feature-topic-on-profile",
"feature-topic",
"flag",
"grant-badge",
diff --git a/app/assets/javascripts/discourse/app/templates/modal/feature-topic-on-profile.hbs b/app/assets/javascripts/discourse/app/templates/modal/feature-topic-on-profile.hbs
deleted file mode 100644
index 97fb502d54e..00000000000
--- a/app/assets/javascripts/discourse/app/templates/modal/feature-topic-on-profile.hbs
+++ /dev/null
@@ -1,24 +0,0 @@
-
-
-
-
-
\ No newline at end of file