2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2020-04-16 13:58:04 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
2019-10-24 01:15:28 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2016-09-14 03:14:17 +08:00
|
|
|
classNames: ["invite-list"],
|
|
|
|
users: null,
|
|
|
|
inviteEmail: "",
|
|
|
|
inviteRole: "",
|
|
|
|
invalid: false,
|
|
|
|
|
|
|
|
init() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2016-09-14 03:14:17 +08:00
|
|
|
this.set("users", []);
|
|
|
|
|
|
|
|
this.set("roles", [
|
|
|
|
{ id: "moderator", label: I18n.t("wizard.invites.roles.moderator") },
|
|
|
|
{ id: "regular", label: I18n.t("wizard.invites.roles.regular") },
|
|
|
|
]);
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.set("inviteRole", this.get("roles.0.id"));
|
|
|
|
|
2016-09-14 03:14:17 +08:00
|
|
|
this.updateField();
|
|
|
|
},
|
|
|
|
|
|
|
|
keyPress(e) {
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.send("addUser");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateField() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const users = this.users;
|
2016-09-21 00:28:22 +08:00
|
|
|
|
|
|
|
this.set("field.value", JSON.stringify(users));
|
2016-09-22 05:15:57 +08:00
|
|
|
|
2016-09-22 22:01:25 +08:00
|
|
|
const staffCount = this.get("step.fieldsById.staff_count.value") || 1;
|
2016-09-22 05:15:57 +08:00
|
|
|
const showWarning = staffCount < 3 && users.length === 0;
|
|
|
|
|
|
|
|
this.set("field.warning", showWarning ? "invites.none_added" : null);
|
2016-09-14 03:14:17 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
addUser() {
|
|
|
|
const user = {
|
2019-05-27 16:15:39 +08:00
|
|
|
email: this.inviteEmail || "",
|
|
|
|
role: this.inviteRole,
|
2016-09-14 03:14:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!/(.+)@(.+){2,}\.(.+){2,}/.test(user.email)) {
|
|
|
|
return this.set("invalid", true);
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
const users = this.users;
|
2016-10-27 03:44:36 +08:00
|
|
|
if (users.findBy("email", user.email)) {
|
2016-09-14 03:14:17 +08:00
|
|
|
return this.set("invalid", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set("invalid", false);
|
|
|
|
|
|
|
|
users.pushObject(user);
|
|
|
|
this.updateField();
|
|
|
|
|
|
|
|
this.set("inviteEmail", "");
|
2020-04-16 13:58:04 +08:00
|
|
|
schedule("afterRender", () =>
|
2019-07-16 18:45:15 +08:00
|
|
|
this.element.querySelector(".invite-email").focus()
|
2016-09-14 03:14:17 +08:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeUser(user) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.users.removeObject(user);
|
2016-09-14 03:14:17 +08:00
|
|
|
this.updateField();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|