2026-03-26 17:30:30 +07:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
|
|
import { ViolationFileDetail } from '@/app/hooks/react-query/useFileViolations';
|
|
|
|
|
import { useActionState, useEffect, useState } from 'react';
|
|
|
|
|
import { useTranslations } from 'next-intl';
|
2026-04-29 15:40:34 +07:00
|
|
|
import { createComplaint, fetchComplainInfo, MatchStatus } from '@/app/actions/violationActions';
|
2026-03-26 17:30:30 +07:00
|
|
|
import { toast } from 'sonner';
|
2026-04-29 15:40:34 +07:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2026-04-15 13:30:19 +07:00
|
|
|
import { formatDate, formatDateTime } from '@/app/lib/formatDate';
|
2026-03-26 17:30:30 +07:00
|
|
|
|
|
|
|
|
interface complaintInfo {
|
|
|
|
|
id: number,
|
|
|
|
|
status: string,
|
|
|
|
|
complaint_text: string,
|
|
|
|
|
violation_id: number,
|
|
|
|
|
email: string,
|
|
|
|
|
created_at: string,
|
|
|
|
|
updated_at: string,
|
|
|
|
|
not_moderated: boolean
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 19:05:44 +07:00
|
|
|
const ENABLED_STATUSES = ['COMPLAINT_IN_WORK', 'COMPLAINT_AND_LEGAL_IN_WORK'];
|
|
|
|
|
type UpdateStatusHandler = (id: number, status: MatchStatus) => Promise<boolean>;
|
|
|
|
|
|
|
|
|
|
export default function CaseComplaint({ selectedViolation, updateStatusHandler }: { selectedViolation: ViolationFileDetail, updateStatusHandler: UpdateStatusHandler }) {
|
2026-03-26 17:30:30 +07:00
|
|
|
const t = useTranslations('Global');
|
|
|
|
|
|
2026-03-27 15:10:06 +07:00
|
|
|
const [localViolation, setLocalViolation] = useState<ViolationFileDetail>(selectedViolation);
|
2026-04-14 16:01:49 +07:00
|
|
|
const [noteText, setNoteText] = useState('');
|
2026-03-27 15:10:06 +07:00
|
|
|
const [state, formAction, isPending] = useActionState(createComplaint, undefined);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setLocalViolation(selectedViolation);
|
|
|
|
|
}, [selectedViolation]);
|
|
|
|
|
|
2026-03-26 17:30:30 +07:00
|
|
|
const { data: complainInfo, isLoading: isLoadingComplain } = useQuery({
|
2026-03-27 15:10:06 +07:00
|
|
|
queryKey: ['complainInfo', localViolation.id],
|
|
|
|
|
queryFn: () => fetchComplainInfo(localViolation.id),
|
2026-03-26 17:30:30 +07:00
|
|
|
select: (data) => {
|
|
|
|
|
if (data) {
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-04-07 19:05:44 +07:00
|
|
|
enabled: ENABLED_STATUSES.includes(localViolation.status),
|
2026-03-26 17:30:30 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (state?.success) {
|
2026-03-27 15:10:06 +07:00
|
|
|
toast.success(t('the-complaint-has-been-registered'));
|
|
|
|
|
|
2026-04-07 19:05:44 +07:00
|
|
|
const newStatus = selectedViolation.status === 'LEGAL_IN_WORK'
|
|
|
|
|
? 'COMPLAINT_AND_LEGAL_IN_WORK'
|
|
|
|
|
: 'COMPLAINT_IN_WORK';
|
|
|
|
|
|
2026-03-27 15:10:06 +07:00
|
|
|
setLocalViolation(prev => ({
|
|
|
|
|
...prev,
|
2026-04-07 19:05:44 +07:00
|
|
|
status: newStatus
|
2026-03-27 15:10:06 +07:00
|
|
|
}));
|
|
|
|
|
|
2026-04-07 19:05:44 +07:00
|
|
|
updateStatusHandler(selectedViolation.id, newStatus);
|
2026-03-26 17:30:30 +07:00
|
|
|
} else if (state && !state.success) {
|
2026-03-31 14:47:39 +07:00
|
|
|
console.log(state.errorMessage);
|
|
|
|
|
if (state.errorMessage?.server) {
|
|
|
|
|
toast.warning(t(state.errorMessage.server));
|
2026-03-27 15:10:06 +07:00
|
|
|
} else {
|
|
|
|
|
toast.warning(t('the-complaint-has-not-been-registered'));
|
|
|
|
|
}
|
2026-03-26 17:30:30 +07:00
|
|
|
}
|
2026-04-07 19:05:44 +07:00
|
|
|
}, [state, updateStatusHandler, selectedViolation.id]);
|
2026-03-26 17:30:30 +07:00
|
|
|
|
2026-04-14 16:01:49 +07:00
|
|
|
function templateHandler(e: React.MouseEvent<HTMLLIElement>) {
|
|
|
|
|
const sampleItemText = e.currentTarget.querySelector('.sample-item-text');
|
|
|
|
|
|
|
|
|
|
if (sampleItemText) {
|
|
|
|
|
const text = sampleItemText.textContent || '';
|
|
|
|
|
setNoteText(text);
|
|
|
|
|
}
|
2026-03-26 17:30:30 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-07 19:05:44 +07:00
|
|
|
if (!ENABLED_STATUSES.includes(localViolation.status)) {
|
2026-03-26 17:30:30 +07:00
|
|
|
return (
|
|
|
|
|
<div className="violation-info-case">
|
2026-04-14 16:01:49 +07:00
|
|
|
|
|
|
|
|
<div className="note-form">
|
|
|
|
|
<form action={formAction}>
|
|
|
|
|
<input type="hidden" name="violationId" value={localViolation.id} />
|
2026-04-17 18:23:13 +07:00
|
|
|
<section
|
2026-04-23 11:42:54 +07:00
|
|
|
className="note-form-section"
|
2026-04-16 17:07:14 +07:00
|
|
|
>
|
2026-04-23 11:42:54 +07:00
|
|
|
<div
|
|
|
|
|
className="note-form-group"
|
2026-04-17 18:23:13 +07:00
|
|
|
>
|
2026-04-23 11:42:54 +07:00
|
|
|
<label
|
|
|
|
|
className="note-case-label"
|
|
|
|
|
htmlFor="note"
|
|
|
|
|
>
|
|
|
|
|
{t('text-of-the-complaint')}
|
|
|
|
|
</label>
|
2026-04-17 18:23:13 +07:00
|
|
|
|
2026-04-23 11:42:54 +07:00
|
|
|
<textarea
|
|
|
|
|
name="note"
|
|
|
|
|
className="note-textarea"
|
|
|
|
|
placeholder={`${t('text-area-error-fill-complaint-text')}...`}
|
|
|
|
|
value={noteText}
|
|
|
|
|
onChange={(e) => setNoteText(e.target.value)}
|
|
|
|
|
>
|
|
|
|
|
</textarea>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{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>
|
|
|
|
|
)}
|
2026-04-17 18:23:13 +07:00
|
|
|
</section>
|
|
|
|
|
|
2026-04-14 16:01:49 +07:00
|
|
|
<div
|
|
|
|
|
className="button-actions"
|
|
|
|
|
>
|
|
|
|
|
<button
|
|
|
|
|
type="submit"
|
2026-04-16 17:07:14 +07:00
|
|
|
className="btn-s btn-primary-small"
|
2026-04-14 16:01:49 +07:00
|
|
|
disabled={isPending}
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Сохранить шаблон
|
|
|
|
|
</button>
|
2026-03-26 17:30:30 +07:00
|
|
|
|
2026-04-14 16:01:49 +07:00
|
|
|
<button
|
|
|
|
|
type="submit"
|
2026-04-16 17:07:14 +07:00
|
|
|
className="btn-s btn-primary-small"
|
2026-04-14 16:01:49 +07:00
|
|
|
disabled={isPending}
|
|
|
|
|
>
|
|
|
|
|
{t('submit-violation')}
|
|
|
|
|
</button>
|
2026-03-26 17:30:30 +07:00
|
|
|
</div>
|
2026-04-14 16:01:49 +07:00
|
|
|
</form>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className="samples-wrapper"
|
|
|
|
|
>
|
|
|
|
|
<h5>Шаблоны</h5>
|
|
|
|
|
|
|
|
|
|
<ul
|
|
|
|
|
className="samples-list"
|
|
|
|
|
>
|
|
|
|
|
<li
|
|
|
|
|
className="sample-item"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
templateHandler(e)
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-title"
|
|
|
|
|
>
|
|
|
|
|
title 1
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-text"
|
|
|
|
|
>
|
|
|
|
|
1text text text text text text text text text text text text
|
|
|
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
|
<li
|
|
|
|
|
className="sample-item"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
templateHandler(e)
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-title"
|
|
|
|
|
>
|
|
|
|
|
title 2
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-text"
|
|
|
|
|
>
|
|
|
|
|
2text text text text text text text text text text text text
|
|
|
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
|
<li
|
|
|
|
|
className="sample-item"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
templateHandler(e)
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-title"
|
|
|
|
|
>
|
|
|
|
|
title 3
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-text"
|
|
|
|
|
>
|
|
|
|
|
3text text text text text text text text text text text text
|
|
|
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li
|
|
|
|
|
className="sample-item"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
templateHandler(e)
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-title"
|
|
|
|
|
>
|
|
|
|
|
title 3
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-text"
|
|
|
|
|
>
|
|
|
|
|
3text text text text text text text text text text text text
|
|
|
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li
|
|
|
|
|
className="sample-item"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
templateHandler(e)
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-title"
|
|
|
|
|
>
|
|
|
|
|
title 3
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className="sample-item-text"
|
|
|
|
|
>
|
|
|
|
|
3text text text text text text text text text text text text
|
|
|
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
2026-03-26 17:30:30 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
if (isLoadingComplain) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="violation-info-case">
|
2026-04-08 13:41:56 +07:00
|
|
|
<div
|
|
|
|
|
className="loading-animation"
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className="global-spinner large"
|
|
|
|
|
>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-03-26 17:30:30 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="violation-info-case">
|
2026-04-20 16:08:10 +07:00
|
|
|
{complainInfo?.body?.content ? (
|
2026-04-15 13:30:19 +07:00
|
|
|
<>
|
2026-03-26 17:30:30 +07:00
|
|
|
{complainInfo.body.content.map((item: complaintInfo) => {
|
|
|
|
|
return (
|
|
|
|
|
<div key={item.id}>
|
|
|
|
|
<div className="complaint-details">
|
2026-04-15 13:30:19 +07:00
|
|
|
<div className="complaint-details-item">
|
|
|
|
|
<div className="complaint-details-header">
|
2026-04-16 17:07:14 +07:00
|
|
|
<h5>COM-{item.id}</h5>
|
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">
|
2026-04-18 14:25:20 +07:00
|
|
|
<div className="complaint-details-title">{t('Status')}:</div>
|
|
|
|
|
<div
|
|
|
|
|
className="complaint-details-content"
|
|
|
|
|
>
|
|
|
|
|
{item.status}
|
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">
|
2026-04-18 14:25:20 +07:00
|
|
|
<div className="complaint-details-title">{t('date-of-creation')}:</div>
|
|
|
|
|
<div
|
|
|
|
|
className="complaint-details-content"
|
|
|
|
|
>
|
|
|
|
|
{item.created_at ? `${formatDate(item.created_at)}: ${formatDateTime(item.created_at)}` : '---'}
|
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">
|
2026-04-18 14:25:20 +07:00
|
|
|
<div className="complaint-details-title">{t('date-of-update')}:</div>
|
|
|
|
|
<div
|
|
|
|
|
className="complaint-details-content"
|
|
|
|
|
>
|
|
|
|
|
{item.updated_at ? `${formatDate(item.updated_at)}: ${formatDateTime(item.updated_at)}` : '---'}
|
2026-04-16 17:07:14 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="complaint-details-item">
|
2026-04-18 14:25:20 +07:00
|
|
|
<div className="complaint-details-title">{t('text-of-the-complaint')}:
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className="complaint-details-content"
|
|
|
|
|
>
|
|
|
|
|
{item.complaint_text}
|
2026-04-15 13:30:19 +07:00
|
|
|
</div>
|
2026-04-14 16:01:49 +07:00
|
|
|
</div>
|
2026-03-26 17:30:30 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2026-04-15 13:30:19 +07:00
|
|
|
</>
|
2026-03-26 17:30:30 +07:00
|
|
|
) : (
|
2026-03-27 15:10:06 +07:00
|
|
|
<div>
|
|
|
|
|
{t('no-information-about-the-complaint')}
|
|
|
|
|
</div>
|
2026-03-26 17:30:30 +07:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|