import Component from "@glimmer/component"; import { cached, tracked } from "@glimmer/tracking"; import { hash } from "@ember/helper"; import { action } from "@ember/object"; import didInsert from "@ember/render-modifiers/modifiers/did-insert"; import { schedule } from "@ember/runloop"; import { service } from "@ember/service"; import { eq, or } from "truth-helpers"; import Form from "discourse/components/form"; import PluginOutlet from "discourse/components/plugin-outlet"; import { popupAjaxError } from "discourse/lib/ajax-error"; import { i18n } from "discourse-i18n"; import ValueList from "admin/components/value-list"; import UserField from "admin/models/user-field"; export default class AdminUserFieldsForm extends Component { @service dialog; @service router; @service adminUserFields; @service adminCustomUserFields; @service toasts; @tracked editableDisabled = this.args.userField.requirement === "for_all_users"; originalRequirement = this.args.userField.requirement; userField; get fieldTypes() { return UserField.fieldTypes(); } @cached get formData() { return this.args.userField.getProperties( "field_type", "name", "description", "requirement", "editable", "show_on_profile", "show_on_user_card", "searchable", "options", ...this.adminCustomUserFields.additionalProperties ); } @action setRequirement(value, { set }) { set("requirement", value); if (value === "for_all_users") { this.editableDisabled = true; set("editable", true); } else { this.editableDisabled = false; } } @action async save(data) { let confirm = true; if ( data.requirement === "for_all_users" && this.originalRequirement !== "for_all_users" ) { confirm = await this._confirmChanges(); } if (!confirm) { return; } try { const isNew = this.args.userField.isNew; await this.args.userField.save(data); this.originalRequirement = data.requirement; if (isNew) { this.adminUserFields.userFields.pushObject(this.args.userField); } this.router.transitionTo("adminUserFields.index"); this.toasts.success({ duration: 3000, data: { message: i18n("admin.config_areas.user_fields.save_successful"), }, }); } catch (error) { popupAjaxError(error); } } @action cancel() { this.router.transitionTo("adminUserFields.index"); } _focusName() { schedule("afterRender", () => document.querySelector(".user-field-name")?.focus() ); } async _confirmChanges() { return new Promise((resolve) => { this.dialog.yesNoConfirm({ message: i18n("admin.user_fields.requirement.confirmation"), didCancel: () => resolve(false), didConfirm: () => resolve(true), }); }); } }