feature: umkm crud and add template file for reklame

This commit is contained in:
2025-02-17 13:03:54 +07:00
parent 41ddbaef24
commit 8c236c460d
27 changed files with 1434 additions and 42 deletions

View File

@@ -1,5 +1,6 @@
import { Grid } from "gridjs/dist/gridjs.umd.js";
import "gridjs/dist/gridjs.umd.js";
import Swal from "sweetalert2";
class GeneralTable {
constructor(tableId, apiUrl, baseUrl, columns, options = {}) {
@@ -94,9 +95,29 @@ class GeneralTable {
handleDelete(event) {
const id = event.target.getAttribute('data-id');
console.log(id);
if (confirm("Are you sure you want to delete this item?")) {
this.deleteRecord(id);
}
// if (confirm("Are you sure you want to delete this item?")) {
// this.deleteRecord(id);
// }
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#d33",
cancelButtonColor: "#3085d6",
confirmButtonText: "Yes, delete it!"
}).then((result) => {
if (result.isConfirmed) {
this.deleteRecord(id);
Swal.fire({
title: "Deleted!",
text: "Your record has been deleted.",
icon: "success",
showConfirmButton: false, // Menghilangkan tombol OK
timer: 2000 // Menutup otomatis dalam 2 detik (opsional)
});
}
});
}
async deleteRecord(id) {
@@ -110,23 +131,30 @@ class GeneralTable {
.getAttribute("content")}`,
"Content-Type": "application/json",
},
// headers: {
// 'Authorization': `Bearer ${document.querySelector('meta[name="api-token"]').getAttribute("content")}`,
// 'Content-Type': 'application/json',
// },
});
if (response.status === 204) { // Jika status code 204, berarti berhasil
alert('Data deleted successfully!');
if (response.status === 204) {
location.reload();
} else {
const data = await response.json(); // Jika ada data di response body
alert('Failed to delete data: ' + (data.message || 'Unknown error.'));
const data = await response.json();
showErrorAlert(`Failed to delete record: ${data.message || "Unknown error"}`);
}
} catch (error) {
console.error('Error deleting data:', error);
alert('Error deleting data.');
showErrorAlert("Error deleting data. Please try again.");
}
}
}
// Fungsi untuk menampilkan alert
function showErrorAlert(message) {
const alertContainer = document.getElementById("alert-container");
alertContainer.innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
${message}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`;
}
export default GeneralTable;