-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Streamline delete process for all deletable items
- Loading branch information
Showing
12 changed files
with
49 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
internal/webserver/embedded/views/partials/delete-modal.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters