assetflow/static/js/formHandler.js

117 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-05-16 22:58:08 +02:00
var amountOfForms = $('.frontendForm').length;
2023-05-17 19:06:38 +02:00
$('.frontendForm').each(function () {
2023-06-06 23:49:16 +02:00
// TODO Handle empty strings as null or undefined, not as ''
2023-05-17 19:06:38 +02:00
$(this).on('submit', function (e) {
e.preventDefault(); // Prevent the form from submitting via the browser
2023-05-15 18:49:02 +02:00
2023-05-17 19:06:38 +02:00
var form = $(this); // Get the form
2023-05-15 18:49:02 +02:00
// Show overlay with spinner
$('.loader-overlay').addClass('loaderActive');
2023-05-17 19:06:38 +02:00
// Get the form data
2023-05-15 18:49:02 +02:00
formData = form.serializeArray();
2023-05-17 19:06:38 +02:00
2023-05-15 18:49:02 +02:00
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('data-target'),
data: formData,
dataType: 'json',
2023-05-17 19:06:38 +02:00
success: function (data) {
2023-05-15 18:49:02 +02:00
console.log('success');
// Hide overlay with spinner
$('.loader-overlay').removeClass('loaderActive');
// Close the modal
$('.modal').modal('hide');
// Clear all fields
form.find('input, textarea').val('');
2023-05-17 17:46:51 +02:00
// Create toast
2023-05-17 19:06:38 +02:00
createNewToast('<i class="bi bi-check2"></i> Changes saved successfully.', "text-bg-success")
2023-05-15 18:49:02 +02:00
},
2023-05-17 19:06:38 +02:00
error: function (data) {
2023-05-15 18:49:02 +02:00
console.log('error');
// Hide overlay with spinner
$('.loader-overlay').removeClass('loaderActive');
2023-05-16 22:58:08 +02:00
2023-05-17 19:06:38 +02:00
// Check for response code 409 (duplicate entry)
2023-05-16 22:58:08 +02:00
if (data.status == 409) {
2023-05-17 19:06:38 +02:00
createNewToast('<i class="bi bi-exclamation-triangle-fill"></i> The element you tried to create already exists.', "text-bg-danger", 3000, false)
} else {
createNewToast('<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. Please try again later.', "text-bg-danger", 3000, false)
2023-05-16 22:58:08 +02:00
}
2023-05-15 18:49:02 +02:00
}
});
})
});
2023-05-16 22:58:08 +02:00
2023-05-17 19:06:38 +02:00
/**
* Generic function to handle the result of a deletion prompt
* @param {Number} id ID of the entry to delete
* @param {String} route Route to send the delete request to will be templated as /api/v1/{route}
* @param {String} name Type of entry to delete, will be templated as {name} deleted successfully.
*/
function deleteEntryNxt(id, route, name) {
$.ajax({
type: 'delete',
url: `/api/v1/` + route,
data: { id: id },
success: function (data) {
$('#staticBackdrop').modal('hide');
createNewToast(`<i class="bi bi-check2"></i> ${name} deleted successfully.`, "text-bg-success")
confetti({
spread: 360,
ticks: 100,
gravity: 0.1,
decay: 0.94,
startVelocity: 30,
particleCount: 20,
scalar: 2,
shapes: ['text'],
shapeOptions: {
text: {
value: ['❌', '🗑️', '🚫']
}
}
});
},
error: function (data) {
// hide the staticBackdrop modal
$('#staticBackdrop').modal('hide');
createNewToast('<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. Please try again later.', "text-bg-danger", 3000, false)
}
});
}
/**
* Generic route to trigger a prefill of the delete modal
* @param {Number} id The ID of the entry to delete
* @param {String} route The endpoint to send the delete request to, will be templated as /api/v1/{route}
* @param {String} name The name of the entry to delete, will be templated as {name} deleted successfully.
*/
function preFillDeleteModalNxt(id, route, name, requestIdent='id') {
$.ajax({
type: 'get',
url: `/api/v1/${route}?${requestIdent}=${id}`,
success: function (data) {
const result = JSON.parse(data);
// Get elements inside the editCategoryModal
const modal_categoryName = document.getElementById('deleteNamePlaceholder');
const modal_deleteButton = document.getElementById('deleteActionBtn');
modal_categoryName.innerText = result.name;
modal_deleteButton.setAttribute('onclick', `deleteEntryNxt(${result.id},'${route}','${name}')`);
},
error: function (data) {
console.log('!!!! ERROR !!!!', data);
document.getElementById('deleteNamePlaceholder').innerText = 'Deleted';
$('#staticBackdrop').modal('hide');
2023-06-06 23:49:16 +02:00
createNewToast(`<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. The ${name} does no longer exist.`, `text-bg-danger`)
2023-05-17 19:06:38 +02:00
}
});
}
2023-05-16 22:58:08 +02:00
console.info("Found " + amountOfForms + " forms on this page.")