Files
no-copy-frontend/src/app/ui/file-page/violation-table/case-legal.tsx
T
2026-04-14 16:01:49 +07:00

344 lines
9.0 KiB
TypeScript

'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<boolean>;
export default function CaseViolation({ selectedViolation, updateStatusHandler }: { selectedViolation: ViolationFileDetail, updateStatusHandler: UpdateStatusHandler }) {
const t = useTranslations('Global');
const queryClient = useQueryClient();
const [localViolation, setLocalViolation] = useState<ViolationFileDetail>(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);
} 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(localViolation.status)) {
return (
<div className="violation-info-case">
<div className="note-form">
<form action={formAction}>
<input type="hidden" name="violationId" value={localViolation.id} />
<div
className="note-form-flex"
>
<section
className="note-form-group"
>
<label
className="note-case-label"
htmlFor="caseTitle"
>
{/* {t('case-name')} */}
Название претензии
</label>
<input
type="text"
name="caseTitle"
className="note-case-input"
placeholder={t('fill-title-of-the-case') + '...'}
/>
{state?.errorMessage?.caseTitle && (
<p
className="text-sm text-red-500"
>
{
state?.errorMessage?.caseTitle.split('&').map((e, index) => {
return (
<span key={index}>
{t(e)}
<br />
</span>
)
})
}
</p>
)}
</section>
<section
className="note-form-group"
>
<label
className="note-case-label"
htmlFor="amount"
>
{t('amount-of-damage')}
</label>
<input
type="number"
name="amount"
className="note-case-input"
placeholder={t('enter-the-amount-of-damage') + '...'}
/>
{state?.errorMessage?.amount && (
<p
className="text-sm text-red-500"
>
{
state?.errorMessage?.amount.split('&').map((e, index) => {
return (
<span key={index}>
{t(e)}
<br />
</span>
)
})
}
</p>
)}
</section>
</div>
<section
className="note-textarea-wrapper"
>
<textarea
name="note"
className="note-textarea"
placeholder={t('fill-text-of-the-case') + '...'}
>
</textarea>
{state?.errorMessage?.textArea && (
<p
className="text-sm text-red-500"
>
{
state?.errorMessage?.textArea.split('&').map((e, index) => {
return (
<span key={index}>
{t(e)}
<br />
</span>
)
})
}
</p>
)}
</section>
<div
className="button-actions"
>
<button
type="submit"
className="btn-small btn-primary-small"
disabled={isPending}
>
{t('submit-violation')}
</button>
</div>
</form>
</div>
</div>
);
} else {
if (isLoadingCase) {
return (
<div className="violation-info-case">
<div
className="loading-animation"
>
<div
className="global-spinner large"
>
</div>
</div>
</div>
);
}
return (
<div className="violation-info-case">
{caseInfo?.body?.content ? (
<div>
{caseInfo.body.content.map((item: CaseInfo) => {
return (
<div key={item.id}>
{/* <div className="complaint-details">
<div><strong>ID:</strong> {item.id}</div>
<div><strong>{t('name')}:</strong>
<br />
{item.name}
</div>
<div><strong>{t('description')}:</strong> {item.description}</div>
<div><strong>{t('amount')}:</strong> {item.amount}</div>
<div><strong>{t('priority')}:</strong> {item.priority}</div>
<div><strong>{t('type')}:</strong> {item.type}</div>
<div><strong>{t('lawyer')}:</strong> {item.lawyer || t('not-assigned')}</div>
<div><strong>{t('violation-id')}:</strong> {item.violationId}</div>
<div><strong>{t('date-of-creation')}:</strong> {item.createdAt ? `${formatDate(item.createdAt)}: ${formatDateTime(item.createdAt)}` : '---'}</div>
<div><strong>{t('date-of-update')}:</strong> {item.createdAt ? `${formatDate(item.createdAt)}: ${formatDateTime(item.createdAt)}` : '---'}</div>
{item.content && <p><strong>{t('content')}:</strong> {item.content}</p>}
</div> */}
<div className="complaint-details">
<div
className="complaint-details-item"
>
<strong>ID:</strong> {item.id}
</div>
<div
className="complaint-details-item"
>
<span
className="complaint-details-title"
>
{t('status')}:
</span>
<br />
<span
className="complaint-details-text"
>
{item.type}
</span>
</div>
<div
className="complaint-details-item"
>
<span
className="complaint-details-title"
>
{t('date-of-creation')}:
</span>
<br />
<span
className="complaint-details-text"
>
{item.createdAt ? `${formatDate(item.createdAt)}: ${formatDateTime(item.createdAt)}` : '---'}
</span>
</div>
<div
className="complaint-details-item"
>
<span
className="complaint-details-title"
>
{t('date-of-update')}:
</span>
<br />
<span
className="complaint-details-text"
>
{item.updatedAt ? `${formatDate(item.updatedAt)}: ${formatDateTime(item.updatedAt)}` : '---'}
</span>
</div>
<div
className="complaint-details-item"
>
<span
className="complaint-details-title"
>
{t('description')}:
</span>
<br />
<span
className="complaint-details-text"
>
{item.description}
</span>
</div>
<div
className="complaint-details-item"
>
<span
className="complaint-details-title"
>
{t('amount-of-damage')}:
</span>
<br />
<span
className="complaint-details-text"
>
{item.amount}
</span>
</div>
<div
className="complaint-details-item"
>
<span
className="complaint-details-title"
>
{t('lawyer')}:
</span>
<br />
<span
className="complaint-details-text"
>
{item.lawyer}
</span>
</div>
</div>
</div>
);
})}
</div>
) : (
<div>
{t('no-information-about-the-complaint')}
</div>
)}
</div>
);
}
}