'use client' import { ViolationFileDetail } from '@/app/hooks/react-query/useFileViolations'; import { useActionState, useEffect, useState } from 'react'; import { useTranslations } from 'next-intl'; import { createComplaint, fetchComplainInfo } from '@/app/actions/violationActions'; import { toast } from 'sonner'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { MatchStatus } from '@/app/actions/violationActions'; interface complaintInfo { id: number, status: string, complaint_text: string, violation_id: number, email: string, created_at: string, updated_at: string, not_moderated: boolean } const ENABLED_STATUSES = ['COMPLAINT_IN_WORK', 'COMPLAINT_AND_LEGAL_IN_WORK']; type UpdateStatusHandler = (id: number, status: MatchStatus) => Promise; export default function CaseComplaint({ 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(createComplaint, undefined); useEffect(() => { setLocalViolation(selectedViolation); }, [selectedViolation]); const { data: complainInfo, isLoading: isLoadingComplain } = useQuery({ queryKey: ['complainInfo', localViolation.id], queryFn: () => fetchComplainInfo(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 === 'LEGAL_IN_WORK' ? 'COMPLAINT_AND_LEGAL_IN_WORK' : 'COMPLAINT_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?.textArea && (

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

)}
)}
); } else { if (isLoadingComplain) { return (
); } return (
{complainInfo?.body.content ? (
{complainInfo.body.content.map((item: complaintInfo) => { return (

{t('complaint-Information')}

ID: {item.id}

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

{t('text-of-the-complaint')}: {item.complaint_text}

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

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

{t('date-of-creation')}: {item.created_at}

{t('date-of-update')}: {item.updated_at}

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