From 3d53b673fd87156e8d528871b98c6ba8d1d3ec96 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Wed, 13 Jan 2021 11:57:13 +0100 Subject: [PATCH] Implement UserList --- web/app/static/js/nopaque/lists/Userlist.js | 71 +++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 web/app/static/js/nopaque/lists/Userlist.js diff --git a/web/app/static/js/nopaque/lists/Userlist.js b/web/app/static/js/nopaque/lists/Userlist.js new file mode 100644 index 00000000..c1abce8f --- /dev/null +++ b/web/app/static/js/nopaque/lists/Userlist.js @@ -0,0 +1,71 @@ +class UserList extends RessourceList { + constructor(listElement, options = {}) { + super(listElement, {...UserList.options, ...options}); + users = undefined; + } + + init(users) { + this.users = users; + super.init(users); + } + + onclick(event) { + let ressourceElement = event.target.closest('tr'); + if (ressourceElement === null) {return;} + let userId = ressourceElement.dataset.id; + let actionButtonElement = event.target.closest('.action-button'); + let action = (actionButtonElement === null) ? 'view' : actionButtonElement.dataset.action; + switch (action) { + case 'delete': + let deleteModalHTML = ``; + let deleteModalParentElement = document.querySelector('main'); + deleteModalParentElement.insertAdjacentHTML('beforeend', deleteModalHTML); + let deleteModalElement = deleteModalParentElement.lastChild; + let deleteModal = M.Modal.init(deleteModalElement, {onCloseEnd: () => {deleteModal.destroy(); deleteModalElement.remove();}}); + deleteModal.open(); + break; + case 'edit': + window.location.href = `/admin/users/${userId}/edit`; + break; + case 'view': + if (userId !== '-1') {window.location.href = `/admin/users/${userId}`;} + break; + default: + console.error(`Unknown action: ${action}`); + break; + } + } + + preprocessRessource(user) { + return {id: user.id, + id_: user.id, + username: user.username, + email: user.email, + last_seen: new Date(user.last_seen * 1000).toLocaleString("en-US"), + role: user.role.name}; + } +} +UserList.options = { + item: ` + + + + + + + delete + edit + send + + `, + valueNames: [{data: ['id']}, 'id_', 'username', 'email', 'last_seen', 'role'] +};