From a9d66145269bca3542361e145bf53f2e41d06169 Mon Sep 17 00:00:00 2001 From: Selase Krakani <849886+s3lase@users.noreply.github.com> Date: Tue, 22 Nov 2022 12:30:03 +0000 Subject: [PATCH] DEV: Adopt modern ember primitives (#66) Modernise components and controllers --- .discourse-compatibility | 1 + .../add-user-notes-button.hbs | 2 +- .../show-user-notes-on-flags.hbs | 4 +- .../show-user-notes-on-card.hbs | 4 +- .../show-notes-on-profile.hbs | 2 +- .../discourse/components/show-user-notes.hbs | 6 + .../discourse/components/show-user-notes.js | 18 ++- .../discourse/controllers/user-notes.js | 120 +++++++++--------- .../templates/components/show-user-notes.hbs | 8 -- .../discourse/templates/modal/user-notes.hbs | 34 ++--- assets/stylesheets/user_notes.scss | 12 ++ .../javascripts/acceptance/user-notes-test.js | 62 +++++++++ 12 files changed, 177 insertions(+), 96 deletions(-) create mode 100644 .discourse-compatibility create mode 100644 assets/javascripts/discourse/components/show-user-notes.hbs delete mode 100644 assets/javascripts/discourse/templates/components/show-user-notes.hbs create mode 100644 test/javascripts/acceptance/user-notes-test.js diff --git a/.discourse-compatibility b/.discourse-compatibility new file mode 100644 index 0000000..0d1e7c4 --- /dev/null +++ b/.discourse-compatibility @@ -0,0 +1 @@ +2.8.11: 7137499c69065928c0fdd8795a3fb8d175e1ff3d diff --git a/assets/javascripts/discourse-user-notes/templates/connectors/admin-user-controls-after/add-user-notes-button.hbs b/assets/javascripts/discourse-user-notes/templates/connectors/admin-user-controls-after/add-user-notes-button.hbs index 2bffbd1..f8b688a 100644 --- a/assets/javascripts/discourse-user-notes/templates/connectors/admin-user-controls-after/add-user-notes-button.hbs +++ b/assets/javascripts/discourse-user-notes/templates/connectors/admin-user-controls-after/add-user-notes-button.hbs @@ -1 +1 @@ -{{show-user-notes show=(action "showUserNotes") count=userNotesCount}} + diff --git a/assets/javascripts/discourse-user-notes/templates/connectors/after-reviewable-post-user/show-user-notes-on-flags.hbs b/assets/javascripts/discourse-user-notes/templates/connectors/after-reviewable-post-user/show-user-notes-on-flags.hbs index 0d2c9c6..b62bf4e 100644 --- a/assets/javascripts/discourse-user-notes/templates/connectors/after-reviewable-post-user/show-user-notes-on-flags.hbs +++ b/assets/javascripts/discourse-user-notes/templates/connectors/after-reviewable-post-user/show-user-notes-on-flags.hbs @@ -1,5 +1,5 @@ {{#if userNotesCount}} - + {{#if emojiEnabled}} + {{/if}} diff --git a/assets/javascripts/discourse-user-notes/templates/connectors/user-card-post-names/show-user-notes-on-card.hbs b/assets/javascripts/discourse-user-notes/templates/connectors/user-card-post-names/show-user-notes-on-card.hbs index 0d2c9c6..b62bf4e 100644 --- a/assets/javascripts/discourse-user-notes/templates/connectors/user-card-post-names/show-user-notes-on-card.hbs +++ b/assets/javascripts/discourse-user-notes/templates/connectors/user-card-post-names/show-user-notes-on-card.hbs @@ -1,5 +1,5 @@ {{#if userNotesCount}} - + {{#if emojiEnabled}} + {{/if}} diff --git a/assets/javascripts/discourse-user-notes/templates/connectors/user-profile-controls/show-notes-on-profile.hbs b/assets/javascripts/discourse-user-notes/templates/connectors/user-profile-controls/show-notes-on-profile.hbs index 2bffbd1..f8b688a 100644 --- a/assets/javascripts/discourse-user-notes/templates/connectors/user-profile-controls/show-notes-on-profile.hbs +++ b/assets/javascripts/discourse-user-notes/templates/connectors/user-profile-controls/show-notes-on-profile.hbs @@ -1 +1 @@ -{{show-user-notes show=(action "showUserNotes") count=userNotesCount}} + diff --git a/assets/javascripts/discourse/components/show-user-notes.hbs b/assets/javascripts/discourse/components/show-user-notes.hbs new file mode 100644 index 0000000..05bf729 --- /dev/null +++ b/assets/javascripts/discourse/components/show-user-notes.hbs @@ -0,0 +1,6 @@ + diff --git a/assets/javascripts/discourse/components/show-user-notes.js b/assets/javascripts/discourse/components/show-user-notes.js index 1252c32..4c15dd0 100644 --- a/assets/javascripts/discourse/components/show-user-notes.js +++ b/assets/javascripts/discourse/components/show-user-notes.js @@ -1,8 +1,12 @@ -import Component from "@ember/component"; -import { gt } from "@ember/object/computed"; +import Component from "@glimmer/component"; +import I18n from "I18n"; -export default Component.extend({ - tagName: "", - - showCount: gt("count", 0), -}); +export default class ShowUserNotes extends Component { + get label() { + if (this.args.count > 0) { + return I18n.t("user_notes.show", { count: this.args.count }); + } else { + return I18n.t("user_notes.title"); + } + } +} diff --git a/assets/javascripts/discourse/controllers/user-notes.js b/assets/javascripts/discourse/controllers/user-notes.js index 09bfb6a..a42f217 100644 --- a/assets/javascripts/discourse/controllers/user-notes.js +++ b/assets/javascripts/discourse/controllers/user-notes.js @@ -1,70 +1,74 @@ -import I18n from "I18n"; -import discourseComputed, { on } from "discourse-common/utils/decorators"; -import { popupAjaxError } from "discourse/lib/ajax-error"; import Controller from "@ember/controller"; +import I18n from "I18n"; +import { action } from "@ember/object"; import { inject as service } from "@ember/service"; +import { popupAjaxError } from "discourse/lib/ajax-error"; +import { tracked } from "@glimmer/tracking"; -export default Controller.extend({ - dialog: service(), - newNote: null, - saving: false, - user: null, +export default class UserNotesController extends Controller { + @service dialog; - @on("init") - reset() { - this.setProperties({ newNote: null, saving: false, callback: null }); - }, + @tracked newNote; + @tracked userId; + @tracked saving = false; - @discourseComputed("newNote", "saving") - attachDisabled(newNote, saving) { - return saving || !newNote || newNote.length === 0; - }, - - _refreshCount() { + #refreshCount() { if (this.callback) { - this.callback(this.get("model.length")); + this.callback(this.model.length); } - }, + } - actions: { - attachNote() { - const note = this.store.createRecord("user-note"); - const userId = parseInt(this.userId, 10); + reset() { + this.newNote = null; + this.userId = null; + this.callback = null; + this.saving = false; + } - this.set("saving", true); - let args = { - raw: this.newNote, - user_id: userId, - }; + get attachDisabled() { + return this.saving || !this.newNote || this.newNote.length === 0; + } - if (this.postId) { - args.post_id = parseInt(this.postId, 10); - } + @action + attachNote() { + const note = this.store.createRecord("user-note"); + const userId = parseInt(this.userId, 10); - note - .save(args) - .then(() => { - this.set("newNote", ""); - this.model.insertAt(0, note); - this._refreshCount(); - }) - .catch(popupAjaxError) - .finally(() => this.set("saving", false)); - }, + this.saving = true; - removeNote(note) { - this.dialog.deleteConfirm({ - message: I18n.t("user_notes.delete_confirm"), - didConfirm: () => { - note - .destroyRecord() - .then(() => { - this.model.removeObject(note); - this._refreshCount(); - }) - .catch(popupAjaxError); - }, - }); - }, - }, -}); + const args = { + raw: this.newNote, + user_id: userId, + }; + + if (this.postId) { + args.post_id = parseInt(this.postId, 10); + } + + note + .save(args) + .then(() => { + this.newNote = ""; + this.model.insertAt(0, note); + this.#refreshCount(); + }) + .catch(popupAjaxError) + .finally(() => (this.saving = false)); + } + + @action + removeNote(note) { + this.dialog.deleteConfirm({ + message: I18n.t("user_notes.delete_confirm"), + didConfirm: () => { + note + .destroyRecord() + .then(() => { + this.model.removeObject(note); + this.#refreshCount(); + }) + .catch(popupAjaxError); + }, + }); + } +} diff --git a/assets/javascripts/discourse/templates/components/show-user-notes.hbs b/assets/javascripts/discourse/templates/components/show-user-notes.hbs deleted file mode 100644 index 73a10b1..0000000 --- a/assets/javascripts/discourse/templates/components/show-user-notes.hbs +++ /dev/null @@ -1,8 +0,0 @@ - - {{d-icon "pencil-alt"}} - {{#if showCount}} - {{i18n "user_notes.show" count=count}} - {{else}} - {{i18n "user_notes.title"}} - {{/if}} - diff --git a/assets/javascripts/discourse/templates/modal/user-notes.hbs b/assets/javascripts/discourse/templates/modal/user-notes.hbs index 1c3c6b3..f67d966 100644 --- a/assets/javascripts/discourse/templates/modal/user-notes.hbs +++ b/assets/javascripts/discourse/templates/modal/user-notes.hbs @@ -1,18 +1,18 @@ -{{#d-modal-body class="user-notes-modal"}} - {{textarea value=newNote}} - {{d-button - action=(action "attachNote") - label="user_notes.attach" - class="btn-primary" - disabled=attachDisabled - }} + +