Can remove clients

This commit is contained in:
Pierre HUBERT 2025-01-27 22:07:18 +01:00
parent 2c14281ae3
commit 9a46cbd819
3 changed files with 43 additions and 1 deletions

30
assets/script.js Normal file
View File

@ -0,0 +1,30 @@
/**
* Delete a client referenced by its ID
*
* @param clientID The ID of the client to delete
*/
async function deleteClient(clientID) {
if(!confirm("Do you really want to remove client " + clientID + "? The operation cannot be reverted!"))
return;
try {
const res = await fetch("/", {
method: "POST",
headers:{
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
"delete_client_id": clientID
}),
});
if(res.status !== 200)
throw new Error(`Invalid status code: ${res.status}`);
alert("The client was successfully deleted!");
location.reload();
} catch (e) {
console.error(`Failed to delete client: ${e}`);
alert("Failed to delete client!");
}
}

View File

@ -50,6 +50,9 @@ pub struct FormRequest {
/// Restrict new client to a given network /// Restrict new client to a given network
ip_network: Option<String>, ip_network: Option<String>,
/// Delete a specified client id
delete_client_id: Option<uuid::Uuid>,
} }
/// Main route /// Main route
@ -114,6 +117,14 @@ pub async fn home(session: Session, form_req: Option<web::Form<FormRequest>>) ->
config.save().await?; config.save().await?;
} }
} }
// Delete a client
if let Some(delete_client_id) = form_req.0.delete_client_id {
config.clients.retain(|c| c.id != delete_client_id);
config.save().await?;
success_message = Some("The client was successfully deleted!".to_string());
// TODO : close connections with given id
}
} }
// Render page // Render page

View File

@ -7,6 +7,7 @@
<link rel="stylesheet" href="/assets/bootstrap.css"/> <link rel="stylesheet" href="/assets/bootstrap.css"/>
<link rel="stylesheet" href="/assets/style.css"/> <link rel="stylesheet" href="/assets/style.css"/>
<script src="/assets/script.js"></script>
</head> </head>
<body> <body>
@ -77,7 +78,7 @@
<td>{{ client.fmt_created() }}</td> <td>{{ client.fmt_created() }}</td>
<td>{{ client.fmt_used() }}</td> <td>{{ client.fmt_used() }}</td>
<td> <td>
<button type="button" class="btn btn-danger btn-sm">Delete</button> <button type="button" class="btn btn-danger btn-sm" onclick="deleteClient('{{ client.id }}');">Delete</button>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}