2025-08-01 18:37:49 +07:00

195 lines
6.2 KiB
JavaScript

function addKalori(){
new bootstrap.Modal(modalKalori).show();
formKalori.attr('action', `/dashboard/kalori`)
}
formKalori.on('submit', function(e){
e.preventDefault();
const form = this;
const actionUrl = formKalori.attr('action');
const formData = new FormData(form);
fetch(actionUrl, {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('input[name="_token"]').value,
},
body: formData
}).then(async(res) => {
const responseData = await res.json();
if (res.status === 422) {
// Tampilkan semua error ke swal
const errors = responseData.errors || {};
let htmlList = '<ul class="text-start">';
for (const key in errors) {
errors[key].forEach(message => {
htmlList += `<li>${message}</li>`;
});
}
htmlList += '</ul>';
Swal.fire({
icon: 'error',
title: 'Validasi Gagal',
html: htmlList,
});
throw new Error(); // Supaya masuk ke catch, tapi tidak tampil swal lagi
}
if (responseData.status) {
const handler = function () {
Swal.fire({
icon: 'success',
title: 'Berhasil',
text: responseData.message || 'Berhasil melakukan aksi!',
timer: 2000,
showConfirmButton: false,
backdrop: true,
});
$("#col_add_kalori").html('');
colCount = 0; // reset counter
formKalori[0].reset();
datatableKalori.bootstrapTable('refresh');
modalKalori.removeEventListener('hidden.bs.modal', handler);
};
modalKalori.addEventListener('hidden.bs.modal', handler);
bootstrap.Modal.getInstance(modalKalori).hide();
} else {
throw new Error(responseData.message || 'Terjadi kesalahan saat menyimpan data.');
}
}).catch(err => {
if (err.message) {
Swal.fire({
icon: 'error',
title: 'Gagal',
text: err.message
});
}
});
});
function deleteKalori(e){
let id =$(e).data('kalori_id')
Swal.fire({
title: "Apakah kamu yakin ingin menghapus data ini?",
text : $(e).data('nilai_kalori'),
icon: "warning",
showCancelButton: true,
backdrop: true,
}).then((result) => {
if(result.isConfirmed){
fetch(`/dashboard/kalori/${id}`, {
method:'DELETE',
headers: {
"X-CSRF-TOKEN": document.querySelector('input[name="_token"]').value,
"Content-Type": "application/json"
}
}).then((response) => {
if(!response.ok){
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
if(data.status){
Swal.fire({
title: "Success",
text: "Data berhasil dihapus",
icon:"success",
showConfirmButton: false,
timer: 1000
}).then(() => {
datatableKalori.bootstrapTable("refresh")
})
}else{
Swal.fire({
title: "Error!",
text: data.message || "Failed to delete Item.",
icon: "error"
});
}
})
.catch(error => {
Swal.fire({
title: "Error!",
text: "Something went wrong. Please try again later.",
icon: "error"
});
});
}
})
}
function editKalori(e){
const data = $(e).data();
new bootstrap.Modal(modalKaloriEdit).show();
formKaloriEdit.attr('action', `/dashboard/kalori/${data?.kalori_id}`)
$("#nilai_kalori_edit").val(data.nilai_kalori)
}
formKaloriEdit.on('submit', function(e){
e.preventDefault();
const form = this;
const actionUrl = formKaloriEdit.attr('action');
const formData = new FormData(form);
formData.append('_method', 'PUT')
fetch(actionUrl, {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('input[name="_token"]').value,
},
body: formData
}).then(async(res) => {
const responseData = await res.json();
if (res.status === 422) {
// Tampilkan semua error ke swal
const errors = responseData.errors || {};
Swal.fire({
icon: 'error',
title: 'Validasi Gagal',
html: errors.nilai_kalori[0],
});
throw new Error(); // Supaya masuk ke catch, tapi tidak tampil swal lagi
}
if (responseData.status) {
const handler = function () {
Swal.fire({
icon: 'success',
title: 'Berhasil',
text: responseData.message || 'Berhasil melakukan aksi!',
timer: 2000,
showConfirmButton: false,
backdrop: true,
});
$("#nama_kategori_diet").val('');
colCount = 0; // reset counter
formKaloriEdit[0].reset();
datatableKalori.bootstrapTable('refresh');
modalKaloriEdit.removeEventListener('hidden.bs.modal', handler);
};
modalKaloriEdit.addEventListener('hidden.bs.modal', handler);
bootstrap.Modal.getInstance(modalKaloriEdit).hide();
} else {
throw new Error(responseData.message || 'Terjadi kesalahan saat menyimpan data.');
}
}).catch(err => {
if (err.message) {
Swal.fire({
icon: 'error',
title: 'Gagal',
text: err.message
});
}
});
});