senarai w backend

This commit is contained in:
Md Afiq Iskandar 2024-09-04 13:36:24 +08:00
parent fd4c511256
commit 4d31939b0f
3 changed files with 126 additions and 70 deletions

View File

@ -1,12 +1,15 @@
<template> <template>
<div> <div>
<!-- Header with title and "Permohonan Baru" button -->
<div class="flex justify-between items-center"> <div class="flex justify-between items-center">
<h1>Senarai Permohonan</h1> <h1>Senarai Permohonan</h1>
<!-- Button to navigate to the "Permohonan Baru" page -->
<rs-button @click="permohonanBaru()" variant="primary" class="mt-2"> <rs-button @click="permohonanBaru()" variant="primary" class="mt-2">
Permohonan Baru Permohonan Baru
</rs-button> </rs-button>
</div> </div>
<!-- Table displaying the list of permohonan -->
<rs-card class="mt-4 py-2"> <rs-card class="mt-4 py-2">
<rs-table <rs-table
:data="tableData" :data="tableData"
@ -22,6 +25,7 @@
}" }"
advanced advanced
> >
<!-- Table Headers -->
<template v-slot:header> <template v-slot:header>
<tr> <tr>
<th>No</th> <th>No</th>
@ -31,6 +35,8 @@
<th>Butiran</th> <th>Butiran</th>
</tr> </tr>
</template> </template>
<!-- Table Data Rows -->
<template v-slot:no="data"> <template v-slot:no="data">
{{ data.text }} {{ data.text }}
</template> </template>
@ -51,8 +57,11 @@
{{ data.text || "N/A" }} {{ data.text || "N/A" }}
</rs-badge> </rs-badge>
</template> </template>
<!-- Actions for each permohonan -->
<template v-slot:butiran="data"> <template v-slot:butiran="data">
<div class="flex flex-wrap gap-2" v-if="data.value.status !== 'Sah'"> <div class="flex flex-wrap gap-2" v-if="data.value.status !== 'Sah'">
<!-- Button to navigate to the "Kemaskini" page for the selected permohonan -->
<rs-button <rs-button
@click="kemaskini(data.value.noSiri)" @click="kemaskini(data.value.noSiri)"
variant="primary" variant="primary"
@ -62,6 +71,8 @@
> >
<Icon name="ic:baseline-edit" size="1.2rem" /> <Icon name="ic:baseline-edit" size="1.2rem" />
</rs-button> </rs-button>
<!-- Button to delete the selected permohonan -->
<rs-button <rs-button
@click="hapus(data.value.noSiri)" @click="hapus(data.value.noSiri)"
variant="danger" variant="danger"
@ -72,6 +83,8 @@
<Icon name="ic:baseline-delete" size="1.2rem" /> <Icon name="ic:baseline-delete" size="1.2rem" />
</rs-button> </rs-button>
</div> </div>
<!-- If permohonan has been confirmed -->
<div v-else> <div v-else>
<span>Permohonan telah disahkan</span> <span>Permohonan telah disahkan</span>
</div> </div>
@ -82,62 +95,39 @@
</template> </template>
<script setup> <script setup>
import { ref } from "vue";
const { $swal } = useNuxtApp(); const { $swal } = useNuxtApp();
definePageMeta({ // Reactive variable to store table data
title: "Senarai Permohonan", const tableData = ref([]);
});
const tableData = ref([ // Fetch permohonan list from API
{ const fetchPermohonan = async () => {
no: 1, try {
noSiri: "1234567890", const response = await $fetch("/api/permohonan");
tarikhMasa: "2024-01-01 12:00:00", if (response.statusCode === 200) {
status: "Sah", // Populate tableData with the fetched permohonan list
butiran: 1, tableData.value = response.data;
}, } else {
{ console.error(response.message);
no: 2, }
noSiri: "0987654321", } catch (error) {
tarikhMasa: "2024-02-01 14:30:00", console.error("Error fetching permohonan data:", error);
status: "Tidak Aktif", }
butiran: 2, };
},
{
no: 3,
noSiri: "1122334455",
tarikhMasa: "2024-03-01 09:15:00",
status: "Aktif",
butiran: 3,
},
{
no: 4,
noSiri: "5566778899",
tarikhMasa: "2024-04-01 16:45:00",
status: "Tidak Aktif",
butiran: 4,
},
{
no: 5,
noSiri: "6677889900",
tarikhMasa: "2024-05-01 11:00:00",
status: "Aktif",
butiran: 5,
},
]);
// Function to navigate to the "Permohonan Baru" page
const permohonanBaru = () => { const permohonanBaru = () => {
navigateTo("/permohonan-temujanji/baru"); navigateTo("/permohonan-temujanji/baru");
}; };
const kemaskini = (item) => { // Function to navigate to the "Kemaskini" page for a specific permohonan
navigateTo(`/permohonan-temujanji/kemaskini/${item}`); const kemaskini = (noSiri) => {
navigateTo(`/permohonan-temujanji/kemaskini/${noSiri}`);
}; };
const hapus = (item) => { // Function to delete a permohonan by its noSiri
$swal const hapus = async (noSiri) => {
.fire({ const confirmation = await $swal.fire({
title: "Anda pasti?", title: "Anda pasti?",
text: "Anda tidak akan dapat memulihkan semula data ini!", text: "Anda tidak akan dapat memulihkan semula data ini!",
icon: "warning", icon: "warning",
@ -146,22 +136,31 @@ const hapus = (item) => {
cancelButtonColor: "#d33", cancelButtonColor: "#d33",
confirmButtonText: "Ya, hapuskan!", confirmButtonText: "Ya, hapuskan!",
cancelButtonText: "Batal", cancelButtonText: "Batal",
})
.then((result) => {
if (result.isConfirmed) {
// Perform deletion logic here
console.log("Deleting item:", item);
// For now, let's just remove the item from the tableData
const index = tableData.value.findIndex((row) => row.noSiri === item);
if (index !== -1) {
tableData.value.splice(index, 1);
}
$swal.fire("Dihapuskan!", "Data telah dihapuskan.", "success");
}
}); });
};
</script>
<style lang="scss" scoped> if (confirmation.isConfirmed) {
/* Add any scoped styles here */ try {
</style> // Call API to delete the permohonan
const response = await $fetch(`/api/permohonan/${noSiri}`, {
method: "DELETE",
});
if (response.statusCode === 200) {
// Remove the deleted permohonan from tableData
tableData.value = tableData.value.filter(
(row) => row.noSiri !== noSiri
);
$swal.fire("Dihapuskan!", response.message, "success");
} else {
$swal.fire("Error!", response.message, "error");
}
} catch (error) {
$swal.fire("Error!", "Failed to delete permohonan.", "error");
}
}
};
// Fetch the permohonan list when the component is mounted
onMounted(() => {
fetchPermohonan();
});
</script>

View File

@ -0,0 +1,27 @@
// File: server/api/permohonan/[noSiri].delete.js
export default defineEventHandler(async (event) => {
const { noSiri } = event.context.params;
if (!noSiri) {
return {
statusCode: 400,
message: "noSiri is required.",
};
}
try {
await prisma.permohonan.delete({
where: { no_siri: noSiri },
});
return {
statusCode: 200,
message: "Permohonan successfully deleted.",
};
} catch (error) {
return {
statusCode: 500,
message: "Failed to delete permohonan.",
};
}
});

View File

@ -0,0 +1,30 @@
// File: server/api/permohonan/index.get.js
export default defineEventHandler(async () => {
try {
const permohonan = await prisma.permohonan.findMany({
select: {
id: true,
no_siri: true,
create_at: true,
status_permohonan: true,
},
});
return {
statusCode: 200,
message: "Success",
data: permohonan.map((item, index) => ({
no: index + 1,
noSiri: item.no_siri,
tarikhMasa: item.create_at.toISOString().replace("T", " ").slice(0, 19),
status: item.status_permohonan,
butiran: item.id,
})),
};
} catch (error) {
return {
statusCode: 500,
message: "Failed to fetch permohonan data.",
};
}
});