'use client' import { ViolationFileDetail } from '@/app/hooks/react-query/useFileViolations'; import { useActionState, useEffect, useState, SetStateAction } from 'react'; import { useTranslations } from 'next-intl'; import { createComplaint, fetchComplainInfo, MatchStatus } from '@/app/actions/violationActions'; import { toast } from 'sonner'; import { useQuery } from '@tanstack/react-query'; import { formatDate, formatDateTime } from '@/app/lib/formatDate'; import ModalWindow from '@/app/components/ModalWindow'; 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, setLocalViolation }: { selectedViolation: ViolationFileDetail, updateStatusHandler: UpdateStatusHandler, setLocalViolation: React.Dispatch> } ) { const t = useTranslations('Global'); const [noteText, setNoteText] = useState(''); const [state, formAction, isPending] = useActionState(createComplaint, undefined); const [isTemplateModalOpen, setIsTemplateModalOpen] = useState(false); const [templateName, setTemplateName] = useState(''); const { data: complainInfo, isLoading: isLoadingComplain } = useQuery({ queryKey: ['complainInfo', selectedViolation.id], queryFn: () => fetchComplainInfo(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 === 'LEGAL_IN_WORK' ? 'COMPLAINT_AND_LEGAL_IN_WORK' : 'COMPLAINT_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]); function templateHandler(e: React.MouseEvent) { const sampleItemText = e.currentTarget.querySelector('.sample-item-text'); if (sampleItemText) { const text = sampleItemText.textContent || ''; setNoteText(text); } } function openTemplateSaveModal() { if (!noteText.trim()) { toast.warning(t('please-enter-complaint-text-first')); return; } setTemplateName(''); setIsTemplateModalOpen(true); } function saveTemplateHandler() { if (!templateName.trim()) { toast.warning(t('please-enter-template-name')); return; } // пока нету эндпоинта оставлю заглушку console.log('СОХРАНЕНИЕ ШАБЛОНА'); console.log('Название шаблона:', templateName); console.log('Текст жалобы:', noteText); console.log('========================'); toast.success(t('template-saved-successfully') || 'Шаблон успешно сохранен'); setIsTemplateModalOpen(false); setTemplateName(''); } if (!ENABLED_STATUSES.includes(selectedViolation.status)) { return ( <>
{state?.errorMessage?.textArea && (

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

)}
{/* пока скроем интерфейс с шаблонами */} {/* */}
{/* пока скроем интерфейс с шаблонами */} {/*
Шаблоны
  • { templateHandler(e) }} >
    title 1
    1text text text text text text text text text text text text
  • { templateHandler(e) }} >
    title 2
    2text text text text text text text text text text text text
  • { templateHandler(e) }} >
    title 3
    3text text text text text text text text text text text text
  • { templateHandler(e) }} >
    title 3
    3text text text text text text text text text text text text
  • { templateHandler(e) }} >
    title 3
    3text text text text text text text text text text text text
*/}

{t('save-template')}

setTemplateName(e.target.value)} autoFocus />
{t('complaint-text-preview')}
{noteText || t('no-text')}
); } else { if (isLoadingComplain) { return (
); } return (
{complainInfo?.body ? (
COM-{complainInfo.body?.id}
{t('Status')}:
{complainInfo.body?.status}
{t('date-of-creation')}:
{complainInfo.body?.created_at ? `${formatDate(complainInfo.body.created_at)}: ${formatDateTime(complainInfo.body.created_at)}` : '---'}
{t('date-of-update')}:
{complainInfo.body?.updated_at ? `${formatDate(complainInfo.body.updated_at)}: ${formatDateTime(complainInfo.body.updated_at)}` : '---'}
{t('text-of-the-complaint')}:
{complainInfo.body?.complaint_text}
) : (
{t('no-information-about-the-complaint')}
)}
); } }