diff --git a/internal/webserver/controller/document/delete.go b/internal/webserver/controller/document/delete.go index 2cf25c5..f5f8876 100644 --- a/internal/webserver/controller/document/delete.go +++ b/internal/webserver/controller/document/delete.go @@ -9,11 +9,11 @@ import ( ) func (d *Controller) Delete(c *fiber.Ctx) error { - if c.FormValue("slug") == "" { + if c.FormValue("id") == "" { return fiber.ErrBadRequest } - document, err := d.idx.Document(c.FormValue("slug")) + document, err := d.idx.Document(c.FormValue("id")) if err != nil { fmt.Println(err) return fiber.ErrBadRequest diff --git a/internal/webserver/controller/user/delete.go b/internal/webserver/controller/user/delete.go index eadab90..fe4f527 100644 --- a/internal/webserver/controller/user/delete.go +++ b/internal/webserver/controller/user/delete.go @@ -7,7 +7,7 @@ import ( // Delete removes a user from the database func (u *Controller) Delete(c *fiber.Ctx) error { - user, err := u.repository.FindByUuid(c.FormValue("uuid")) + user, err := u.repository.FindByUuid(c.FormValue("id")) if err != nil { return fiber.ErrInternalServerError } @@ -20,7 +20,7 @@ func (u *Controller) Delete(c *fiber.Ctx) error { return fiber.ErrForbidden } - if err = u.repository.Delete(c.FormValue("uuid")); err != nil { + if err = u.repository.Delete(c.FormValue("id")); err != nil { return fiber.ErrInternalServerError } diff --git a/internal/webserver/embedded/js/delete.js b/internal/webserver/embedded/js/delete.js index 74ec9aa..bce0546 100644 --- a/internal/webserver/embedded/js/delete.js +++ b/internal/webserver/embedded/js/delete.js @@ -1,41 +1,50 @@ -const deleteModal = document.getElementById('deleteModal'); +"use strict" + +// We use several conventions to be able to use the same code to delete different resources. +// The link that initiates the action needs to have an attribute called data-id which must contain an unique identifier +// for the resource to delete. +// This identifier well be sent to the backend controller specified in the form's action attribute +// under the name "id". +// This code is designed to be used alongside partials/delete-modal.html + +const deleteModal = document.getElementById('delete-modal'); +const deleteForm = document.getElementById('delete-form'); deleteModal.addEventListener('show.bs.modal', event => { - const link = event.relatedTarget; - const slug = link.getAttribute('data-bs-slug'); - const modalInputSlug = deleteModal.querySelector('.slug'); + const link = event.relatedTarget + const id = link.getAttribute('data-id') + const modalInput = deleteModal.querySelector('.id') - modalInputSlug.value = slug; + modalInput.value = id; }) deleteModal.addEventListener('hidden.bs.modal', event => { - let message = document.getElementById('delete-document-message'); + let message = document.getElementById('error-message-container'); message.classList.add("visually-hidden"); }) -function remove(errorMessage) { +deleteForm.addEventListener('submit', event => { event.preventDefault(); - form = document.getElementById("delete-form"); - fetch('/document', { + fetch(deleteForm.getAttribute("action"), { method: "DELETE", headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ - 'slug': form.elements['slug'].value, + 'id': deleteForm.elements['id'].value, }) }) .then((response) => { if (response.ok) { location.reload(); } else { - message = document.getElementById("delete-document-message") + message = document.getElementById("error-message-container") message.classList.remove("visually-hidden"); - message.innerHTML = errorMessage; + message.innerHTML = deleteForm.getAttribute("data-error-message"); } }) .catch(function (error) { // Catch errors console.log(error); }); -} +}) diff --git a/internal/webserver/embedded/views/highlights.html b/internal/webserver/embedded/views/highlights.html index 2854121..7eed404 100644 --- a/internal/webserver/embedded/views/highlights.html +++ b/internal/webserver/embedded/views/highlights.html @@ -12,6 +12,3 @@