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 @@

{{t .Lang "%s highlights" .Session.Name}}

{{ $length := len .Paginator.Pages }} {{ if gt $length 1 }} {{template "partials/pagination" .}} {{end}} - -{{template "partials/delete-modal" .}} - diff --git a/internal/webserver/embedded/views/index.html b/internal/webserver/embedded/views/index.html index 765c353..d4156f9 100644 --- a/internal/webserver/embedded/views/index.html +++ b/internal/webserver/embedded/views/index.html @@ -30,13 +30,13 @@

{{t .Lang "Your highlights" }}

{{t $lang "Actions"}} {{ template "partials/actions" dict "Lang" $lang "Document" $doc "EmailSendingConfigured" - $emailSendingConfigured "Index" $i "Session" $session "EmailFrom" $emailFrom "OnDehighlight" "remove"}} + $emailSendingConfigured "Session" $session "EmailFrom" $emailFrom "OnDehighlight" "remove"}} {{end}} - {{template "partials/delete-modal" .}} + {{template "partials/delete-modal" dict "Lang" .Lang "Action" "/document" "ModalHeader" "Delete document" "ModalBody" "Are you sure you want to delete this document?" "ModalErrorMessage" "There was an error deleting the document"}} diff --git a/internal/webserver/embedded/views/partials/actions.html b/internal/webserver/embedded/views/partials/actions.html index 41e018d..0d1c99c 100644 --- a/internal/webserver/embedded/views/partials/actions.html +++ b/internal/webserver/embedded/views/partials/actions.html @@ -69,7 +69,7 @@ {{end}} {{if and .Session (eq .Session.Role 2)}} -
  • +
  • diff --git a/internal/webserver/embedded/views/partials/delete-modal.html b/internal/webserver/embedded/views/partials/delete-modal.html index 9adb988..cb4f2f9 100644 --- a/internal/webserver/embedded/views/partials/delete-modal.html +++ b/internal/webserver/embedded/views/partials/delete-modal.html @@ -1,15 +1,15 @@ - @@ -91,7 +91,7 @@
    {{t $lang "Unknown author"}}
    {{end}} {{end}} -{{template "partials/delete-modal" .}} +{{template "partials/delete-modal" dict "Lang" $lang "Action" "/document" "ModalHeader" "Delete document" "ModalBody" "Are you sure you want to delete this document?" "ModalErrorMessage" "There was an error deleting the document"}} diff --git a/internal/webserver/embedded/views/users/index.html b/internal/webserver/embedded/views/users/index.html index e10cb95..fb18600 100644 --- a/internal/webserver/embedded/views/users/index.html +++ b/internal/webserver/embedded/views/users/index.html @@ -26,7 +26,7 @@

    {{t $lang "Users"}}

    {{end}} {{ if not (and (eq $admins 1) (eq $user.Role 2)) }} -
    + @@ -40,65 +40,6 @@

    {{t $lang "Users"}}

    {{template "partials/pagination" .}} {{end}} - - - - + diff --git a/internal/webserver/highlights_test.go b/internal/webserver/highlights_test.go index 034af97..13a737b 100644 --- a/internal/webserver/highlights_test.go +++ b/internal/webserver/highlights_test.go @@ -110,6 +110,10 @@ func TestHighlights(t *testing.T) { t.Fatalf("Unexpected error: %v", err.Error()) } + data = url.Values{ + "id": {"john-doe-test-epub"}, + } + _, err = deleteRequest(data, adminCookie, app, "/document") if err != nil { t.Fatalf("Unexpected error: %v", err.Error()) @@ -148,7 +152,7 @@ func TestHighlights(t *testing.T) { } data := url.Values{ - "uuid": {regularUser.Uuid}, + "id": {regularUser.Uuid}, } _, err = deleteRequest(data, adminCookie, app, "/users") diff --git a/internal/webserver/search_test.go b/internal/webserver/search_test.go index 6382530..b507040 100644 --- a/internal/webserver/search_test.go +++ b/internal/webserver/search_test.go @@ -155,7 +155,7 @@ func TestRemoveDocument(t *testing.T) { ) data := url.Values{ - "slug": {tcase.slug}, + "id": {tcase.slug}, } cookie, err := login(app, tcase.email, tcase.password) diff --git a/internal/webserver/user_management_test.go b/internal/webserver/user_management_test.go index 0c59701..d34843b 100644 --- a/internal/webserver/user_management_test.go +++ b/internal/webserver/user_management_test.go @@ -226,7 +226,7 @@ func TestUserManagement(t *testing.T) { }) data = url.Values{ - "uuid": {testUser.Uuid}, + "id": {testUser.Uuid}, } t.Run("Try to delete a user without an active session", func(t *testing.T) { @@ -264,7 +264,7 @@ func TestUserManagement(t *testing.T) { t.Run("Try to delete the only existing admin user", func(t *testing.T) { data = url.Values{ - "uuid": {adminUser.Uuid}, + "id": {adminUser.Uuid}, } response, err := deleteRequest(data, adminCookie, app, "/users") if response == nil { @@ -276,7 +276,7 @@ func TestUserManagement(t *testing.T) { t.Run("Try to delete a non existing user with an admin session", func(t *testing.T) { data = url.Values{ - "uuid": {"abcde"}, + "id": {"abcde"}, } response, err := deleteRequest(data, adminCookie, app, "/users")