diff --git a/.env b/.env index 7fee5fb..faf8920 100644 --- a/.env +++ b/.env @@ -1,4 +1,5 @@ # VITE_API_URL=https://dpage-api.danakcorp.com VITE_API_URL=http://192.168.99.131:4000 VITE_TOKEN_NAME=dpage-editor-t -VITE_REFRESH_TOKEN_NAME=dpage-editor-refresh-t \ No newline at end of file +VITE_REFRESH_TOKEN_NAME=dpage-editor-refresh-t +VITE_INVOICE_URL=https://console.danakcorp.com/receipts/ \ No newline at end of file diff --git a/src/pages/designer/Request.tsx b/src/pages/designer/Request.tsx index d4cfd5e..08bd0bc 100644 --- a/src/pages/designer/Request.tsx +++ b/src/pages/designer/Request.tsx @@ -8,6 +8,7 @@ import UploadBoxDraggble from "@/components/UploadBoxDraggble"; import { extractErrorMessage } from "@/helpers/utils"; import { usePageTitle } from "@/hooks/usePageTitle"; import { useInitiateDesignRequest } from "@/pages/designer/hooks/useDesignerData"; +import type { DesignerRequestFormValues } from "@/pages/designer/types/Types"; import { useMultipleUpload } from "@/pages/uploader/hooks/useUploaderData"; import { Form, Formik, type FormikHelpers, type FormikProps } from "formik"; import { type FC } from "react"; @@ -15,14 +16,6 @@ import * as Yup from "yup"; const PRICE_PER_PAGE = 1000; -export type DesignerRequestFormValues = { - title: string; - count: number | ""; - desc: string; - expectedDate: string; - attachments: File[]; -}; - const initialValues: DesignerRequestFormValues = { title: "", count: "", @@ -44,16 +37,7 @@ const validationSchema = Yup.object({ type DesignerRequestFormFieldsProps = FormikProps; -const DesignerRequestFormFields: FC = ({ - values, - errors, - touched, - isSubmitting, - setFieldValue, - setFieldTouched, - handleChange, - handleBlur, -}) => { +const DesignerRequestFormFields: FC = ({ values, errors, touched, isSubmitting, setFieldValue, setFieldTouched, handleChange, handleBlur }) => { const totalPrice = (values.count === "" ? 0 : values.count) * PRICE_PER_PAGE; const handleAttachmentChange = (files: File[]) => { @@ -123,33 +107,37 @@ const DesignerRequest: FC = () => { const { mutateAsync: uploadFiles } = useMultipleUpload(); const { mutateAsync: initiateDesignRequest } = useInitiateDesignRequest(); - const handleSubmit = async ( - values: DesignerRequestFormValues, - { setSubmitting, resetForm }: FormikHelpers, - ) => { - try { - let attachmentUrls: string[] = []; + const handleSubmit = async (values: DesignerRequestFormValues, { setSubmitting, resetForm }: FormikHelpers) => { + let attachmentUrls: string[] = []; - if (values.attachments.length > 0) { - const uploadResponse = await uploadFiles(values.attachments); - attachmentUrls = uploadResponse.data.map((item) => item.url); - } - - await initiateDesignRequest({ - title: values.title, - ...(values.count !== "" ? { count: values.count } : {}), - desc: values.desc, - expectedDate: values.expectedDate, - attachments: attachmentUrls, - }); - - toast("درخواست با موفقیت ثبت شد", "success"); - resetForm(); - } catch (error) { - toast(extractErrorMessage(error), "error"); - } finally { - setSubmitting(false); + if (values.attachments.length > 0) { + const uploadResponse = await uploadFiles(values.attachments); + attachmentUrls = uploadResponse.data.map((item) => item.url); } + + const { attachments, count, ...rest } = values; + + await initiateDesignRequest( + { + ...rest, + count: count === "" ? undefined : count, + attachments: attachmentUrls, + }, + { + onSuccess: (data) => { + toast("صورت حساب با موفقیت ثبت شد", "success"); + window.open(`${import.meta.env.VITE_INVOICE_URL}${data?.data?.invoiceId}`, "_blank"); + setSubmitting(false); + resetForm(); + }, + onError: (error) => { + toast(extractErrorMessage(error), "error"); + }, + }, + ); + + toast("درخواست با موفقیت ثبت شد", "success"); + resetForm(); }; return ( diff --git a/src/pages/designer/service/DesignerService.ts b/src/pages/designer/service/DesignerService.ts index a73aeb5..989ecb2 100644 --- a/src/pages/designer/service/DesignerService.ts +++ b/src/pages/designer/service/DesignerService.ts @@ -1,9 +1,12 @@ import axios from "@/config/axios"; -import type { DesignRequestInitiateParams } from "../types/Types"; +import type { + DesignRequestInitiateParams, + RequestDesignResponse, +} from "../types/Types"; export const initiateDesignRequest = async ( params: DesignRequestInitiateParams, -) => { +): Promise => { const { data } = await axios.post( "/admin/design-request/initiate", params, diff --git a/src/pages/designer/types/Types.ts b/src/pages/designer/types/Types.ts index 3395647..04b46a3 100644 --- a/src/pages/designer/types/Types.ts +++ b/src/pages/designer/types/Types.ts @@ -1,7 +1,49 @@ -export type DesignRequestInitiateParams = { +import type { BaseResponse } from "@/shared/types/SharedTypes"; + +export type DesignerRequestFormValues = { title: string; + count: number | ""; + desc: string; + expectedDate: string; + attachments: File[]; +}; + +export type DesignRequestInitiateParams = Omit< + DesignerRequestFormValues, + "attachments" | "count" +> & { count?: number; + attachments: string[]; +}; + +export type DesignRequestBusinessType = { + id: string; + createdAt: string; + updatedAt: string; + deletedAt: string | null; + danakSubscriptionId: string; + name: string; + slug: string; + domain: string | null; + isDomainVerified: boolean; + domainVerificationToken: string | null; + domainVerifiedAt: string | null; + logoUrl: string | null; + maxCataloguesCount: number; + admin: string; +}; + +export type DesignRequestType = { + id: string; + createdAt: string; + updatedAt: string; + title: string; + count: number; desc: string; expectedDate: string; attachments: string[]; + invoiceId: string; + business: DesignRequestBusinessType; }; + +export type RequestDesignResponse = BaseResponse;