Skip to content

A platform for community discussion. Free, open, simple.

DEV: Convert InlineEditCheckbox to glimmer (#34030)

Jarek Radoszf3c6260e1b3d
diff --git a/app/assets/javascripts/admin/addon/components/inline-edit-checkbox.gjs b/app/assets/javascripts/admin/addon/components/inline-edit-checkbox.gjs
index 25bc0abbe6c..478dac519c8 100644
--- a/app/assets/javascripts/admin/addon/components/inline-edit-checkbox.gjs
+++ b/app/assets/javascripts/admin/addon/components/inline-edit-checkbox.gjs
@@ -1,70 +1,52 @@
-/* eslint-disable ember/no-classic-components */
-import Component, { Input } from "@ember/component";
+import Component from "@glimmer/component";
+import { fn } from "@ember/helper";
+import { on } from "@ember/modifier";
import { action } from "@ember/object";
-import { classNames } from "@ember-decorators/component";
+import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import DButton from "discourse/components/d-button";
-import discourseComputed from "discourse/lib/decorators";
+import withEventValue from "discourse/helpers/with-event-value";
+import { resettableTracked } from "discourse/lib/tracked-tools";
import { i18n } from "discourse-i18n";
-@classNames("inline-edit")
+// args: action, labelKey, checked, modelId
export default class InlineEditCheckbox extends Component {
- buffer = null;
- bufferModelId = null;
+ @resettableTracked value = this.args.checked;
- didReceiveAttrs() {
- super.didReceiveAttrs(...arguments);
-
- if (this.modelId !== this.bufferModelId) {
- // HACK: The condition above ensures this method is called only when its
- // attributes are changed (i.e. only when `checked` changes).
- //
- // Reproduction steps: navigate to theme #1, switch to theme #2 from the
- // left-side panel, then switch back to theme #1 and click on the <input>
- // element wrapped by this component. It will call `didReceiveAttrs` even
- // though none of the attributes have changed (only `buffer` does).
- this.setProperties({
- buffer: this.checked,
- bufferModelId: this.modelId,
- });
- }
- }
-
- @discourseComputed("checked", "buffer")
- changed(checked, buffer) {
- return !!checked !== !!buffer;
+ get changed() {
+ return !!this.args.checked !== !!this.value;
}
@action
- apply() {
- this.set("checked", this.buffer);
- this.action();
- }
-
- @action
- cancel() {
- this.set("buffer", this.checked);
+ reset() {
+ this.value = this.args.checked;
}
<template>
- <label class="checkbox-label">
- <Input
- @type="checkbox"
- disabled={{this.disabled}}
- @checked={{this.buffer}}
- />
- {{i18n this.labelKey}}
- </label>
- {{#if this.changed}}
- <DButton
- @action={{this.apply}}
- @icon="check"
- class="btn-primary btn-small submit-edit"
- />
- <DButton
- @action={{this.cancel}}
- @icon="xmark"
- class="btn-small cancel-edit"
- />
- {{/if}}
+ <div {{didUpdate this.reset @modelId}} class="inline-edit">
+ <label class="checkbox-label">
+ <input
+ {{on
+ "change"
+ (withEventValue (fn (mut this.value)) "target.checked")
+ }}
+ checked={{this.value}}
+ type="checkbox"
+ />
+ {{i18n @labelKey}}
+ </label>
+
+ {{#if this.changed}}
+ <DButton
+ @action={{fn @action this.value}}
+ @icon="check"
+ class="btn-primary btn-small submit-edit"
+ />
+ <DButton
+ @action={{this.reset}}
+ @icon="xmark"
+ class="btn-small cancel-edit"
+ />
+ {{/if}}
+ </div>
</template>
}
diff --git a/app/assets/javascripts/admin/addon/controllers/admin-customize-colors-show.js b/app/assets/javascripts/admin/addon/controllers/admin-customize-colors-show.js
index 51ddb99f75e..ddec15c0fbe 100644
--- a/app/assets/javascripts/admin/addon/controllers/admin-customize-colors-show.js
+++ b/app/assets/javascripts/admin/addon/controllers/admin-customize-colors-show.js
@@ -70,8 +70,9 @@ export default class AdminCustomizeColorsShowController extends Controller {
}
@action
- applyUserSelectable() {
- this.model.updateUserSelectable(this.get("model.user_selectable"));
+ applyUserSelectable(value) {
+ this.model.set("user_selectable", value);
+ this.model.updateUserSelectable(value);
}
@action
diff --git a/app/assets/javascripts/admin/addon/controllers/admin-customize-themes-show-index.js b/app/assets/javascripts/admin/addon/controllers/admin-customize-themes-show-index.js
index 91ab25ec8b3..5c90ce6b906 100644
--- a/app/assets/javascripts/admin/addon/controllers/admin-customize-themes-show-index.js
+++ b/app/assets/javascripts/admin/addon/controllers/admin-customize-themes-show-index.js
@@ -356,26 +356,29 @@ export default class AdminCustomizeThemesShowIndexController extends Controller
}
@action
- applyDefault() {
- const model = this.model;
- model.saveChanges("default").then(() => {
- if (model.get("default")) {
- this.allThemes.forEach((theme) => {
- if (theme !== model && theme.get("default")) {
- theme.set("default", false);
- }
- });
- }
- });
+ async applyDefault(value) {
+ this.model.set("default", value);
+
+ await this.model.saveChanges("default");
+
+ if (this.model.get("default")) {
+ this.allThemes.forEach((theme) => {
+ if (theme !== this.model && theme.get("default")) {
+ theme.set("default", false);
+ }
+ });
+ }
}
@action
- applyUserSelectable() {
+ applyUserSelectable(value) {
+ this.model.set("user_selectable", value);
this.model.saveChanges("user_selectable");
}
@action
- applyAutoUpdateable() {
+ applyAutoUpdateable(value) {
+ this.model.set("auto_update", value);
this.model.saveChanges("auto_update");
}