-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcomponent.js
167 lines (144 loc) · 4.64 KB
/
component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { Changeset } from "ember-changeset";
import lookupValidator from "ember-changeset-validations";
import { dropTask } from "ember-concurrency";
import { trackedFunction } from "reactiveweb/function";
import UIkit from "uikit";
import applyError from "mysagw/utils/apply-error";
import IdentityValidations from "mysagw/validations/identity";
/**
* @arg identity
* @arg onSave
* @arg onOrganisationUpdate
*/
export default class IdentityFormComponent extends Component {
@service notification;
@service store;
@service intl;
@service router;
@tracked changeset;
@tracked backToIdentities;
changesetResource = trackedFunction(this, async () => {
this.changeset = Changeset(
this.args.identity || this.store.createRecord("identity"),
lookupValidator(IdentityValidations),
IdentityValidations,
);
});
get salutations() {
return [
{ label: this.intl.t("global.salutation.neutral"), value: "neutral" },
{ label: this.intl.t("global.salutation.male"), value: "male" },
{ label: this.intl.t("global.salutation.female"), value: "female" },
];
}
get titles() {
return [
{ label: this.intl.t("global.title.none"), value: "none" },
{ label: this.intl.t("global.title.dr"), value: "dr" },
{ label: this.intl.t("global.title.prof"), value: "prof" },
{ label: this.intl.t("global.title.profDr"), value: "prof-dr" },
{ label: this.intl.t("global.title.pdDr"), value: "pd-dr" },
];
}
get languages() {
return [
{ label: this.intl.t("global.languages.german"), value: "de" },
{ label: this.intl.t("global.languages.english"), value: "en" },
{ label: this.intl.t("global.languages.french"), value: "fr" },
];
}
get keyCloakAccountUrl() {
const host =
location.hostname === "localhost" ? "mysagw.local" : location.hostname;
if (this.args.customEndpoint === "me") {
return `https://${host}/auth/realms/mysagw/account/#/personal-info`;
}
return [
`https://${host}/auth/admin/mysagw/console/`,
"#/mysagw/users/",
this.changeset.get("idpId"),
"/settings",
].join("");
}
get cancelRoute() {
return this.args.cancelRouteOverride || "identities";
}
@action
eventTarget(handler, event) {
handler(event.target.value);
}
@action
updateOrganisation() {
const isOrganisation = !this.changeset.get("isOrganisation");
this.args.onOrganisationUpdate?.(isOrganisation);
this.changeset.set("isOrganisation", isOrganisation);
}
@action
setBackToIdentities() {
this.backToIdentities = true;
this.submit.perform(this.changeset);
}
@dropTask
*submit(changeset) {
try {
if (!changeset.get("isOrganisation")) {
changeset.set("organisationName", null);
changeset.set("isExpertAssociation", false);
changeset.set("isAdvisoryBoard", false);
}
yield changeset.save({
adapterOptions: { customEndpoint: this.args.customEndpoint },
});
this.notification.success(
this.intl.t("components.identity-form.success", {
name: changeset.data.fullName,
}),
);
this.args.onSave?.(changeset.data);
if (this.backToIdentities) {
this.router.transitionTo(this.cancelRoute);
}
} catch (error) {
console.error(error);
this.notification.fromError(error);
applyError(changeset, error);
}
}
@dropTask
*delete(identity) {
try {
let message = this.intl.t("components.identity-form.delete.prompt");
const accesses = yield this.store.query("case-access", {
filter: { identityIds: this.args.identity.id },
});
message += `\n${this.intl.t(
"components.identity-form.delete.promptInfo",
{
caseAmount: accesses.length,
},
)}`;
const modal = UIkit.modal.confirm(message);
// We need to add css white-space rule for the new line
modal.dialog.$el.classList.add("white-space-pre-line");
yield modal;
try {
yield identity.destroyRecord();
this.notification.success(
this.intl.t("components.identity-form.delete.success"),
);
this.router.transitionTo("identities");
} catch (error) {
console.error(error);
this.notification.fromError(error);
}
} catch (error) {
// Dialog was dimissed. No action necessary.
// Log the error anyway in case something else broke in the try.
console.error(error);
}
}
}