195 lines
6.3 KiB
JavaScript
195 lines
6.3 KiB
JavaScript
function addKarbohidrat(){
|
|
new bootstrap.Modal(modalKarbohidrat).show();
|
|
formKarbohidrat.attr('action', `/dashboard/karbohidrat`)
|
|
}
|
|
|
|
formKarbohidrat.on('submit', function(e){
|
|
e.preventDefault();
|
|
|
|
const form = this;
|
|
const actionUrl = formKarbohidrat.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_karbohidrat").html('');
|
|
colCount = 0; // reset counter
|
|
formKarbohidrat[0].reset();
|
|
datatableKarbohidrat.bootstrapTable('refresh');
|
|
modalKarbohidrat.removeEventListener('hidden.bs.modal', handler);
|
|
};
|
|
modalKarbohidrat.addEventListener('hidden.bs.modal', handler);
|
|
bootstrap.Modal.getInstance(modalKarbohidrat).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 deleteKarbohidrat(e){
|
|
let id =$(e).data('karbohidrat_id')
|
|
Swal.fire({
|
|
title: "Apakah kamu yakin ingin menghapus data ini?",
|
|
text : $(e).data('nama'),
|
|
icon: "warning",
|
|
showCancelButton: true,
|
|
backdrop: true,
|
|
}).then((result) => {
|
|
if(result.isConfirmed){
|
|
fetch(`/dashboard/karbohidrat/${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(() => {
|
|
datatableKarbohidrat.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 editKarbohidrat(e){
|
|
const data = $(e).data();
|
|
new bootstrap.Modal(modalKarbohidratEdit).show();
|
|
formKarbohidratEdit.attr('action', `/dashboard/karbohidrat/${data?.karbohidrat_id}`)
|
|
$("#nama_karbohidrat").val(data.nama)
|
|
}
|
|
|
|
formKarbohidratEdit.on('submit', function(e){
|
|
e.preventDefault();
|
|
|
|
const form = this;
|
|
const actionUrl = formKarbohidratEdit.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.nama_karbohidrat[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
|
|
formKarbohidratEdit[0].reset();
|
|
datatableKarbohidrat.bootstrapTable('refresh');
|
|
modalKarbohidratEdit.removeEventListener('hidden.bs.modal', handler);
|
|
};
|
|
modalKarbohidratEdit.addEventListener('hidden.bs.modal', handler);
|
|
bootstrap.Modal.getInstance(modalKarbohidratEdit).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
|
|
});
|
|
}
|
|
});
|
|
});
|