all users front end

This commit is contained in:
vinchent 2024-08-23 14:26:12 +02:00
parent f39c000e5d
commit 05db85eca1
2 changed files with 40 additions and 1 deletions

View File

@ -21,5 +21,8 @@ All Users
</table> </table>
{{ end }} {{ end }}
{{ define "js" }} {{ define "js" }}
<script>let tbody = document.getElementById("user-table").getElementByTagName("tbody")[0]</script> <script type="module">
import {showUsers} from "/static/js/all-users.js"
showUsers({{.API}});
</script>
{{ end }} {{ end }}

36
static/js/all-users.js Normal file
View File

@ -0,0 +1,36 @@
export function showUsers(api) {
const tbody = document.getElementById("user-table").getElementsByTagName("tbody")[0];
const token = localStorage.getItem("token");
const requestOptions = {
method: 'post',
headers: {
'Accept': `application/json`,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
};
fetch(api + "/api/admin/all-users", requestOptions)
.then(response => response.json())
.then(function (data) {
if (data) {
data.forEach(i => {
let newRow = tbody.insertRow();
let newCell = newRow.insertCell();
newCell.innerHTML = `<a href="/admin/all-users/${i.id}">${i.last_name}, ${i.first_name}</a>`;
newCell = newRow.insertCell();
let item = document.createTextNode(i.email);
newCell.appendChild(item);
});
} else {
let newRow = tbody.insertRow();
let newCell = newRow.insertCell();
newCell.setAttribute("colspan", "2");
newCell.innerHTML = "no data available";
}
});
}