From 7a4e7cbefe714c35ca068534105e72f6b8df69d2 Mon Sep 17 00:00:00 2001 From: Sabda Yagra Date: Wed, 20 Aug 2025 22:43:10 +0700 Subject: [PATCH] fixing --- .../calendar-polri/component/columns.tsx | 182 +++++++++++++++--- .../form/schedule/form-calendar-polri.tsx | 133 ++++++++++++- components/landing-page/hero-new.tsx | 2 +- 3 files changed, 280 insertions(+), 37 deletions(-) diff --git a/app/[locale]/(protected)/contributor/schedule/calendar-polri/component/columns.tsx b/app/[locale]/(protected)/contributor/schedule/calendar-polri/component/columns.tsx index 85ac17b6..255f01cc 100644 --- a/app/[locale]/(protected)/contributor/schedule/calendar-polri/component/columns.tsx +++ b/app/[locale]/(protected)/contributor/schedule/calendar-polri/component/columns.tsx @@ -2,7 +2,7 @@ import * as React from "react"; import { ColumnDef } from "@tanstack/react-table"; import { Eye, MoreVertical, SquarePen, Trash2 } from "lucide-react"; -import { cn } from "@/lib/utils"; +import { cn, getCookiesDecrypt } from "@/lib/utils"; import { DropdownMenu, DropdownMenuContent, @@ -17,9 +17,10 @@ import withReactContent from "sweetalert2-react-content"; import Swal from "sweetalert2"; import { error } from "@/lib/swal"; import { deleteCalendar } from "@/service/schedule/schedule"; +import { loading, success } from "@/config/swal"; const useTableColumns = () => { - const t = useTranslations("Table"); // Panggil di dalam hook + const t = useTranslations("Table"); const columns: ColumnDef[] = [ { @@ -100,15 +101,13 @@ const useTableColumns = () => { enableHiding: false, cell: ({ row }) => { const MySwal = withReactContent(Swal); + const levelNumber = Number(getCookiesDecrypt("ulne")); // 1 = Mabes, 2 = Polda + const calendarOwnerLevel = Number(row.original.assignedToLevel); // dari API + const calendarOwner = row.original.assignedTo; // ID unit Polda/Mabes dari API + const myUnit = getCookiesDecrypt("unitId"); // misal ID unit Polda login async function doDelete(id: any) { - // loading(); - const data = { - id, - }; - const response = await deleteCalendar(id); - if (response?.error) { error(response.message); return false; @@ -144,6 +143,29 @@ const useTableColumns = () => { } }); }; + + // === RULE AKSI === + let canEdit = false; + let canDelete = false; + const canView = true; + + if (levelNumber === 1) { + // Mabes -> bebas + canEdit = true; + canDelete = true; + } else if (levelNumber === 2) { + // Polda + if (calendarOwnerLevel === 1) { + // kalender Mabes -> hanya view + canEdit = false; + canDelete = false; + } else if (calendarOwnerLevel === 2 && calendarOwner === myUnit) { + // kalender polda sendiri -> bisa edit/delete + canEdit = true; + canDelete = true; + } + } + return ( @@ -156,34 +178,134 @@ const useTableColumns = () => { - - - - Detail + {canView && ( + + + + Detail + + + )} + + {canEdit && ( + + + + Edit + + + )} + + {canDelete && ( + handleDeleteCalendars(row.original.id)} + className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none" + > + + Delete - - - - - Edit - - - handleDeleteCalendars(row.original.id)} - className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none" - > - - Delete - + )} ); }, }, + + // { + // id: "actions", + // accessorKey: "action", + // header: t("action", { defaultValue: "Action" }), + // enableHiding: false, + // cell: ({ row }) => { + // const MySwal = withReactContent(Swal); + + // async function doDelete(id: any) { + // // loading(); + // const data = { + // id, + // }; + + // const response = await deleteCalendar(id); + + // if (response?.error) { + // error(response.message); + // return false; + // } + // success(); + // } + + // function success() { + // MySwal.fire({ + // title: "Sukses", + // icon: "success", + // confirmButtonColor: "#3085d6", + // confirmButtonText: "OK", + // }).then((result) => { + // if (result.isConfirmed) { + // window.location.reload(); + // } + // }); + // } + + // const handleDeleteCalendars = (id: any) => { + // MySwal.fire({ + // title: "Hapus Data", + // text: "", + // icon: "warning", + // showCancelButton: true, + // cancelButtonColor: "#3085d6", + // confirmButtonColor: "#d33", + // confirmButtonText: "Hapus", + // }).then((result) => { + // if (result.isConfirmed) { + // doDelete(id); + // } + // }); + // }; + // return ( + // + // + // + // + // + // + // + // + // Detail + // + // + // + // + // + // Edit + // + // + // handleDeleteCalendars(row.original.id)} + // className="p-2 border-b text-destructive bg-destructive/30 focus:bg-destructive focus:text-destructive-foreground rounded-none" + // > + // + // Delete + // + // + // + // ); + // }, + // }, ]; return columns; diff --git a/components/form/schedule/form-calendar-polri.tsx b/components/form/schedule/form-calendar-polri.tsx index b75b79e0..c0d2dd0d 100644 --- a/components/form/schedule/form-calendar-polri.tsx +++ b/components/form/schedule/form-calendar-polri.tsx @@ -41,7 +41,7 @@ import { } from "@/components/ui/popover"; import { Calendar } from "@/components/ui/calendar"; import { format } from "date-fns"; -import { cn } from "@/lib/utils"; +import { cn, getCookiesDecrypt } from "@/lib/utils"; import { getUserLevelForAssignments } from "@/service/task"; import { Card } from "@/components/ui/card"; import { useDropzone } from "react-dropzone"; @@ -84,6 +84,8 @@ export function CalendarPolriAdd() { >([]); const [imageFiles, setImageFiles] = React.useState([]); const [date, setDate] = React.useState(); + const levelNumber = Number(getCookiesDecrypt("ulne")) || 0; + const roleId = Number(getCookiesDecrypt("urie")) || 0; const [unitSelection, setUnitSelection] = React.useState({ semua: false, @@ -143,7 +145,7 @@ export function CalendarPolriAdd() { }; const handlePoldaPolresChange = () => { - return Array.from(checkedLevels).join(","); // Mengonversi Set ke string + return Array.from(checkedLevels).join(","); }; const handleUnitChange = ( @@ -233,7 +235,6 @@ export function CalendarPolriAdd() { const resCsrf = await getCsrfToken(); const csrfToken = resCsrf?.data?.token; - console.log("CSRF TOKEN : ", csrfToken); const headers = { "X-XSRF-TOKEN": csrfToken, }; @@ -387,7 +388,7 @@ export function CalendarPolriAdd() {

Publish Area

-
+ {/*
{Object.keys(unitSelection).map((key) => (
))} +
*/} +
+ {Object.keys(unitSelection) + .filter((key) => { + // Jika login sebagai polda (2), hanya tampilkan polda + if (levelNumber === 2) return key === "polda"; + // Jika login sebagai satker (4), hanya tampilkan satker + if (levelNumber === 4) return key === "satker"; + // Selain itu tampilkan semua + return true; + }) + .map((key) => ( +
+ + handleUnitChange( + key as keyof typeof unitSelection, + value as boolean + ) + } + /> + +
+ ))}
-
+ + {levelNumber !== 2 && levelNumber !== 4 && ( +
+ + + + + + + + Daftar Wilayah Polda dan Polres + + +
+ {listDest.map((polda: any) => ( +
+ + {expandedPolda[polda.id] && ( +
+ + {polda?.subDestination?.map((polres: any) => ( + + ))} +
+ )} +
+ ))} +
+
+
+
+ )} + + {/*
-
+
*/}
diff --git a/components/landing-page/hero-new.tsx b/components/landing-page/hero-new.tsx index 0ab87c99..9488908d 100644 --- a/components/landing-page/hero-new.tsx +++ b/components/landing-page/hero-new.tsx @@ -536,7 +536,7 @@ const HeroNew = (props: { group?: string }) => {