diff --git a/assets/script.js b/assets/script.js new file mode 100644 index 0000000..f987c2c --- /dev/null +++ b/assets/script.js @@ -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!"); + } +} \ No newline at end of file diff --git a/src/server/web_ui.rs b/src/server/web_ui.rs index 5e91c22..237d171 100644 --- a/src/server/web_ui.rs +++ b/src/server/web_ui.rs @@ -50,6 +50,9 @@ pub struct FormRequest { /// Restrict new client to a given network ip_network: Option, + + /// Delete a specified client id + delete_client_id: Option, } /// Main route @@ -114,6 +117,14 @@ pub async fn home(session: Session, form_req: Option>) -> 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 diff --git a/templates/index.html b/templates/index.html index ca240aa..93dd0ca 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,6 +7,7 @@ + @@ -77,7 +78,7 @@ {{ client.fmt_created() }} {{ client.fmt_used() }} - + {% endfor %}