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';
|
|
|
|
|
import { createComplaint, fetchComplainInfo } from '@/app/actions/violationActions';
|
|
|
|
|
import { toast } from 'sonner';
|
|
|
|
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
2026-04-07 19:05:44 +07:00
|
|
|
import { MatchStatus } from '@/app/actions/violationActions';
|
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');
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
2026-03-27 15:10:06 +07:00
|
|
|
const [showCreateCase, setShowCreateCase] = useState(false);
|
|
|
|
|
const [localViolation, setLocalViolation] = useState<ViolationFileDetail>(selectedViolation);
|
|
|
|
|
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-27 15:10:06 +07:00
|
|
|
|
|
|
|
|
setShowCreateCase(false);
|
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
|
|
|
|
|
|
|
|
function toggleForm() {
|
|
|
|
|
setShowCreateCase(!showCreateCase);
|
|
|
|
|
}
|
|
|
|
|
|
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">
|
|
|
|
|
{!showCreateCase ? (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
className="btn-action btn-case"
|
|
|
|
|
onClick={toggleForm}
|
|
|
|
|
>
|
2026-03-27 15:10:06 +07:00
|
|
|
{t('filing-complaint')}
|
2026-03-26 17:30:30 +07:00
|
|
|
</button>
|
|
|
|
|
) : (
|
|
|
|
|
<div>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
className="btn-action btn-case"
|
|
|
|
|
onClick={toggleForm}
|
|
|
|
|
>
|
|
|
|
|
{t('cancel')}
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
<div className="note-form">
|
|
|
|
|
<form action={formAction}>
|
2026-03-27 15:10:06 +07:00
|
|
|
<input type="hidden" name="violationId" value={localViolation.id} />
|
2026-03-26 17:30:30 +07:00
|
|
|
<textarea
|
|
|
|
|
name="note"
|
|
|
|
|
className="note-textarea"
|
2026-03-27 15:10:06 +07:00
|
|
|
placeholder={`${t('text-of-the-complaint')}...`}>
|
2026-03-26 17:30:30 +07:00
|
|
|
</textarea>
|
2026-03-31 14:47:39 +07:00
|
|
|
<div
|
|
|
|
|
className="flex gap-3"
|
2026-03-26 17:30:30 +07:00
|
|
|
>
|
2026-03-31 14:47:39 +07:00
|
|
|
<button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="btn-small btn-primary-small"
|
|
|
|
|
disabled={isPending}
|
|
|
|
|
>
|
|
|
|
|
{t('submit-violation')}
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
{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>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-03-26 17:30:30 +07:00
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
if (isLoadingComplain) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="violation-info-case">
|
2026-03-27 15:10:06 +07:00
|
|
|
<div>{t('loading')}...</div>
|
2026-03-26 17:30:30 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="violation-info-case">
|
|
|
|
|
{complainInfo?.body.content ? (
|
|
|
|
|
<div>
|
|
|
|
|
{complainInfo.body.content.map((item: complaintInfo) => {
|
|
|
|
|
return (
|
|
|
|
|
<div key={item.id}>
|
2026-03-27 15:10:06 +07:00
|
|
|
<h3 className="mb-3">
|
|
|
|
|
{t('complaint-Information')}
|
2026-03-26 17:30:30 +07:00
|
|
|
</h3>
|
|
|
|
|
<div className="complaint-details">
|
|
|
|
|
<p><strong>ID:</strong> {item.id}</p>
|
2026-03-27 15:10:06 +07:00
|
|
|
<p><strong>{t('status')}:</strong> {item.status}</p>
|
|
|
|
|
<p><strong>{t('text-of-the-complaint')}:</strong> {item.complaint_text}</p>
|
|
|
|
|
<p><strong>{t('violation-id')}:</strong> {item.violation_id}</p>
|
|
|
|
|
<p><strong>{t('email')}:</strong> {item.email}</p>
|
|
|
|
|
<p><strong>{t('date-of-creation')}:</strong> {item.created_at}</p>
|
|
|
|
|
<p><strong>{t('date-of-update')}:</strong> {item.updated_at}</p>
|
2026-03-26 17:30:30 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
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>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|