189 lines
6.3 KiB
JavaScript
189 lines
6.3 KiB
JavaScript
formAddMKlasifikasi.on('submit', function(e){
|
|
e.preventDefault();
|
|
|
|
const form = this;
|
|
const formData = new FormData(form);
|
|
|
|
fetch(`/master-klasifikasi`, {
|
|
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_klasifikasi").html('');
|
|
colCount = 0; // reset counter
|
|
formAddMKlasifikasi[0].reset();
|
|
datatableMKlasifikasi.bootstrapTable('refresh');
|
|
modalKlasifikasi.removeEventListener('hidden.bs.modal', handler);
|
|
};
|
|
modalKlasifikasi.addEventListener('hidden.bs.modal', handler);
|
|
bootstrap.Modal.getInstance(modalKlasifikasi).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 deleteMKlasifikasi(e){
|
|
let id =$(e).data('master_klasifikasi_directory_id')
|
|
Swal.fire({
|
|
title: "Apakah kamu yakin ingin menghapus data ini?",
|
|
text : $(e).data('nama_klasifikasi_directory'),
|
|
icon: "warning",
|
|
showCancelButton: true,
|
|
backdrop: true,
|
|
}).then((result) => {
|
|
if(result.isConfirmed){
|
|
fetch(`/master-klasifikasi/${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(() => {
|
|
datatableMKlasifikasi.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 editKlasifikasi(e){
|
|
const data = $(e).data();
|
|
new bootstrap.Modal(modalKlasifikasiEdit).show();
|
|
formEditMKlasifikasi.attr('action', `/master-klasifikasi/${data.master_klasifikasi_directory_id}`)
|
|
$("#nama_klasifikasi_edit").val(data.nama_klasifikasi_directory)
|
|
}
|
|
|
|
formEditMKlasifikasi.on('submit', function(e){
|
|
e.preventDefault();
|
|
|
|
const form = this;
|
|
const actionUrl = formEditMKlasifikasi.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_kategori_directory[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_klasifikasi_edit").val('');
|
|
colCount = 0; // reset counter
|
|
formEditMKlasifikasi[0].reset();
|
|
datatableMKlasifikasi.bootstrapTable('refresh');
|
|
modalKlasifikasiEdit.removeEventListener('hidden.bs.modal', handler);
|
|
};
|
|
modalKlasifikasiEdit.addEventListener('hidden.bs.modal', handler);
|
|
bootstrap.Modal.getInstance(modalKlasifikasiEdit).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
|
|
});
|
|
}
|
|
});
|
|
});
|