last push
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import React, { useState, useEffect, useRef } from 'react';
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { AxiosError } from 'axios';
|
||||
import { apiRequest } from '@/shared/request/apiRequest';
|
||||
import { links } from '@/shared/request/links';
|
||||
import { CertificateFormData } from './types';
|
||||
import { SIPaymentResponse } from '../siModal/utils/useFileUpload';
|
||||
|
||||
interface UseCertificateModalProps {
|
||||
document_id: number;
|
||||
@@ -24,13 +26,21 @@ export function useCertificateModal({
|
||||
const [form, setForm] = useState<CertificateFormData>({
|
||||
fullname: '',
|
||||
document_theme: '',
|
||||
document_type: '',
|
||||
type: 0,
|
||||
document_id,
|
||||
});
|
||||
const [success, setSuccess] = useState(false);
|
||||
const [visible, setVisible] = useState(false);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const payment = useMutation({
|
||||
mutationFn: (id: number) =>
|
||||
apiRequest<SIPaymentResponse>('POST', links.demo_pay(id)),
|
||||
onSuccess: (res) => {
|
||||
window.open(res?.data?.payment_link, '_self');
|
||||
},
|
||||
});
|
||||
|
||||
const certificateMutation = useMutation({
|
||||
mutationFn: (payload: CertificatePayload) =>
|
||||
apiRequest('POST', links.sertifikat(document_id), payload).then(
|
||||
@@ -53,13 +63,18 @@ export function useCertificateModal({
|
||||
resetForm();
|
||||
}, 1500);
|
||||
},
|
||||
onError: (error: AxiosError<{ code?: string }>) => {
|
||||
if (error?.response?.data?.code === 'not_paid') {
|
||||
payment.mutate(document_id);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const resetForm = () => {
|
||||
setForm({
|
||||
fullname: '',
|
||||
document_theme: '',
|
||||
document_type: '',
|
||||
type: 0,
|
||||
document_id,
|
||||
});
|
||||
};
|
||||
@@ -90,16 +105,14 @@ export function useCertificateModal({
|
||||
) => setForm((prev) => ({ ...prev, [field]: value }));
|
||||
|
||||
const isFormValid =
|
||||
!!form.fullname.trim() &&
|
||||
!!form.document_theme.trim() &&
|
||||
!!form.document_type;
|
||||
!!form.fullname.trim() && !!form.document_theme.trim() && !!form.type;
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (!isFormValid || certificateMutation.isPending) return;
|
||||
certificateMutation.mutate({
|
||||
full_name: form.fullname,
|
||||
file_name: form.document_theme,
|
||||
document_type: Number(form.document_type),
|
||||
document_type: Number(form.type),
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user