'use client' import { ViolationFileDetail } from '@/app/hooks/react-query/useFileViolations'; import { useEffect, useState, useActionState } from 'react'; import { fetchCaseInfo, MatchStatus, createCase } from '@/app/actions/violationActions'; import { useQuery, useQueryClient } 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; createdAt: string; updatedAt: string; pageNumber: number; pageSize: number; totalElements: number; totalPages: number; violationId: 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 }: { selectedViolation: ViolationFileDetail, updateStatusHandler: UpdateStatusHandler }) { const t = useTranslations('Global'); const queryClient = useQueryClient(); const [showCreateCase, setShowCreateCase] = useState(false); const [localViolation, setLocalViolation] = useState(selectedViolation); const [state, formAction, isPending] = useActionState(createCase, undefined); const { data: caseInfo, isLoading: isLoadingCase } = useQuery({ queryKey: ['caseInfo', localViolation.id], queryFn: () => fetchCaseInfo(localViolation.id), select: (data) => { if (data) { return data } }, enabled: ENABLED_STATUSES.includes(localViolation.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); setShowCreateCase(false); } 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]); function toggleForm() { setShowCreateCase(!showCreateCase); } if (!ENABLED_STATUSES.includes(localViolation.status)) { return (
{!showCreateCase ? ( ) : (
{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 (

{t('complaint-Information')}

ID: {item.id}

{t('name')}: {item.name}

{t('description')}: {item.description}

{t('amount')}: {item.amount}

{t('priority')}: {item.priority}

{t('type')}: {item.type}

{t('lawyer')}: {item.lawyer || t('not-assigned')}

{t('violation-id')}: {item.violationId}

{t('date-of-creation')}: {item.createdAt ? `${formatDate(item.createdAt)}: ${formatDateTime(item.createdAt)}` : '---'}

{t('date-of-update')}: {item.createdAt ? `${formatDate(item.createdAt)}: ${formatDateTime(item.createdAt)}` : '---'}

{item.content &&

{t('content')}: {item.content}

}
); })}
) : (
{t('no-information-about-the-complaint')}
)}
); } }