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

72 lines
1.5 KiB
Plaintext
Raw Normal View History

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