plagiatcheck part complated base new request types
This commit is contained in:
53
src/widgets/plagiatCheck/lib/types.ts
Normal file
53
src/widgets/plagiatCheck/lib/types.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
// ─── Domain Types ───────────────────────────────────────────────────────────
|
||||
|
||||
import { PriceCalculate } from '@/features/modals/paymentModal/lib/types';
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
export interface PlagiarismSubmissionPayload {
|
||||
topic: string;
|
||||
senderFullName: string;
|
||||
file: File;
|
||||
withCertificate: boolean;
|
||||
}
|
||||
|
||||
export interface PlagiarismSubmissionResponse {
|
||||
submissionId: string;
|
||||
status: 'pending' | 'processing' | 'completed' | 'failed';
|
||||
message: string;
|
||||
certificateUrl?: string;
|
||||
}
|
||||
|
||||
export interface CheckDocumentRequestResponse extends PriceCalculate {
|
||||
id: number;
|
||||
order_id: number;
|
||||
}
|
||||
|
||||
// ─── Form State Types ────────────────────────────────────────────────────────
|
||||
|
||||
export interface PlagiarismFormState {
|
||||
title: string;
|
||||
file: File | null;
|
||||
certificate: boolean;
|
||||
text?: string;
|
||||
total_price: number;
|
||||
document_type: string;
|
||||
}
|
||||
|
||||
export type PlagiarismFormErrors = Partial<
|
||||
Record<keyof PlagiarismFormState, string>
|
||||
>;
|
||||
|
||||
// ─── UI State Types ──────────────────────────────────────────────────────────
|
||||
|
||||
export type SubmissionStatus = 'idle' | 'loading' | 'success' | 'error';
|
||||
|
||||
export interface SubmissionState {
|
||||
status: SubmissionStatus;
|
||||
error: string | null;
|
||||
}
|
||||
193
src/widgets/plagiatCheck/lib/usePlagiraism.ts
Normal file
193
src/widgets/plagiatCheck/lib/usePlagiraism.ts
Normal file
@@ -0,0 +1,193 @@
|
||||
'use client';
|
||||
import { useState, useCallback, useEffect } from 'react';
|
||||
import {
|
||||
CheckDocumentRequestResponse,
|
||||
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';
|
||||
import { PriceCalculate } from '@/features/modals/paymentModal/lib/types';
|
||||
|
||||
// ─── Initial States ──────────────────────────────────────────────────────────
|
||||
|
||||
const INITIAL_FORM: PlagiarismFormState = {
|
||||
title: '',
|
||||
file: null,
|
||||
certificate: true,
|
||||
text: '',
|
||||
total_price: 41200,
|
||||
document_type: 'boshqa',
|
||||
};
|
||||
|
||||
const PRICE: PriceCalculate = {
|
||||
service_fee: 0,
|
||||
discount: 0,
|
||||
total_price: 0,
|
||||
};
|
||||
|
||||
const INITIAL_SUBMISSION: SubmissionState = {
|
||||
status: 'idle',
|
||||
error: null,
|
||||
};
|
||||
|
||||
// ─── Hook ────────────────────────────────────────────────────────────────────
|
||||
|
||||
export function usePlagiarismForm() {
|
||||
const user = useUserPlagiatStore((state) => state.user);
|
||||
const [localUser, setLocalUser] = useState<{
|
||||
id: number;
|
||||
name: string;
|
||||
surname: string;
|
||||
} | null>(null);
|
||||
useEffect(() => {
|
||||
const data = localStorage.getItem('user');
|
||||
|
||||
if (data) {
|
||||
setLocalUser(JSON.parse(data));
|
||||
} else {
|
||||
setLocalUser(null);
|
||||
}
|
||||
}, [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 [order_id, setOrder_id] = useState<number>(0);
|
||||
const [prices, setPrices] = useState<PriceCalculate>(PRICE);
|
||||
|
||||
const checkdocumentRequest = useMutation({
|
||||
mutationKey: ['plagiarismCheck'],
|
||||
mutationFn: (data: FormData) =>
|
||||
apiRequest('POST', links.plagiarismCheck, data),
|
||||
onSuccess: (res) => {
|
||||
console.log('uploda: ', res);
|
||||
const resdata = res.data as CheckDocumentRequestResponse;
|
||||
const priceInfo: PriceCalculate = {
|
||||
total_price: resdata?.total_price || 0,
|
||||
discount: resdata?.discount || 0,
|
||||
service_fee: resdata?.service_fee || 0,
|
||||
};
|
||||
setPrices(priceInfo);
|
||||
console.log('order_id:', resdata.id);
|
||||
setOrder_id(resdata.order_id);
|
||||
setSubmission({ status: 'success', error: null });
|
||||
setForm(INITIAL_FORM);
|
||||
setIsPaymentOpen(true);
|
||||
},
|
||||
onError: (err) => {
|
||||
const message =
|
||||
err instanceof Error ? err.message : 'An unexpected error occurred.';
|
||||
setSubmission({ status: 'error', error: message });
|
||||
},
|
||||
});
|
||||
|
||||
const payment = useMutation({
|
||||
mutationKey: ['payload'],
|
||||
mutationFn: ({ order_id }: { order_id: number }) =>
|
||||
apiRequest<{ payment_link: string }>('POST', links.payment(order_id)),
|
||||
onSuccess: (res) => {
|
||||
console.log('payment res: ', res);
|
||||
window.open(res.data.payment_link, '_self');
|
||||
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 setOption = useCallback((option: string) => {
|
||||
setForm((prev) => ({ ...prev, document_type: option }));
|
||||
setErrors((prev) => ({ ...prev, document_type: 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 (localUser === 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
|
||||
}
|
||||
|
||||
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');
|
||||
fd.append('type', form.document_type);
|
||||
checkdocumentRequest.mutate(fd);
|
||||
},
|
||||
[form],
|
||||
);
|
||||
|
||||
const handleSubmit = useCallback(async () => {
|
||||
setSubmission({ status: 'loading', error: null });
|
||||
|
||||
payment.mutate({ order_id });
|
||||
}, [form, user]);
|
||||
|
||||
const resetSubmission = useCallback(() => {
|
||||
setSubmission(INITIAL_SUBMISSION);
|
||||
}, []);
|
||||
|
||||
// ── Derived state ────────────────────────────────────────────────────────
|
||||
|
||||
const isLoading = submission.status === 'loading';
|
||||
|
||||
return {
|
||||
form,
|
||||
errors,
|
||||
submission,
|
||||
senderFullName: localUser
|
||||
? `${localUser?.name} ${localUser?.surname}`
|
||||
: null,
|
||||
isLoading,
|
||||
setTopic,
|
||||
setFile,
|
||||
toggleCertificate,
|
||||
handleSubmit,
|
||||
resetSubmission,
|
||||
handleSubmitWithModal,
|
||||
setIsPaymentOpen,
|
||||
isPaymentOpen,
|
||||
setOption,
|
||||
prices,
|
||||
};
|
||||
}
|
||||
33
src/widgets/plagiatCheck/lib/userStore.ts
Normal file
33
src/widgets/plagiatCheck/lib/userStore.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { create } from 'zustand';
|
||||
import { User } from './types';
|
||||
|
||||
// ─── Store Interface ─────────────────────────────────────────────────────────
|
||||
|
||||
interface UserStore {
|
||||
user: User | null;
|
||||
isAuthenticated: boolean;
|
||||
setUser: (user: User) => void;
|
||||
clearUser: () => void;
|
||||
}
|
||||
|
||||
// ─── Store Implementation ────────────────────────────────────────────────────
|
||||
|
||||
export const useUserStore = create<UserStore>((set) => ({
|
||||
// Mock: pre-populated as if the user is already logged in
|
||||
user: {
|
||||
id: 'usr_01',
|
||||
firstName: 'Amir',
|
||||
lastName: 'Karimov',
|
||||
email: 'amir.karimov@example.com',
|
||||
},
|
||||
isAuthenticated: true,
|
||||
|
||||
setUser: (user) => set({ user, isAuthenticated: true }),
|
||||
clearUser: () => set({ user: null, isAuthenticated: false }),
|
||||
}));
|
||||
|
||||
// ─── Selectors ───────────────────────────────────────────────────────────────
|
||||
|
||||
export const selectUser = (state: UserStore) => state.user;
|
||||
export const selectFullName = (state: UserStore): string =>
|
||||
state.user ? `${state.user.firstName} ${state.user.lastName}` : '';
|
||||
50
src/widgets/plagiatCheck/lib/validation.ts
Normal file
50
src/widgets/plagiatCheck/lib/validation.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// ─── Constants ───────────────────────────────────────────────────────────────
|
||||
|
||||
import { PlagiarismFormErrors, PlagiarismFormState } from './types';
|
||||
|
||||
const MAX_FILE_SIZE_MB = 20;
|
||||
const MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024;
|
||||
|
||||
const ALLOWED_MIME_TYPES = [
|
||||
'application/pdf',
|
||||
'application/msword',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'text/plain',
|
||||
];
|
||||
|
||||
const ALLOWED_EXTENSIONS = '.pdf, .doc, .docx, .txt';
|
||||
|
||||
// ─── Validator ───────────────────────────────────────────────────────────────
|
||||
|
||||
export function validatePlagiarismForm(
|
||||
state: PlagiarismFormState,
|
||||
): PlagiarismFormErrors {
|
||||
const errors: PlagiarismFormErrors = {};
|
||||
|
||||
// Topic validation
|
||||
const trimmedTopic = state.title.trim();
|
||||
if (!trimmedTopic) {
|
||||
errors.title = 'Title is required.';
|
||||
} else if (trimmedTopic.length < 3) {
|
||||
errors.title = 'Title must be at least 3 characters.';
|
||||
} else if (trimmedTopic.length > 200) {
|
||||
errors.title = 'Title must not exceed 200 characters.';
|
||||
}
|
||||
|
||||
// File validation
|
||||
if (!state.file) {
|
||||
errors.file = 'Please upload a document.';
|
||||
} else {
|
||||
if (state.file.size > MAX_FILE_SIZE_BYTES) {
|
||||
errors.file = `File size must not exceed ${MAX_FILE_SIZE_MB} MB.`;
|
||||
} else if (!ALLOWED_MIME_TYPES.includes(state.file.type)) {
|
||||
errors.file = `Unsupported file type. Allowed: ${ALLOWED_EXTENSIONS}.`;
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
export function isFormValid(errors: PlagiarismFormErrors): boolean {
|
||||
return Object.keys(errors).length === 0;
|
||||
}
|
||||
Reference in New Issue
Block a user