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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
1.7 KiB
JavaScript
Raw Normal View History

import Component from "@ember/component";
import I18n from "I18n";
import { schedule } from "@ember/runloop";
export default Component.extend({
2016-09-14 03:14:17 +08:00
classNames: ["invite-list"],
users: null,
inviteEmail: "",
inviteRole: "",
invalid: false,
init() {
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") },
]);
this.set("inviteRole", this.get("roles.0.id"));
2016-09-14 03:14:17 +08:00
this.updateField();
},
keyPress(e) {
if (e.key === "Enter") {
2016-09-14 03:14:17 +08:00
e.preventDefault();
e.stopPropagation();
this.send("addUser");
}
},
updateField() {
const users = this.users;
this.set("field.value", JSON.stringify(users));
2016-09-22 22:01:25 +08:00
const staffCount = this.get("step.fieldsById.staff_count.value") || 1;
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 = {
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);
}
const users = this.users;
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", "");
schedule("afterRender", () =>
this.element.querySelector(".invite-email").focus()
2016-09-14 03:14:17 +08:00
);
},
removeUser(user) {
this.users.removeObject(user);
2016-09-14 03:14:17 +08:00
this.updateField();
},
},
});