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