From 4a1464185b0cb52d39817bc4550b7715369401b2 Mon Sep 17 00:00:00 2001 From: rstubryan Date: Mon, 2 Feb 2026 10:30:34 +0700 Subject: [PATCH] refactor(FE): Replace approval history modal with status badge --- .../production/recording/RecordingTable.tsx | 310 ++++-------------- 1 file changed, 61 insertions(+), 249 deletions(-) diff --git a/src/components/pages/production/recording/RecordingTable.tsx b/src/components/pages/production/recording/RecordingTable.tsx index cd98b597..6beb0954 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,55 @@ 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'; + +// ===== 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< + string, + 'success' | 'error' | 'neutral' | 'info' | 'warning' +> = { + 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 +): 'success' | 'error' | 'neutral' | 'info' | 'warning' => { + return statusBadgeColorMap[status] || 'neutral'; +}; const RowOptionsMenu = ({ type = 'dropdown', @@ -135,221 +174,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 +219,6 @@ const RecordingTable = () => { const singleDeleteModal = useModal(); const approveModal = useModal(); const rejectModal = useModal(); - const approvalHistoryModal = useModal(); const { data: recordings, @@ -1032,32 +855,22 @@ 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 = + status === 'REJECTED' + ? 'Ditolak' + : approval.step_name || getStatusText(status); return ( - - {approval.step_name || approval.action} - + /> ); }, }, @@ -1208,7 +1021,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 +1042,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 +1056,6 @@ const RecordingTable = () => { placeholder='(Opsional) Tambahkan catatan untuk reject ini...' rows={3} /> - - ); };