nopaque/app/static/js/RessourceLists/UserList.js

102 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-01-13 10:57:13 +00:00
class UserList extends RessourceList {
2022-09-02 11:07:30 +00:00
static autoInit() {
for (let userListElement of document.querySelectorAll('.user-list:not(.no-autoinit)')) {
new UserList(userListElement);
}
}
static options = {
2022-09-02 11:07:30 +00:00
initialHtmlGenerator: (id) => {
return `
<div class="input-field">
<i class="material-icons prefix">search</i>
<input id="${id}-search" class="search" type="search"></input>
<label for="${id}-search">Search user</label>
</div>
<table>
<thead>
<tr>
<th>Id</th>
<th>Username</th>
<th>Email</th>
<th>Last seen</th>
<th>Role</th>
<th></th>
</tr>
</thead>
<tbody class="list"></tbody>
</table>
<ul class="pagination"></ul>
`.trim();
},
item: `
2022-09-02 11:07:30 +00:00
<tr class="clickable hoverable">
<td><span class="id-1"></span></td>
<td><span class="username"></span></td>
<td><span class="email"></span></td>
<td><span class="last-seen"></span></td>
<td><span class="role"></span></td>
<td class="right-align">
2022-09-02 11:07:30 +00:00
<a class="action-button btn-floating red waves-effect waves-light" data-action="delete"><i class="material-icons">delete</i></a>
<a class="action-button btn-floating waves-effect waves-light" data-action="edit"><i class="material-icons">edit</i></a>
<a class="action-button btn-floating waves-effect waves-light" data-action="view"><i class="material-icons">send</i></a>
</td>
</tr>
`.trim(),
2022-09-02 11:07:30 +00:00
ressourceMapper: (user) => {
return {
'id': user.id,
'id-1': user.id,
'username': user.username,
'email': user.email,
'last-seen': new Date(user.last_seen).toLocaleString('en-US'),
'member-since': user.member_since,
'role': user.role.name
};
},
2022-09-02 11:07:30 +00:00
sortArgs: ['member-since', {order: 'desc'}],
valueNames: [
{data: ['id']},
{data: ['member-since']},
'email',
'id-1',
'last-seen',
'role',
'username'
]
};
2021-01-13 10:57:13 +00:00
constructor(listElement, options = {}) {
super(listElement, {...UserList.options, ...options});
}
init(users) {
2021-11-30 15:22:16 +00:00
super._init(Object.values(users));
2021-01-13 10:57:13 +00:00
}
2022-09-02 11:07:30 +00:00
onClick(event) {
let actionButtonElement = event.target.closest('.action-button');
let action = actionButtonElement === null ? 'view' : actionButtonElement.dataset.action;
let userElement = event.target.closest('tr');
let userId = userElement.dataset.id;
2021-01-13 10:57:13 +00:00
switch (action) {
2022-09-02 11:07:30 +00:00
case 'delete': {
Utils.deleteUserRequest(userId);
if (userId === currentUserId) {window.location.href = '/';}
2021-01-13 10:57:13 +00:00
break;
2022-09-02 11:07:30 +00:00
}
case 'edit': {
2021-01-13 10:57:13 +00:00
window.location.href = `/admin/users/${userId}/edit`;
break;
2022-09-02 11:07:30 +00:00
}
case 'view': {
2021-11-30 15:22:16 +00:00
window.location.href = `/admin/users/${userId}`;
2021-01-13 10:57:13 +00:00
break;
2022-09-02 11:07:30 +00:00
}
default: {
2021-01-13 10:57:13 +00:00
break;
2022-09-02 11:07:30 +00:00
}
2021-01-13 10:57:13 +00:00
}
}
}