diff --git a/assets/javascripts/discourse-user-notes/lib/user-notes.js b/assets/javascripts/discourse-user-notes/lib/user-notes.js index 6eaeb3a..e8e1dbc 100644 --- a/assets/javascripts/discourse-user-notes/lib/user-notes.js +++ b/assets/javascripts/discourse-user-notes/lib/user-notes.js @@ -1,22 +1,18 @@ -import showModal from "discourse/lib/show-modal"; +import UserNotesModal from "../../discourse/components/modal/user-notes"; +import { getOwnerWithFallback } from "discourse-common/lib/get-owner"; export function showUserNotes(store, userId, callback, opts) { + const modal = getOwnerWithFallback(this).lookup("service:modal"); opts = opts || {}; return store.find("user-note", { user_id: userId }).then((model) => { - const controller = showModal("user-notes", { - model, - title: "user_notes.title", - addModalBodyView: true, + return modal.show(UserNotesModal, { + model: { + note: model, + userId, + callback, + postId: opts.postId, + }, }); - controller.reset(); - - controller.setProperties({ - userId, - callback, - postId: opts.postId, - }); - - return controller; }); } diff --git a/assets/javascripts/discourse/templates/modal/user-notes.hbs b/assets/javascripts/discourse/components/modal/user-notes.hbs similarity index 83% rename from assets/javascripts/discourse/templates/modal/user-notes.hbs rename to assets/javascripts/discourse/components/modal/user-notes.hbs index 0c47410..a006f83 100644 --- a/assets/javascripts/discourse/templates/modal/user-notes.hbs +++ b/assets/javascripts/discourse/components/modal/user-notes.hbs @@ -1,13 +1,17 @@ - + - {{#each model as |n|}} + {{#each @model.note as |n|}} @@ -22,7 +26,7 @@ {{#if n.can_delete}} {{/each}} - \ No newline at end of file + \ No newline at end of file diff --git a/assets/javascripts/discourse/controllers/user-notes.js b/assets/javascripts/discourse/components/modal/user-notes.js similarity index 63% rename from assets/javascripts/discourse/controllers/user-notes.js rename to assets/javascripts/discourse/components/modal/user-notes.js index a42f217..3e809c3 100644 --- a/assets/javascripts/discourse/controllers/user-notes.js +++ b/assets/javascripts/discourse/components/modal/user-notes.js @@ -1,36 +1,32 @@ -import Controller from "@ember/controller"; +import Component from "@glimmer/component"; 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 class UserNotesController extends Controller { +export default class UserNotesModal extends Component { @service dialog; + @service store; @tracked newNote; - @tracked userId; + @tracked userId = this.args.model.userId; @tracked saving = false; + postId = this.args.model.postId; + callback = this.args.model.callback; #refreshCount() { if (this.callback) { - this.callback(this.model.length); + this.callback(this.args.model.note.length); } } - reset() { - this.newNote = null; - this.userId = null; - this.callback = null; - this.saving = false; - } - get attachDisabled() { return this.saving || !this.newNote || this.newNote.length === 0; } @action - attachNote() { + async attachNote() { const note = this.store.createRecord("user-note"); const userId = parseInt(this.userId, 10); @@ -45,15 +41,16 @@ export default class UserNotesController extends Controller { 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)); + try { + await note.save(args); + this.newNote = ""; + this.args.model.note.insertAt(0, note); + this.#refreshCount(); + } catch (error) { + popupAjaxError(error); + } finally { + this.saving = false; + } } @action @@ -64,7 +61,7 @@ export default class UserNotesController extends Controller { note .destroyRecord() .then(() => { - this.model.removeObject(note); + this.args.model.note.removeObject(note); this.#refreshCount(); }) .catch(popupAjaxError); diff --git a/assets/javascripts/discourse/components/show-user-notes.hbs b/assets/javascripts/discourse/components/show-user-notes.hbs index 9d24167..518d904 100644 --- a/assets/javascripts/discourse/components/show-user-notes.hbs +++ b/assets/javascripts/discourse/components/show-user-notes.hbs @@ -1,5 +1,5 @@