diff --git a/src/components/pages/production/recording/RecordingTable.tsx b/src/components/pages/production/recording/RecordingTable.tsx index cd98b597..36a4e69a 100644 --- a/src/components/pages/production/recording/RecordingTable.tsx +++ b/src/components/pages/production/recording/RecordingTable.tsx @@ -7,14 +7,12 @@ import React, { useEffect, useRef, } from 'react'; -import { RefObject } from 'react'; import useSWR from 'swr'; import { Icon } from '@iconify/react'; import { SortingState, CellContext } from '@tanstack/react-table'; import { cn, formatDate, formatNumber } from '@/lib/helper'; import RequirePermission from '@/components/helper/RequirePermission'; import { useModal } from '@/components/Modal'; -import Modal from '@/components/Modal'; import Button from '@/components/Button'; import ConfirmationModal from '@/components/modal/ConfirmationModal'; import ConfirmationModalWithNotes from '@/components/modal/ConfirmationModalWithNotes'; @@ -28,14 +26,51 @@ import RowCollapseOptions from '@/components/table/RowCollapseOptions'; import RowOptionsMenuWrapper from '@/components/table/RowOptionsMenuWrapper'; import { type Recording } from '@/types/api/production/recording'; import { RecordingApi } from '@/services/api/production'; -import { ApprovalApi } from '@/services/api/approval'; import { isResponseSuccess } from '@/lib/api-helper'; import { useTableFilter } from '@/services/hooks/useTableFilter'; import toast from 'react-hot-toast'; import Badge from '@/components/Badge'; +import StatusBadge from '@/components/helper/StatusBadge'; import CheckboxInput from '@/components/input/CheckboxInput'; import { useUiStore } from '@/stores/ui/ui.store'; -import { BaseApproval, BaseApiResponse } from '@/types/api/api-general'; +import { Color } from '@/types/theme'; + +// ===== STATUS BADGE UTILITIES ===== +const statusTextMap: Record = { + APPROVED: 'Disetujui', + Disetujui: 'Disetujui', + REJECTED: 'Ditolak', + Ditolak: 'Ditolak', + CREATED: 'Dibuat', + UPDATED: 'Diperbarui', +}; + +const getStatusText = (status: string): string => { + return statusTextMap[status] || status; +}; + +const statusBadgeColorMap: Record = { + APPROVED: 'success', + Disetujui: 'success', + approved: 'success', + disetujui: 'success', + REJECTED: 'error', + Ditolak: 'error', + rejected: 'error', + ditolak: 'error', + CREATED: 'neutral', + Dibuat: 'neutral', + created: 'neutral', + dibuat: 'neutral', + UPDATED: 'warning', + Diperbarui: 'warning', + updated: 'warning', + diperbarui: 'warning', +}; + +const getStatusBadgeColor = (status: string): Color => { + return statusBadgeColorMap[status] || 'neutral'; +}; const RowOptionsMenu = ({ type = 'dropdown', @@ -135,221 +170,6 @@ const RowOptionsMenu = ({ ); }; -const ApprovalHistoryModal = ({ - ref, - currentApproval, - module_name = 'RECORDINGS', - module_id, -}: { - ref: RefObject; - currentApproval?: BaseApproval; - module_name?: string; - module_id?: number | undefined; -}) => { - const [isModalOpen, setIsModalOpen] = useState(false); - - const approvalHistoryUrl = useMemo(() => { - if (!isModalOpen) return null; - const params = new URLSearchParams({ - module_name: module_name, - group_step_number: 'true', - }); - - if (module_id) { - params.append('module_id', module_id.toString()); - } - - return `${ApprovalApi.basePath}?${params.toString()}`; - }, [module_name, module_id, isModalOpen]); - - type GroupedApprovalResponse = { - step_number: number; - step_name: string; - approvals: BaseApproval[]; - }; - - const fetchGroupedApprovals = async ( - url: string - ): Promise> => { - return (await ApprovalApi.getAllFetcher(url)) as BaseApiResponse< - GroupedApprovalResponse[] - >; - }; - - const { data: approvalHistoryData, isLoading } = useSWR< - BaseApiResponse - >(approvalHistoryUrl, fetchGroupedApprovals); - - useEffect(() => { - const checkModalOpen = () => { - const isOpen = ref.current?.open || false; - setIsModalOpen(isOpen); - }; - - checkModalOpen(); - - const observer = new MutationObserver(checkModalOpen); - if (ref.current) { - observer.observe(ref.current, { - attributes: true, - attributeFilter: ['open'], - }); - } - - return () => observer.disconnect(); - }, [ref]); - - const approvalHistory = useMemo(() => { - if (!approvalHistoryData || approvalHistoryData.status !== 'success') - return []; - - const groupedData = approvalHistoryData.data || []; - const flattenedApprovals: BaseApproval[] = []; - - groupedData.forEach((group) => { - group.approvals.forEach((approval) => { - flattenedApprovals.push(approval); - }); - }); - - return flattenedApprovals; - }, [approvalHistoryData]); - - const closeModalHandler = () => { - ref.current?.close(); - }; - - return ( - -
- {/* Header */} -
-

Riwayat Approval

- -
- - {isLoading ? ( -
- -
- ) : ( - <> - {/* Current Status */} - {currentApproval && ( -
-

Status Saat Ini

-
- - {currentApproval.step_name} - - - {currentApproval.action === 'APPROVED' && 'Disetujui'} - {currentApproval.action === 'REJECTED' && 'Ditolak'} - {currentApproval.action === 'CREATED' && 'Dibuat'} - {currentApproval.action === 'UPDATED' && 'Diperbarui'} - -
- {currentApproval.notes && ( -

- Catatan:{' '} - {currentApproval.notes} -

- )} -

- Oleh: {currentApproval.action_by.name} •{' '} - {formatDate(currentApproval.action_at, 'DD MMMM YYYY HH:mm')} -

-
- )} - - {/* Full History */} - {approvalHistory.length > 0 && ( -
-

Riwayat Lengkap

-
- - - - - - - - - - - - {approvalHistory - .sort( - (a: BaseApproval, b: BaseApproval) => - new Date(b.action_at).getTime() - - new Date(a.action_at).getTime() - ) - .map((approval: BaseApproval, index: number) => ( - - - - - - - - ))} - -
TahapAksiCatatanOlehWaktu
{approval.step_name} - - {approval.action === 'APPROVED' && 'Disetujui'} - {approval.action === 'REJECTED' && 'Ditolak'} - {approval.action === 'CREATED' && 'Dibuat'} - {approval.action === 'UPDATED' && 'Diperbarui'} - - -
- {approval.notes || '-'} -
-
{approval.action_by.name} - {formatDate( - approval.action_at, - 'DD MMMM YYYY HH:mm' - )} -
-
-
- )} - - )} -
-
- ); -}; - const RecordingTable = () => { const { searchValue, setSearchValue, resetSearchValue } = useUiStore(); const previousPathRef = useRef(null); @@ -395,7 +215,6 @@ const RecordingTable = () => { const singleDeleteModal = useModal(); const approveModal = useModal(); const rejectModal = useModal(); - const approvalHistoryModal = useModal(); const { data: recordings, @@ -1032,32 +851,19 @@ const RecordingTable = () => { const approval = props.row.original.approval; if (!approval) return '-'; - const statusColor = - approval.action === 'APPROVED' - ? 'success' - : approval.action === 'REJECTED' - ? 'error' - : approval.action === 'UPDATED' - ? 'warning' - : 'info'; + const status = approval.action; + const statusColor = getStatusBadgeColor(status); - const openApprovalHistory = () => { - setSelectedRecording(props.row.original); - approvalHistoryModal.openModal(); - }; + const statusText = approval.step_name || getStatusText(status); return ( - - {approval.step_name || approval.action} - + /> ); }, }, @@ -1208,7 +1014,10 @@ const RecordingTable = () => { text={`Apakah anda yakin ingin approve data recording ini (${eligibleRowIds.length} data dari ${selectedRowIds.length} yang dipilih)?`} secondaryButton={{ text: 'Tidak', - onClick: () => setApprovalNotes(''), + onClick: () => { + setApprovalNotes(''); + approveModal.closeModal(); + }, }} primaryButton={{ text: 'Ya', @@ -1226,7 +1035,10 @@ const RecordingTable = () => { text={`Apakah anda yakin ingin reject data recording ini (${eligibleRowIds.length} data dari ${selectedRowIds.length} yang dipilih)?`} secondaryButton={{ text: 'Tidak', - onClick: () => setApprovalNotes(''), + onClick: () => { + setApprovalNotes(''); + rejectModal.closeModal(); + }, }} primaryButton={{ text: 'Ya', @@ -1237,13 +1049,6 @@ const RecordingTable = () => { placeholder='(Opsional) Tambahkan catatan untuk reject ini...' rows={3} /> - - ); }; diff --git a/src/components/pages/purchase/PurchaseTable.tsx b/src/components/pages/purchase/PurchaseTable.tsx index 9af19b42..d0c72dac 100644 --- a/src/components/pages/purchase/PurchaseTable.tsx +++ b/src/components/pages/purchase/PurchaseTable.tsx @@ -16,7 +16,7 @@ import RowDropdownOptions from '@/components/table/RowDropdownOptions'; import RowCollapseOptions from '@/components/table/RowCollapseOptions'; import RowOptionsMenuWrapper from '@/components/table/RowOptionsMenuWrapper'; import RequirePermission from '@/components/helper/RequirePermission'; -import Badge from '@/components/Badge'; +import StatusBadge from '@/components/helper/StatusBadge'; import { cn, formatDate } from '@/lib/helper'; import { isResponseSuccess } from '@/lib/api-helper'; @@ -25,6 +25,44 @@ import { useTableFilter } from '@/services/hooks/useTableFilter'; import { ROWS_OPTIONS } from '@/config/constant'; import { Purchase } from '@/types/api/purchase/purchase'; import { PurchaseApi } from '@/services/api/purchase'; +import { Color } from '@/types/theme'; + +// ===== STATUS BADGE UTILITIES ===== +const statusTextMap: Record = { + APPROVED: 'Disetujui', + Disetujui: 'Disetujui', + REJECTED: 'Ditolak', + Ditolak: 'Ditolak', + CREATED: 'Dibuat', + UPDATED: 'Diperbarui', +}; + +const getStatusText = (status: string): string => { + return statusTextMap[status] || status; +}; + +const statusBadgeColorMap: Record = { + APPROVED: 'success', + Disetujui: 'success', + approved: 'success', + disetujui: 'success', + REJECTED: 'error', + Ditolak: 'error', + rejected: 'error', + ditolak: 'error', + CREATED: 'neutral', + Dibuat: 'neutral', + created: 'neutral', + dibuat: 'neutral', + UPDATED: 'warning', + Diperbarui: 'warning', + updated: 'warning', + diperbarui: 'warning', +}; + +const getStatusBadgeColor = (status: string): Color => { + return statusBadgeColorMap[status] || 'neutral'; +}; // ===== INTERFACES ===== interface RowOptionsMenuProps { @@ -160,48 +198,42 @@ const PurchaseTable = () => { const approval = props.row.original.latest_approval; if (!approval) return '-'; - const isRejected = approval.action === 'REJECTED'; + const status = approval.action; - let statusColor: - | 'warning' - | 'success' - | 'neutral' - | 'error' - | 'primary' - | 'info' = 'neutral'; + let statusColor: Color = 'neutral'; - switch (approval.step_number) { - case 1: - statusColor = 'neutral'; - break; - case 2: - statusColor = 'primary'; - break; - case 3: - statusColor = 'info'; - break; - case 4: - statusColor = 'warning'; - break; - case 5: - statusColor = 'success'; - break; + if (status === 'REJECTED') { + statusColor = getStatusBadgeColor(status); + } else { + switch (approval.step_number) { + case 1: + statusColor = 'neutral'; + break; + case 2: + statusColor = 'primary'; + break; + case 3: + statusColor = 'info'; + break; + case 4: + statusColor = 'warning'; + break; + case 5: + statusColor = 'success'; + break; + } } - if (isRejected) { - statusColor = 'error'; - } + const statusText = approval.step_name || getStatusText(status); return ( - - {isRejected ? 'Ditolak' : approval.step_name} - + /> ); }, }, @@ -369,6 +401,7 @@ const PurchaseTable = () => { text={`Apakah anda yakin ingin menghapus data permintaan pembelian ini?`} secondaryButton={{ text: 'Tidak', + onClick: () => deleteModal.closeModal(), }} primaryButton={{ text: 'Ya',