'use client' import { ViolationFileDetail } from '@/app/hooks/react-query/useFileViolations'; import { useEffect, useActionState, SetStateAction } from 'react'; import { fetchCaseInfo, createCase, MatchStatus } from '@/app/actions/violationActions'; import { useQuery } from '@tanstack/react-query'; import { useTranslations } from 'next-intl'; import { toast } from 'sonner'; import { formatDate, formatDateTime } from '@/app/lib/formatDate'; interface CaseInfo { id: number; name: string; description: string; amount: number; priority: string; type: string; lawyer: string | null; created_at: string; updated_at: string; page_number: number; page_size: number; total_elements: number; total_pages: number; violation_id: number; content: string | null; } const ENABLED_STATUSES = ['LEGAL_IN_WORK', 'COMPLAINT_AND_LEGAL_IN_WORK']; type UpdateStatusHandler = (id: number, status: MatchStatus) => Promise; export default function CaseViolation({ selectedViolation, updateStatusHandler, setLocalViolation }: { selectedViolation: ViolationFileDetail, updateStatusHandler: UpdateStatusHandler, setLocalViolation: React.Dispatch> } ) { const t = useTranslations('Global'); const [state, formAction, isPending] = useActionState(createCase, undefined); const { data: caseInfo, isLoading: isLoadingCase } = useQuery({ queryKey: ['caseInfo', selectedViolation.id], queryFn: () => fetchCaseInfo(selectedViolation.id), select: (data) => { if (data) { return data } }, enabled: ENABLED_STATUSES.includes(selectedViolation.status) }); useEffect(() => { if (state?.success) { toast.success(t('the-complaint-has-been-registered')); const newStatus = selectedViolation.status === 'COMPLAINT_IN_WORK' ? 'COMPLAINT_AND_LEGAL_IN_WORK' : 'LEGAL_IN_WORK'; setLocalViolation(prev => ({ ...prev, status: newStatus })) updateStatusHandler(selectedViolation.id, newStatus); } else if (state && !state.success) { console.log(state.errorMessage); if (state.errorMessage?.server) { toast.warning(t(state.errorMessage.server)); } else { toast.warning(t('the-complaint-has-not-been-registered')); } } }, [state, updateStatusHandler, selectedViolation.id]); if (!ENABLED_STATUSES.includes(selectedViolation.status)) { return (
{state?.errorMessage?.caseTitle && (

{ state?.errorMessage?.caseTitle.split('&').map((e, index) => { return ( {t(e)}
) }) }

)}
{state?.errorMessage?.amount && (

{ state?.errorMessage?.amount.split('&').map((e, index) => { return ( {t(e)}
) }) }

)}
{state?.errorMessage?.textArea && (

{ state?.errorMessage?.textArea.split('&').map((e, index) => { return ( {t(e)}
) }) }

)}
); } else { if (isLoadingCase) { return (
); } return (
{caseInfo?.body?.content ? ( <> {caseInfo.body.content.map((item: CaseInfo) => { return (
{item.name}
{t('Status')}:
{item.type}
{t('date-of-creation')}:
{item.created_at ? `${formatDate(item.created_at)}: ${formatDateTime(item.created_at)}` : '---'}
{t('date-of-update')}:
{item.updated_at ? `${formatDate(item.updated_at)}: ${formatDateTime(item.updated_at)}` : '---'}
{t('amount-of-damage')}:
{item.amount}
{t('lawyer')}:
{item.lawyer}
{t('description')}:
{item.description}
); })} ) : (
{t('no-information-about-the-complaint')}
)}
); } }