Newer
Older
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
// Imports
import { AnimateCSS } from "/services/common/common.js";
import * as Auth from "/services/auth/auth.js";
import { Delete } from "/services/common/delete.js";
import * as HandleUser from "/components/users/handleUser.js";
import * as HandleBanker from "/components/users/handleBanker.js";
import * as HandleClient from "/components/users/handleClient.js";
// DOM elements
// local variables
let current_user;
export async function mount(where) {
const userPage = new Users();
await userPage.mount(where);
}
class Users {
constructor() {
}
async mount(where) {
const mountpoint = where;
document.getElementById(mountpoint).innerHTML = /* HTML */ `
<div class="table-container">
<table class="table is-bordered is-narrow is-hoverable is-fullwidth">
<thead>
<tr class="is-selected">
<th>Id</th>
<th>IdOAuth</th>
<th>Identifiant</th>
<th>Nom complet</th>
<th>Nom</th>
<th>Surnom</th>
<th>Rôle</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="users"></tbody>
</table>
</div>
<button id="users-new" class="button is-primary">
<span class="icon is-small">
<i class="fas fa-plus"></i>
</span>
</button>
<div class="modal" id="users-modal"></div>
<div class="modal" id="banker-modal"></div>
<div class="modal" id="client-modal"></div>
`;
current_user = await Auth.GetUser();
this.HandleUser = await HandleUser.mount("users-modal", this);
this.HandleBanker = await HandleBanker.mount("banker-modal", this);
this.HandleClient = await HandleClient.mount("client-modal", this);
await this.displayUsers();
}
async displayUsers() {
let users = await this.HandleUser.updateUsers();
const markup = users.map((user) => this.userTemplate(user)).join("");
document.getElementById("users").innerHTML = markup;
let userHandler = this.HandleUser;
users.map((user) => {
document.getElementById(`users-user-edit-${user.id}`).addEventListener("click", function () {
userHandler.editUser(user);
});
document.getElementById(`users-user-delete-${user.id}`).addEventListener("click", function () {
new Delete(() => {
userHandler.deleteUser(user);
});
});
});
}
cleanUser(user) {
let props = ["password", "name", "surname", "memberOf", "displayName"];
for (const prop of props) {
user[prop] = user[prop] === undefined ? "" : user[prop];
}
}
userTemplate(user) {
this.cleanUser(user);
return /* HTML */ `
<tr id="users-user-${user.id}">
<td>${user.id}</td>
<td>${user.idOAuth}</td>
<td>${user.login}</td>
<td>${user.displayName == undefined ? "" : user.displayName}</td>
<td>${user.name}</td>
<td>${user.surname}</td>
<td>${user.role}</td>
<td>
<a id="users-user-edit-${user.id}" class="button is-link is-small">
<span>Modifier</span>
<span class="icon is-small">
<i class="fas fa-pen"></i>
</span>
</a>
<a id="users-user-delete-${user.id}" class="button is-danger is-small">
<span>Supprimer</span>
<span class="icon is-small">
<i class="fas fa-times"></i>
</span>
</a>
</td>
</tr>
`;
}
}
export function toggleModal(modalID, cardID) {
const modal = document.getElementById(modalID);
const card = document.getElementById(cardID);
if (modal.classList.contains("is-active")) {
AnimateCSS(modal, "fadeOut");
AnimateCSS(card, "zoomOut", function () {
modal.classList.remove("is-active");
});
} else {
modal.classList.add("is-active");
AnimateCSS(modal, "fadeIn");
AnimateCSS(card, "zoomIn");
}
}