diff --git a/src/components/pages/production/project-flock/ProjectFlockTable.tsx b/src/components/pages/production/project-flock/ProjectFlockTable.tsx index ab14ef84..cad76310 100644 --- a/src/components/pages/production/project-flock/ProjectFlockTable.tsx +++ b/src/components/pages/production/project-flock/ProjectFlockTable.tsx @@ -13,6 +13,7 @@ import { useModal } from '@/components/Modal'; import ConfirmationModal from '@/components/modal/ConfirmationModal'; import ConfirmationModalWithNotes from '@/components/modal/ConfirmationModalWithNotes'; import Table from '@/components/Table'; +import Dropdown from '@/components/Dropdown'; import { ROWS_OPTIONS } from '@/config/constant'; import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; import { cn, formatDate, formatTitleCase } from '@/lib/helper'; @@ -29,6 +30,111 @@ import toast from 'react-hot-toast'; import useSWR from 'swr'; import RequirePermission from '@/components/helper/RequirePermission'; +import StatusBadge from '@/components/helper/StatusBadge'; +import PopoverButton from '@/components/popover/PopoverButton'; +import PopoverContent from '@/components/popover/PopoverContent'; + +const RowOptionsMenu = ({ + props, + popoverPosition = 'bottom', + editClickHandler, + detailClickHandler, + deleteClickHandler, +}: { + props: CellContext; + popoverPosition: 'bottom' | 'top'; + editClickHandler: (id: number) => void; + detailClickHandler: (id: number) => void; + deleteClickHandler: () => void; +}) => { + // TODO: change this to real condition + const showEditButton = true; + + const showDeleteButton = showEditButton; + + const popoverId = `projectFlock#${props.row.original.id}`; + const popoverAnchorName = `--anchor-projectFlock#${props.row.original.id}`; + + const closePopover = () => { + document.getElementById(popoverId)?.hidePopover(); + }; + + const detailClickHandlerWrapper = () => { + detailClickHandler(props.row.original.id); + closePopover(); + }; + + const editClickHandlerWrapper = () => { + editClickHandler(props.row.original.id); + closePopover(); + }; + + return ( +
+ + + + + +
+ + + + + {showEditButton && ( + + + + )} + + {showDeleteButton && ( + +
+ +
+ )} +
+
+
+ ); +}; const ProjectFlockTable = ({ refresh }: { refresh?: () => void }) => { const { @@ -62,6 +168,7 @@ const ProjectFlockTable = ({ refresh }: { refresh?: () => void }) => { const selectedRowIds = Object.keys(rowSelection) .filter((id) => rowSelection[id]) .map((id) => parseInt(id)); + const [selectedArea, setSelectedArea] = useState(null); const [selectedLocation, setSelectedLocation] = useState( null @@ -78,6 +185,8 @@ const ProjectFlockTable = ({ refresh }: { refresh?: () => void }) => { ); const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [isApproveLoading, setIsApproveLoading] = useState(false); + const [isLoadingExportingToExcel, setIsLoadingExportingToExcel] = + useState(false); // ===== Fetch Data ===== const { @@ -175,14 +284,27 @@ const ProjectFlockTable = ({ refresh }: { refresh?: () => void }) => { : null; }, [rowSelection]); + // const canApprove = useMemo(() => { + // if (!selectedSingleRow || isApproveLoading) return false; + + // const isPengajuan = selectedSingleRow.approval?.step_number == 1; + // const isNotRejected = selectedSingleRow.approval?.action != 'REJECTED'; + + // return isPengajuan && isNotRejected; + // }, [selectedSingleRow, isApproveLoading]); + const canApprove = useMemo(() => { - if (!selectedSingleRow || isApproveLoading) return false; + return selectedRowIds.every((id) => { + const projectFlock = isResponseSuccess(projectFlocks) + ? projectFlocks?.data.find((row) => row.id === id) + : null; - const isPengajuan = selectedSingleRow.approval?.step_number == 1; - const isNotRejected = selectedSingleRow.approval?.action != 'REJECTED'; - - return isPengajuan && isNotRejected; - }, [selectedSingleRow, isApproveLoading]); + const isProjectFlockRequesting = projectFlock?.approval?.step_number == 1; + const isProjectFlockNotRejected = + projectFlock?.approval?.action != 'REJECTED'; + return isProjectFlockRequesting && isProjectFlockNotRejected; + }); + }, [selectedRowIds, projectFlocks]); // ====== COLUMNS ====== const columns = useMemo[]>( @@ -256,44 +378,39 @@ const ProjectFlockTable = ({ refresh }: { refresh?: () => void }) => { const approval = props.row.original.approval; const isRejected = approval?.action == 'REJECTED'; const isApproved = approval?.action == 'APPROVED'; - return ( - - - {isRejected - ? 'Ditolak' - : formatTitleCase(approval?.step_name || '')} - + : 'neutral'; + + switch (approval.action.toLowerCase()) { + case 'pengajuan': + latestApprovalStepName = 'Pengajuan'; + break; + case 'aktif': + latestApprovalStepName = 'Aktif'; + break; + case 'Selesai': + latestApprovalStepName = 'Closing'; + break; + } + + if (isRejected) { + latestApprovalStepName = 'Ditolak'; + } + + return ( + ); }, }, @@ -325,27 +442,88 @@ const ProjectFlockTable = ({ refresh }: { refresh?: () => void }) => { cell: (props) => formatDate(props.row.original.created_at, 'MMM DD, YYYY'), }, + { + id: 'actions', + cell: (props) => { + const currentPageSize = + props.table.getPaginationRowModel().rows.length; + const currentPageRows = props.table.getPaginationRowModel().flatRows; + const currentRowRelativeIndex = + currentPageRows.findIndex((r) => r.id === props.row.id) + 1; + + const isLast2Rows = currentRowRelativeIndex > currentPageSize - 2; + + const detailClickHandler = (id: number) => { + router.push( + `/production/project-flock/detail/?projectFlockId=${id}` + ); + }; + + const editClickHandler = (id: number) => { + router.push( + `/production/project-flock/detail/edit/?projectFlockId=${id}` + ); + }; + + const deleteClickHandler = () => { + // Set row selection + setRowSelection({ + [String(props.row.original.id)]: true, + }); + + deleteModal.openModal(); + }; + + return ( + + ); + }, + }, ], [] ); + const exportToExcelHandler = async () => { + setIsLoadingExportingToExcel(true); + + toast.error('Not implemented yet!'); + + setIsLoadingExportingToExcel(false); + }; + + const bulkApproveClickHandler = () => { + setApprovalAction('APPROVED'); + confirmModal.openModal(); + }; + + const bulkRejectClickHandler = () => { + setApprovalAction('REJECTED'); + confirmModal.openModal(); + }; + return ( <> -
-
-
+
+
+ {/*
@@ -423,6 +601,158 @@ const ProjectFlockTable = ({ refresh }: { refresh?: () => void }) => { }} />
+
*/} + +
+
+ + + + + {selectedRowIds.length > 0 && canApprove && ( + <> +
+ + + + + + + + + + )} +
+ +
+ + } + className={{ + wrapper: 'w-full min-w-24 max-w-3xs', + inputWrapper: 'rounded-xl! shadow-button-soft', + input: + 'placeholder:font-semibold placeholder:text-base-content/50', + }} + /> + + + + +
+ + + Export + +
+ + +
+ + } + > + + +
@@ -448,26 +778,20 @@ const ProjectFlockTable = ({ refresh }: { refresh?: () => void }) => { setSorting={setSorting} rowSelection={rowSelection} setRowSelection={setRowSelection} + withCheckbox className={{ - containerClassName: cn({ - 'mb-40': + containerClassName: cn('p-3', { + 'w-full mb-20': isResponseSuccess(projectFlocks) && - projectFlocks?.data?.length > 0, + projectFlocks?.data?.length === 0, }), - tableWrapperClassName: 'overflow-x-auto min-h-full!', - tableClassName: 'font-inter w-full table-auto min-h-full!', - headerRowClassName: 'border-b border-b-gray-200', - headerColumnClassName: - 'px-6 py-3 text-xs font-semibold text-gray-500 last:flex last:flex-row last:justify-end', - bodyRowClassName: 'border-b border-b-gray-200', - bodyColumnClassName: - 'px-6 py-3 last:flex last:flex-row last:justify-end', + headerColumnClassName: 'text-nowrap', }} />
- void }) => { onClose={() => { setRowSelection({}); }} - /> + /> */}