Files
no-copy-frontend/src/app/ui/file-page/violation-table/case-claims.tsx
T

324 lines
8.1 KiB
TypeScript
Raw Normal View History

2026-04-07 19:05:44 +07:00
'use client'
import { ViolationFileDetail } from '@/app/hooks/react-query/useFileViolations';
2026-04-30 13:37:01 +07:00
import { useEffect, useActionState, SetStateAction } from 'react';
import { fetchCaseInfo, createCase, MatchStatus } from '@/app/actions/violationActions';
import { useQuery } from '@tanstack/react-query';
2026-04-07 19:05:44 +07:00
import { useTranslations } from 'next-intl';
import { toast } from 'sonner';
import { formatDate, formatDateTime } from '@/app/lib/formatDate';
2026-04-07 19:05:44 +07:00
interface CaseInfo {
id: number;
name: string;
description: string;
amount: number;
priority: string;
type: string;
lawyer: string | null;
2026-04-23 12:43:27 +07:00
created_at: string;
updated_at: string;
page_number: number;
page_size: number;
total_elements: number;
total_pages: number;
violation_id: number;
2026-04-07 19:05:44 +07:00
content: string | null;
}
2026-04-07 19:05:44 +07:00
const ENABLED_STATUSES = ['LEGAL_IN_WORK', 'COMPLAINT_AND_LEGAL_IN_WORK'];
type UpdateStatusHandler = (id: number, status: MatchStatus) => Promise<boolean>;
2026-04-30 13:37:01 +07:00
export default function CaseViolation({ selectedViolation, updateStatusHandler, setLocalViolation }:
{
selectedViolation: ViolationFileDetail,
updateStatusHandler: UpdateStatusHandler,
setLocalViolation: React.Dispatch<SetStateAction<ViolationFileDetail>>
}
) {
2026-04-07 19:05:44 +07:00
const t = useTranslations('Global');
const [state, formAction, isPending] = useActionState(createCase, undefined);
const { data: caseInfo, isLoading: isLoadingCase } = useQuery({
2026-04-30 13:37:01 +07:00
queryKey: ['caseInfo', selectedViolation.id],
queryFn: () => fetchCaseInfo(selectedViolation.id),
2026-04-07 19:05:44 +07:00
select: (data) => {
if (data) {
return data
}
},
2026-04-30 13:37:01 +07:00
enabled: ENABLED_STATUSES.includes(selectedViolation.status)
2026-04-07 19:05:44 +07:00
});
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
2026-04-30 13:37:01 +07:00
}))
2026-04-07 19:05:44 +07:00
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]);
2026-04-30 13:37:01 +07:00
if (!ENABLED_STATUSES.includes(selectedViolation.status)) {
2026-04-07 19:05:44 +07:00
return (
<div className="violation-info-case">
2026-04-14 16:01:49 +07:00
<div className="note-form">
<form action={formAction}>
2026-04-30 13:37:01 +07:00
<input type="hidden" name="violationId" value={selectedViolation.id} />
2026-04-14 16:01:49 +07:00
<div
className="note-form-flex"
2026-04-07 19:05:44 +07:00
>
2026-04-14 16:01:49 +07:00
<section
2026-04-23 11:42:54 +07:00
className="note-form-section"
2026-04-14 16:01:49 +07:00
>
2026-04-23 11:42:54 +07:00
<div
className="note-form-group"
2026-04-07 19:05:44 +07:00
>
2026-04-23 11:42:54 +07:00
<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') + '...'}
defaultValue={state?.previousCaseTitle ? state?.previousCaseTitle : ''}
2026-04-23 11:42:54 +07:00
/>
</div>
2026-04-14 16:01:49 +07:00
{state?.errorMessage?.caseTitle && (
<p
className="text-sm text-red-500"
2026-04-07 19:05:44 +07:00
>
2026-04-14 16:01:49 +07:00
{
state?.errorMessage?.caseTitle.split('&').map((e, index) => {
return (
<span key={index}>
{t(e)}
<br />
</span>
)
})
}
</p>
)}
</section>
<section
2026-04-23 11:42:54 +07:00
className="note-form-section"
2026-04-14 16:01:49 +07:00
>
2026-04-23 11:42:54 +07:00
<div
className="note-form-group"
>
2026-04-23 11:42:54 +07:00
<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') + '...'}
defaultValue={state?.previousAmount ? state?.previousAmount : ''}
2026-04-23 11:42:54 +07:00
/>
</div>
2026-04-14 16:01:49 +07:00
{state?.errorMessage?.amount && (
<p
className="text-sm text-red-500"
>
2026-04-14 16:01:49 +07:00
{
state?.errorMessage?.amount.split('&').map((e, index) => {
return (
<span key={index}>
{t(e)}
<br />
</span>
)
})
}
</p>
)}
</section>
2026-04-07 19:05:44 +07:00
</div>
2026-04-14 16:01:49 +07:00
<section
2026-04-23 11:42:54 +07:00
className="note-form-section"
2026-04-14 16:01:49 +07:00
>
2026-04-23 11:42:54 +07:00
<div
className="note-form-group"
>
2026-04-23 11:42:54 +07:00
<label
className="note-case-label"
htmlFor="note"
>
{t('text-of-the-case')}
</label>
2026-04-23 11:42:54 +07:00
<textarea
name="note"
className="note-textarea"
placeholder={t('fill-text-of-the-case') + '...'}
defaultValue={state?.previousText ? state?.previousText : ''}
2026-04-23 11:42:54 +07:00
>
</textarea>
</div>
2026-04-14 16:01:49 +07:00
{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-s btn-primary-small"
2026-04-14 16:01:49 +07:00
disabled={isPending}
>
{t('submit-violation')}
</button>
</div>
</form>
</div>
2026-04-07 19:05:44 +07:00
</div>
);
} else {
if (isLoadingCase) {
return (
<div className="violation-info-case">
<div
className="loading-animation"
>
<div
className="global-spinner large"
>
</div>
</div>
2026-04-07 19:05:44 +07:00
</div>
);
}
return (
<div className="violation-info-case">
{caseInfo?.body?.content ? (
2026-04-15 13:30:19 +07:00
<>
2026-04-07 19:05:44 +07:00
{caseInfo.body.content.map((item: CaseInfo) => {
return (
<div key={item.id}>
2026-04-14 16:01:49 +07:00
<div className="complaint-details">
2026-04-15 13:30:19 +07:00
<div className="complaint-details-item">
<h5>{item.name}</h5>
</div>
<div className="complaint-details-item">
<div className="complaint-details-title">
{t('Status')}:
</div>
<div
className="complaint-details-content"
>
{item.type}
2026-04-15 13:30:19 +07:00
</div>
2026-04-14 16:01:49 +07:00
</div>
2026-04-15 13:30:19 +07:00
<div className="complaint-details-item">
<div className="complaint-details-title">
{t('date-of-creation')}:
</div>
<div
className="complaint-details-content"
>
2026-04-23 12:43:27 +07:00
{item.created_at ? `${formatDate(item.created_at)}: ${formatDateTime(item.created_at)}` : '---'}
2026-04-15 13:30:19 +07:00
</div>
</div>
<div className="complaint-details-item">
<div className="complaint-details-title">
{t('date-of-update')}:
</div>
<div
className="complaint-details-content"
>
2026-04-23 12:43:27 +07:00
{item.updated_at ? `${formatDate(item.updated_at)}: ${formatDateTime(item.updated_at)}` : '---'}
2026-04-15 13:30:19 +07:00
</div>
</div>
<div className="complaint-details-item">
<div className="complaint-details-title">
{t('amount-of-damage')}:
</div>
<div
className="complaint-details-content"
>
{item.amount}
2026-04-15 13:30:19 +07:00
</div>
</div>
<div className="complaint-details-item">
<div className="complaint-details-title">
{t('lawyer')}:
</div>
<div
className="complaint-details-content"
>
{item.lawyer}
2026-04-15 13:30:19 +07:00
</div>
</div>
<div className="complaint-details-item">
<div className="complaint-details-title">
{t('description')}:
</div>
<div
className="complaint-details-content"
>
{item.description}
2026-04-15 13:30:19 +07:00
</div>
2026-04-14 16:01:49 +07:00
</div>
2026-04-07 19:05:44 +07:00
</div>
</div>
);
})}
2026-04-15 13:30:19 +07:00
</>
2026-04-07 19:05:44 +07:00
) : (
<div>
{t('no-information-about-the-complaint')}
</div>
)}
</div>
);
}
}