detail page
This commit is contained in:
@@ -5,30 +5,31 @@ import {
|
||||
PlagiarismFormState,
|
||||
SubmissionState,
|
||||
} from './types';
|
||||
import { selectFullName, useUserStore } from './userStore';
|
||||
import { isFormValid, validatePlagiarismForm } from './validation';
|
||||
import { submitPlagiarismCheck } from '@/shared/request/plagiarismapi';
|
||||
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 = {
|
||||
topic: '',
|
||||
title: '',
|
||||
file: null,
|
||||
withCertificate: false,
|
||||
certificate: true,
|
||||
text: '',
|
||||
total_price: 41200,
|
||||
};
|
||||
|
||||
const INITIAL_SUBMISSION: SubmissionState = {
|
||||
status: 'idle',
|
||||
response: null,
|
||||
error: null,
|
||||
};
|
||||
|
||||
// ─── Hook ────────────────────────────────────────────────────────────────────
|
||||
|
||||
export function usePlagiarismForm() {
|
||||
const senderFullName = useUserStore(selectFullName);
|
||||
const user = useUserPlagiatStore((state) => state.user);
|
||||
const [form, setForm] = useState<PlagiarismFormState>(INITIAL_FORM);
|
||||
const [errors, setErrors] = useState<PlagiarismFormErrors>({});
|
||||
@@ -36,15 +37,28 @@ export function usePlagiarismForm() {
|
||||
const [submission, setSubmission] =
|
||||
useState<SubmissionState>(INITIAL_SUBMISSION);
|
||||
|
||||
// const checkdocumentRequest = useMutation({
|
||||
// mutationFn: (data:any) => apiRequest("POST",links.plagiarismCheck, data)
|
||||
// })
|
||||
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, topic }));
|
||||
setErrors((prev) => ({ ...prev, topic: undefined }));
|
||||
setForm((prev) => ({ ...prev, title: topic }));
|
||||
setErrors((prev) => ({ ...prev, title: undefined }));
|
||||
}, []);
|
||||
|
||||
const setFile = useCallback((file: File | null) => {
|
||||
@@ -53,7 +67,7 @@ export function usePlagiarismForm() {
|
||||
}, []);
|
||||
|
||||
const toggleCertificate = useCallback(() => {
|
||||
setForm((prev) => ({ ...prev, withCertificate: !prev.withCertificate }));
|
||||
setForm((prev) => ({ ...prev, certificate: !prev.certificate }));
|
||||
}, []);
|
||||
|
||||
// ── Submission ───────────────────────────────────────────────────────────
|
||||
@@ -82,23 +96,15 @@ export function usePlagiarismForm() {
|
||||
);
|
||||
|
||||
const handleSubmit = useCallback(async () => {
|
||||
setSubmission({ status: 'loading', response: null, error: null });
|
||||
try {
|
||||
const response = await submitPlagiarismCheck({
|
||||
topic: form.topic.trim(),
|
||||
senderFullName,
|
||||
file: form.file!,
|
||||
withCertificate: form.withCertificate,
|
||||
});
|
||||
setSubmission({ status: 'success', response, error: null });
|
||||
setForm(INITIAL_FORM);
|
||||
setIsPaymentOpen(false); // Close modal on success
|
||||
} catch (err) {
|
||||
const message =
|
||||
err instanceof Error ? err.message : 'An unexpected error occurred.';
|
||||
setSubmission({ status: 'error', response: null, error: message });
|
||||
}
|
||||
}, [form, senderFullName]);
|
||||
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);
|
||||
@@ -112,7 +118,7 @@ export function usePlagiarismForm() {
|
||||
form,
|
||||
errors,
|
||||
submission,
|
||||
senderFullName,
|
||||
senderFullName: user ? `${user?.name} ${user?.surname}` : null,
|
||||
isLoading,
|
||||
setTopic,
|
||||
setFile,
|
||||
|
||||
Reference in New Issue
Block a user