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
// Imports
import * as Auth from "/services/auth/auth.js";
import { Delete } from "/services/common/delete.js";
import * as HandleUser from "/components/users/handleUser.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</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>
`;
current_user = await Auth.GetUser();
this.HandleUser = await HandleUser.mount("users-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) {
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.name}</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>
`;
}