discourse/app/assets/javascripts/admin/addon/controllers/admin-email-templates-edit.js
Kelv 3522867fac
DEV: refactor usage of buffered-content mixin (#31926)
Refactors the use of the buffered-content mixin to native getters on the
dependent classes.

This mixin previously provided a cached wrapper around an instance of
BufferedProxy and added 2 convenience methods aliasing BufferedProxy
methods.

### Main changes:
* Use of the`@cached` decorator to maintain parity with the previous
version of `this.buffered` to make sure we only refresh the buffered
proxy if the dependent property changes.
* _Not entirely sure if @cached + @dependentCompat is more performant
than just using `@computed`_

* Use of the`@dependentCompat` decorator to ensure backwards
compatibility of the getter with computed properties - we will leave
refactoring of those somewhere down the road as that would greatly
increase the scope of this PR

* `applyChanges` / `discardChanges` are the same as
`applyBufferedChanges` / `discardBufferedChanges` for BufferedProxy
2025-03-21 09:06:26 +08:00

72 lines
1.9 KiB
JavaScript
Vendored

import { cached, tracked } from "@glimmer/tracking";
import Controller, { inject as controller } from "@ember/controller";
import { action } from "@ember/object";
import { dependentKeyCompat } from "@ember/object/compat";
import { service } from "@ember/service";
import BufferedProxy from "ember-buffered-proxy/proxy";
import { popupAjaxError } from "discourse/lib/ajax-error";
import discourseComputed from "discourse/lib/decorators";
import { i18n } from "discourse-i18n";
export default class AdminEmailTemplatesEditController extends Controller {
@service dialog;
@controller adminEmailTemplates;
@tracked emailTemplate = null;
saved = false;
@cached
@dependentKeyCompat
get buffered() {
return BufferedProxy.create({
content: this.emailTemplate,
});
}
@discourseComputed("buffered.body", "buffered.subject")
saveDisabled(body, subject) {
return (
this.emailTemplate.body === body && this.emailTemplate.subject === subject
);
}
@discourseComputed("buffered")
hasMultipleSubjects(buffered) {
if (buffered.getProperties("subject")["subject"]) {
return false;
} else {
return buffered.getProperties("id")["id"];
}
}
@action
saveChanges() {
this.set("saved", false);
const buffered = this.buffered;
this.emailTemplate
.save(buffered.getProperties("subject", "body"))
.then(() => {
this.set("saved", true);
})
.catch(popupAjaxError);
}
@action
revertChanges() {
this.set("saved", false);
this.dialog.yesNoConfirm({
title: i18n("admin.customize.email_templates.revert_confirm"),
didConfirm: () => {
return this.emailTemplate
.revert()
.then((props) => {
const buffered = this.buffered;
buffered.setProperties(props);
this.buffered.applyChanges();
})
.catch(popupAjaxError);
},
});
}
}