Add contact list and deletion
This commit is contained in:
parent
7caff58cdf
commit
8a8b21ba85
@ -8,7 +8,7 @@ export default {
|
|||||||
async execute(request, response) {
|
async execute(request, response) {
|
||||||
const { phone } = request.params;
|
const { phone } = request.params;
|
||||||
const contact = await global.database.contacts.findOne({
|
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" });
|
if (!contact) return response.status(404).send({ message: "Contact does not exist" });
|
||||||
|
|
||||||
|
41
src/html/pages/dashboard/contacts/list.js
Normal file
41
src/html/pages/dashboard/contacts/list.js
Normal 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
|
||||||
|
}
|
@ -30,7 +30,7 @@ body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Full-width input fields */
|
/* Full-width input fields */
|
||||||
input[type=text], input[type=password] {
|
input[type=text], input[type=password], input[type=tel] {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
padding: 15px;
|
padding: 15px;
|
||||||
margin: 5px 0 22px 0;
|
margin: 5px 0 22px 0;
|
||||||
|
31
www/dashboard/contacts/list.html
Normal file
31
www/dashboard/contacts/list.html
Normal 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>
|
Loading…
Reference in New Issue
Block a user