Add contact list and deletion

This commit is contained in:
Ninjdai 2023-12-12 13:55:35 +01:00
parent 7caff58cdf
commit 8a8b21ba85
4 changed files with 74 additions and 2 deletions

View File

@ -8,7 +8,7 @@ export default {
async execute(request, response) {
const { phone } = request.params;
const contact = await global.database.contacts.findOne({
where: { phone: phone },
where: { phone: phone.replaceAll("%20", " ") },
});
if (!contact) return response.status(404).send({ message: "Contact does not exist" });

View File

@ -0,0 +1,41 @@
import { navbar } from '../../../../../utils/navbar.js';
import { readFile } from 'fs/promises';
import { permissionBits, checkPermissions } from '../../../../../utils/permissions.js';
export default {
path: "/dashboard/contacts/list",
requiresLogin: true,
permissions: permissionBits.ADMIN,
type: "get",
async execute(request, response) {
const contactList = await global.database.contacts.findAll();
const contactTable = genContactTable(contactList);
const html = await readFile(`${process.env.WWW}/dashboard/contacts/list.html`);
return await response.send(html.toString()
.replace('<NAVBAR>', navbar(request.session))
.replace('<CONTACTTABLE>', contactTable)
);
},
}
function genContactTable(contacts) {
let res = `
<table>
<tr>
<th>Téléphone</th>
<th>Prénom</th>
<th>Nom</th>
<th>Action</th>
</tr>`;
for(const contact of contacts) {
res += `
<tr>
<td>${contact.phone}</td>
<td>${contact.firstName}</td>
<td>${contact.firstName}</td>
<td><button onclick="deleteContact('${contact.phone}');">Supprimer</button></td>
</tr>`;
}
res += `</table>`;
return res
}

View File

@ -30,7 +30,7 @@ body {
}
/* Full-width input fields */
input[type=text], input[type=password] {
input[type=text], input[type=password], input[type=tel] {
width: 100%;
padding: 15px;
margin: 5px 0 22px 0;

View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Utilisateurs</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
table, th, td {
border:1px solid black;
}
</style>
</head>
<body>
<script>
function deleteContact(phone) {
fetch(`/api/contacts/${phone}`, {
method: "DELETE",
})
.then(async (response) => {
const res = await response.json();
console.log(res);
if(!response.ok) return alert(res.message);
location.reload();
});
}
</script>
<NAVBAR>
<CONTACTTABLE>
<a href='./'><p>Retour</p></a>
</body>
</html>