133 lines
4.6 KiB
TypeScript
133 lines
4.6 KiB
TypeScript
'use client';
|
|
import { useState, useCallback } from 'react';
|
|
import {
|
|
PlagiarismFormErrors,
|
|
PlagiarismFormState,
|
|
SubmissionState,
|
|
} from './types';
|
|
import { isFormValid, validatePlagiarismForm } from './validation';
|
|
import { toast } from 'react-toastify';
|
|
import { useUserPlagiatStore } from '@/shared/zustand/user';
|
|
import { useMutation } from '@tanstack/react-query';
|
|
import { links } from '@/shared/request/links';
|
|
import { apiRequest } from '@/shared/request/apiRequest';
|
|
|
|
// ─── Initial States ──────────────────────────────────────────────────────────
|
|
|
|
const INITIAL_FORM: PlagiarismFormState = {
|
|
title: '',
|
|
file: null,
|
|
certificate: true,
|
|
text: '',
|
|
total_price: 41200,
|
|
};
|
|
|
|
const INITIAL_SUBMISSION: SubmissionState = {
|
|
status: 'idle',
|
|
error: null,
|
|
};
|
|
|
|
// ─── Hook ────────────────────────────────────────────────────────────────────
|
|
|
|
export function usePlagiarismForm() {
|
|
const user = useUserPlagiatStore((state) => state.user);
|
|
const [form, setForm] = useState<PlagiarismFormState>(INITIAL_FORM);
|
|
const [errors, setErrors] = useState<PlagiarismFormErrors>({});
|
|
const [isPaymentOpen, setIsPaymentOpen] = useState(false);
|
|
const [submission, setSubmission] =
|
|
useState<SubmissionState>(INITIAL_SUBMISSION);
|
|
|
|
const checkdocumentRequest = useMutation({
|
|
mutationKey: ['plagiarismCheck'],
|
|
mutationFn: (data: FormData) =>
|
|
apiRequest('POST', links.plagiarismCheck, data),
|
|
onSuccess: () => {
|
|
setSubmission({ status: 'success', error: null });
|
|
setForm(INITIAL_FORM);
|
|
setIsPaymentOpen(false);
|
|
},
|
|
onError: (err) => {
|
|
const message =
|
|
err instanceof Error ? err.message : 'An unexpected error occurred.';
|
|
setSubmission({ status: 'error', error: message });
|
|
setIsPaymentOpen(false);
|
|
},
|
|
});
|
|
|
|
// ── Field updaters ───────────────────────────────────────────────────────
|
|
|
|
const setTopic = useCallback((topic: string) => {
|
|
setForm((prev) => ({ ...prev, title: topic }));
|
|
setErrors((prev) => ({ ...prev, title: undefined }));
|
|
}, []);
|
|
|
|
const setFile = useCallback((file: File | null) => {
|
|
setForm((prev) => ({ ...prev, file }));
|
|
setErrors((prev) => ({ ...prev, file: undefined }));
|
|
}, []);
|
|
|
|
const toggleCertificate = useCallback(() => {
|
|
setForm((prev) => ({ ...prev, certificate: !prev.certificate }));
|
|
}, []);
|
|
|
|
// ── Submission ───────────────────────────────────────────────────────────
|
|
|
|
// 1. Wrap the form's onSubmit to intercept the event properly
|
|
const handleSubmitWithModal = useCallback(
|
|
async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
|
|
console.log('Form submitted user:', user); // Debugging log
|
|
if (user === null) {
|
|
toast.error('Iltimos, avval tizimga kiring!');
|
|
return;
|
|
}
|
|
// Run validation first
|
|
const validationErrors = validatePlagiarismForm(form);
|
|
if (!isFormValid(validationErrors)) {
|
|
setErrors(validationErrors);
|
|
return; // Don't open modal if invalid
|
|
}
|
|
|
|
// Validation passed → open the payment modal
|
|
setIsPaymentOpen(true);
|
|
},
|
|
[form],
|
|
);
|
|
|
|
const handleSubmit = useCallback(async () => {
|
|
setSubmission({ status: 'loading', error: null });
|
|
const fd = new FormData();
|
|
fd.append('title', form.title.trim());
|
|
fd.append('text', `${user?.name} ${user?.surname}` || '');
|
|
fd.append('file', form.file!); // File object — multipart/form-data
|
|
fd.append('certificate', String(form.certificate));
|
|
fd.append('total_price', '41200');
|
|
checkdocumentRequest.mutate(fd);
|
|
}, [form, user]);
|
|
|
|
const resetSubmission = useCallback(() => {
|
|
setSubmission(INITIAL_SUBMISSION);
|
|
}, []);
|
|
|
|
// ── Derived state ────────────────────────────────────────────────────────
|
|
|
|
const isLoading = submission.status === 'loading';
|
|
|
|
return {
|
|
form,
|
|
errors,
|
|
submission,
|
|
senderFullName: user ? `${user?.name} ${user?.surname}` : null,
|
|
isLoading,
|
|
setTopic,
|
|
setFile,
|
|
toggleCertificate,
|
|
handleSubmit,
|
|
resetSubmission,
|
|
handleSubmitWithModal,
|
|
setIsPaymentOpen,
|
|
isPaymentOpen,
|
|
};
|
|
}
|