2024-10-14 13:19:53 +08:00
|
|
|
import Component from "@glimmer/component";
|
|
|
|
import { cached, tracked } from "@glimmer/tracking";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { schedule } from "@ember/runloop";
|
2024-03-07 01:05:11 +08:00
|
|
|
import { service } from "@ember/service";
|
2019-11-01 01:37:24 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2024-08-01 02:06:59 +08:00
|
|
|
import { tagName } from "@ember-decorators/component";
|
2024-06-25 19:32:18 +08:00
|
|
|
import { Promise } from "rsvp";
|
2015-07-11 00:28:56 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2024-11-20 04:45:18 +08:00
|
|
|
import { i18n } from "discourse-i18n";
|
2023-10-11 02:38:59 +08:00
|
|
|
import UserField from "admin/models/user-field";
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2024-08-01 02:06:59 +08:00
|
|
|
@tagName("")
|
2024-10-14 13:19:53 +08:00
|
|
|
export default class AdminUserFieldItem extends Component {
|
2024-08-01 02:06:59 +08:00
|
|
|
@service adminCustomUserFields;
|
|
|
|
@service dialog;
|
2023-12-28 23:24:24 +08:00
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
@tracked isEditing = false;
|
|
|
|
@tracked
|
|
|
|
editableDisabled = this.args.userField.requirement === "for_all_users";
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2024-10-21 14:37:46 +08:00
|
|
|
originalRequirement = this.args.userField.requirement;
|
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
get fieldName() {
|
|
|
|
return UserField.fieldTypeById(this.fieldType)?.name;
|
2024-08-01 02:06:59 +08:00
|
|
|
}
|
2015-07-29 00:29:40 +08:00
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
get cantMoveUp() {
|
|
|
|
return this.args.userField.id === this.args.firstField?.id;
|
2024-08-01 02:06:59 +08:00
|
|
|
}
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
get cantMoveDown() {
|
|
|
|
return this.args.userField.id === this.args.lastField?.id;
|
2024-08-01 02:06:59 +08:00
|
|
|
}
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
get isNewRecord() {
|
|
|
|
return isEmpty(this.args.userField?.id);
|
2024-08-01 02:06:59 +08:00
|
|
|
}
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
get flags() {
|
|
|
|
const flags = [
|
|
|
|
"editable",
|
|
|
|
"show_on_profile",
|
|
|
|
"show_on_user_card",
|
|
|
|
"searchable",
|
|
|
|
];
|
2024-06-25 19:32:18 +08:00
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
return flags
|
|
|
|
.map((flag) => {
|
|
|
|
if (this.args.userField[flag]) {
|
2024-11-20 04:45:18 +08:00
|
|
|
return i18n(`admin.user_fields.${flag}.enabled`);
|
2024-10-14 13:19:53 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Boolean)
|
|
|
|
.join(", ");
|
2024-08-01 02:06:59 +08:00
|
|
|
}
|
2024-06-25 19:32:18 +08:00
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
@cached
|
|
|
|
get formData() {
|
|
|
|
return this.args.userField.getProperties(
|
|
|
|
"field_type",
|
2022-04-19 19:31:43 +08:00
|
|
|
"name",
|
|
|
|
"description",
|
2024-05-23 19:18:25 +08:00
|
|
|
"requirement",
|
2024-10-14 13:19:53 +08:00
|
|
|
"editable",
|
2022-04-19 19:31:43 +08:00
|
|
|
"show_on_profile",
|
|
|
|
"show_on_user_card",
|
|
|
|
"searchable",
|
2023-12-28 23:24:24 +08:00
|
|
|
"options",
|
|
|
|
...this.adminCustomUserFields.additionalProperties
|
2022-04-19 19:31:43 +08:00
|
|
|
);
|
2024-10-14 13:19:53 +08:00
|
|
|
}
|
2022-04-19 19:31:43 +08:00
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
@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) {
|
2024-06-25 19:32:18 +08:00
|
|
|
let confirm = true;
|
|
|
|
|
2024-10-21 14:37:46 +08:00
|
|
|
if (
|
|
|
|
data.requirement === "for_all_users" &&
|
|
|
|
this.originalRequirement !== "for_all_users"
|
|
|
|
) {
|
2024-06-25 19:32:18 +08:00
|
|
|
confirm = await this._confirmChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!confirm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
return this.args.userField
|
|
|
|
.save(data)
|
2022-04-19 19:31:43 +08:00
|
|
|
.then(() => {
|
|
|
|
if (this.isDestroying || this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2024-10-21 14:37:46 +08:00
|
|
|
this.originalRequirement = data.requirement;
|
2024-10-14 13:19:53 +08:00
|
|
|
this.isEditing = false;
|
2022-04-19 19:31:43 +08:00
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
2024-08-01 02:06:59 +08:00
|
|
|
}
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2024-10-14 13:19:53 +08:00
|
|
|
async _confirmChanges() {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.dialog.yesNoConfirm({
|
2024-11-20 04:45:18 +08:00
|
|
|
message: i18n("admin.user_fields.requirement.confirmation"),
|
2024-10-14 13:19:53 +08:00
|
|
|
didCancel: () => resolve(false),
|
|
|
|
didConfirm: () => resolve(true),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-19 19:31:43 +08:00
|
|
|
@action
|
|
|
|
edit() {
|
2024-10-14 13:19:53 +08:00
|
|
|
this.isEditing = true;
|
2024-08-01 02:06:59 +08:00
|
|
|
}
|
2015-07-11 00:28:56 +08:00
|
|
|
|
2022-04-19 19:31:43 +08:00
|
|
|
@action
|
|
|
|
cancel() {
|
2024-10-14 13:19:53 +08:00
|
|
|
if (this.isNewRecord) {
|
|
|
|
this.args.destroyAction(this.args.userField);
|
2022-04-19 19:31:43 +08:00
|
|
|
} else {
|
2024-10-14 13:19:53 +08:00
|
|
|
this.isEditing = false;
|
2022-04-19 19:31:43 +08:00
|
|
|
}
|
2024-08-01 02:06:59 +08:00
|
|
|
}
|
2024-10-14 13:19:53 +08:00
|
|
|
|
|
|
|
_focusName() {
|
|
|
|
schedule("afterRender", () =>
|
|
|
|
document.querySelector(".user-field-name")?.focus()
|
|
|
|
);
|
|
|
|
}
|
2024-08-01 02:06:59 +08:00
|
|
|
}
|