nopaque/app/static/js/resource-lists/admin-user-list.js

108 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-10-11 14:20:17 +00:00
ResourceLists.AdminUserList = class AdminUserList extends ResourceLists.ResourceList {
static htmlClass = 'admin-user-list';
2023-01-04 19:00:37 +00:00
constructor(listContainerElement, options = {}) {
super(listContainerElement, options);
this.listjs.list.addEventListener('click', (event) => {this.onClick(event)});
}
get item() {
return `
2023-01-05 14:03:59 +00:00
<tr class="list-item clickable hoverable">
2023-01-04 19:00:37 +00:00
<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">
2023-01-05 14:03:59 +00:00
<a class="list-action-trigger btn-floating red waves-effect waves-light" data-list-action="delete"><i class="material-icons">delete</i></a>
<a class="list-action-trigger btn-floating waves-effect waves-light" data-list-action="edit"><i class="material-icons">edit</i></a>
<a class="list-action-trigger btn-floating waves-effect waves-light" data-list-action="view"><i class="material-icons">send</i></a>
2023-01-04 19:00:37 +00:00
</td>
</tr>
`.trim();
}
get valueNames() {
return [
{data: ['id']},
{data: ['member-since']},
'email',
'id-1',
'last-seen',
'role',
'username'
];
}
initListContainerElement() {
if (!this.listContainerElement.hasAttribute('id')) {
this.listContainerElement.id = Utils.generateElementId('user-list-');
}
let listSearchElementId = Utils.generateElementId(`${this.listContainerElement.id}-search-`);
this.listContainerElement.innerHTML = `
<div class="input-field">
<i class="material-icons prefix">search</i>
<input id="${listSearchElementId}" class="search" type="text"></input>
<label for="${listSearchElementId}">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();
}
2023-01-05 07:11:27 +00:00
2023-01-04 19:00:37 +00:00
mapResourceToValue(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
};
}
sort() {
this.listjs.sort('member-since', {order: 'desc'});
}
onClick(event) {
let listItemElement = event.target.closest('.list-item[data-id]');
2023-01-05 14:03:59 +00:00
if (listItemElement === null) {return;}
let itemId = listItemElement.dataset.id;
let listActionElement = event.target.closest('.list-action-trigger[data-list-action]');
let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction;
2023-01-05 14:03:59 +00:00
switch (listAction) {
2023-01-04 19:00:37 +00:00
case 'delete': {
2023-10-06 13:04:36 +00:00
Requests.users.entity.delete(itemId);
2023-01-05 14:03:59 +00:00
if (itemId === currentUserId) {window.location.href = '/';}
2023-01-04 19:00:37 +00:00
break;
}
case 'edit': {
2023-01-05 14:03:59 +00:00
window.location.href = `/admin/users/${itemId}/edit`;
2023-01-04 19:00:37 +00:00
break;
}
case 'view': {
2023-01-05 14:03:59 +00:00
window.location.href = `/admin/users/${itemId}`;
2023-01-04 19:00:37 +00:00
break;
}
default: {
break;
}
}
}
2023-10-11 14:20:17 +00:00
};