discourse/app/assets/javascripts/wizard/components/invite-list.js.es6

72 lines
1.6 KiB
Plaintext
Raw Normal View History

2016-09-14 03:14:17 +08:00
export default Ember.Component.extend({
2018-06-15 23:03:24 +08:00
classNames: ["invite-list"],
2016-09-14 03:14:17 +08:00
users: null,
2018-06-15 23:03:24 +08:00
inviteEmail: "",
inviteRole: "",
2016-09-14 03:14:17 +08:00
invalid: false,
init() {
this._super();
2018-06-15 23:03:24 +08:00
this.set("users", []);
2016-09-14 03:14:17 +08:00
2018-06-15 23:03:24 +08:00
this.set("roles", [
{ id: "moderator", label: I18n.t("wizard.invites.roles.moderator") },
{ id: "regular", label: I18n.t("wizard.invites.roles.regular") }
2016-09-14 03:14:17 +08:00
]);
this.updateField();
},
keyPress(e) {
if (e.keyCode === 13) {
e.preventDefault();
e.stopPropagation();
2018-06-15 23:03:24 +08:00
this.send("addUser");
2016-09-14 03:14:17 +08:00
}
},
updateField() {
2018-06-15 23:03:24 +08:00
const users = this.get("users");
2018-06-15 23:03:24 +08:00
this.set("field.value", JSON.stringify(users));
2018-06-15 23:03:24 +08:00
const staffCount = this.get("step.fieldsById.staff_count.value") || 1;
const showWarning = staffCount < 3 && users.length === 0;
2018-06-15 23:03:24 +08:00
this.set("field.warning", showWarning ? "invites.none_added" : null);
2016-09-14 03:14:17 +08:00
},
actions: {
addUser() {
const user = {
2018-06-15 23:03:24 +08:00
email: this.get("inviteEmail") || "",
role: this.get("inviteRole")
2016-09-14 03:14:17 +08:00
};
if (!/(.+)@(.+){2,}\.(.+){2,}/.test(user.email)) {
2018-06-15 23:03:24 +08:00
return this.set("invalid", true);
2016-09-14 03:14:17 +08:00
}
2018-06-15 23:03:24 +08:00
const users = this.get("users");
if (users.findBy("email", user.email)) {
return this.set("invalid", true);
2016-09-14 03:14:17 +08:00
}
2018-06-15 23:03:24 +08:00
this.set("invalid", false);
2016-09-14 03:14:17 +08:00
users.pushObject(user);
this.updateField();
2018-06-15 23:03:24 +08:00
this.set("inviteEmail", "");
Ember.run.scheduleOnce("afterRender", () =>
this.$(".invite-email").focus()
);
2016-09-14 03:14:17 +08:00
},
removeUser(user) {
2018-06-15 23:03:24 +08:00
this.get("users").removeObject(user);
2016-09-14 03:14:17 +08:00
this.updateField();
}
}
});