diff --git a/src/pages/payment/PayInvoice.tsx b/src/pages/payment/PayInvoice.tsx index 9760c82..9a6552e 100644 --- a/src/pages/payment/PayInvoice.tsx +++ b/src/pages/payment/PayInvoice.tsx @@ -1,7 +1,9 @@ import { type FC, useState, useMemo, useEffect } from 'react' import { useFormik } from 'formik' import * as Yup from 'yup' -import { useParams } from 'react-router-dom' +import { Link, useParams } from 'react-router-dom' +import { useQueryClient } from '@tanstack/react-query' +import { Paths } from '@/config/Paths' import Input from '@/components/Input' import Select from '@/components/Select' import type { ItemsSelectType } from '@/components/Select' @@ -39,10 +41,12 @@ const PayInvoice: FC = () => { const { data: user } = useGetMe() const { id } = useParams() + const queryClient = useQueryClient() const { data, isPending } = useGetInvoiceDetail(id ?? '') const { mutate: payInvoice, isPending: isPaying } = usePayInvoice() const multiUpload = useMultiUpload() const [attachmentFiles, setAttachmentFiles] = useState([]) + const [isPaymentDone, setIsPaymentDone] = useState(false) const userCredit = user?.maxCredit ?? 0 const methodItems = useMemo(() => getMethodItems(userCredit), [userCredit]) @@ -103,13 +107,25 @@ const PayInvoice: FC = () => { }, }, { - onSuccess: (res) => { + onSuccess: (res, variables) => { const paymentUrl = res?.data?.paymentUrl if (paymentUrl) { toast('در حال انتقال به درگاه پرداخت', 'success') window.location.href = paymentUrl return } + + const method = variables.params.method + if ( + method === PaymentMethodEnum.Cash || + method === PaymentMethodEnum.Credit + ) { + queryClient.invalidateQueries({ queryKey: ['invoice', id] }) + queryClient.invalidateQueries({ queryKey: ['payment', id] }) + setIsPaymentDone(true) + return + } + toast('پرداخت با موفقیت ثبت شد', 'success') }, onError: (error: ErrorType) => { @@ -139,6 +155,27 @@ const PayInvoice: FC = () => { ) } + if (isPaymentDone) { + return ( +
+
+ +
+
پرداخت انجام شد
+

+ پرداخت شما با موفقیت ثبت شد. +

+
+ + + +
+
+ ) + } + return (
پرداخت صورت حساب
diff --git a/src/pages/request/NewRequest.tsx b/src/pages/request/NewRequest.tsx index 564d35a..92958d8 100644 --- a/src/pages/request/NewRequest.tsx +++ b/src/pages/request/NewRequest.tsx @@ -1,64 +1,115 @@ -import { useState, type FC } from 'react' +import { useEffect, useState, type FC } from 'react' import Request from './components/Request' -import Button from '@/components/Button' -import { TickSquare } from 'iconsax-react' +import RequestItemsList from './components/RequestItemsList' import { useRequestStore } from './store/RequestStore' import { useSubmitRequest } from './hooks/useRequestData' import { toast } from '@/shared/toast' -import { useNavigate } from 'react-router-dom' +import { Link, useNavigate } from 'react-router-dom' import { Paths } from '@/config/Paths' import { extractErrorMessage } from '@/config/func' +import { ArrowRight2 } from 'iconsax-react' const NewRequest: FC = () => { - const navigate = useNavigate() const { mutate: submitRequest, isPending } = useSubmitRequest() - const [showRequests, setShowRequests] = useState([0]) const { items, setItems } = useRequestStore() + const [editingIndex, setEditingIndex] = useState(null) + const [formKey, setFormKey] = useState(0) - const onSubmit = () => { - if (items.length > 0) { - submitRequest(items, { - onSuccess: () => { - setItems([]) - setShowRequests([0]) - toast('سفارش با موفقیت ثبت شد', 'success') - navigate(Paths.myRequests, { state: { refresh: true } }) - }, - onError: (error) => { - toast(extractErrorMessage(error), 'error') - } - }) - } else { - toast('حداقل یک سفارش باید اضافه کنید.', 'error') + useEffect(() => { + setItems([]) + return () => setItems([]) + }, [setItems]) + + const handleSaved = () => { + const wasEditing = editingIndex !== null + setEditingIndex(null) + setFormKey((k) => k + 1) + toast(wasEditing ? 'قلم ویرایش شد' : 'قلم به لیست اضافه شد', 'success') + } + + const handleRemove = (index: number) => { + const next = items.filter((_, i) => i !== index) + setItems(next) + + if (editingIndex === index) { + setEditingIndex(null) + setFormKey((k) => k + 1) + } else if (editingIndex !== null && editingIndex > index) { + setEditingIndex(editingIndex - 1) } } + const handleEdit = (index: number) => { + setEditingIndex(index) + } + + const onSubmit = () => { + if (items.length === 0) { + toast('حداقل یک قلم باید به درخواست اضافه کنید.', 'error') + return + } + + submitRequest(items, { + onSuccess: () => { + setItems([]) + setEditingIndex(null) + setFormKey((k) => k + 1) + toast('سفارش با موفقیت ثبت شد', 'success') + navigate(Paths.myRequests, { state: { refresh: true } }) + }, + onError: (error) => { + toast(extractErrorMessage(error), 'error') + }, + }) + } return ( -
-
-

- درخواست جدید -

- + + بازگشت به لیست +
- {showRequests.map((item) => { - return ( - setShowRequests([...showRequests, showRequests.length])} key={item} /> - ) - })} +

+ برای هر محصول یک قلم اضافه کنید، سپس درخواست را ثبت نهایی کنید. +

+
+
+ { + setEditingIndex(null) + setFormKey((k) => k + 1) + } + : undefined + } + /> +
+ + +
) } diff --git a/src/pages/request/components/ManageAttribute.tsx b/src/pages/request/components/ManageAttribute.tsx index b046154..bff843f 100644 --- a/src/pages/request/components/ManageAttribute.tsx +++ b/src/pages/request/components/ManageAttribute.tsx @@ -19,16 +19,20 @@ const ManageAttribute: FC = (props) => { const { attributes, formik } = props + const getAttributeValue = (fieldId: string) => { + const attr = formik.values.attributes.find((o) => o.fieldId === fieldId) + return attr?.value != null ? String(attr.value) : '' + } const handleChange = (attributeId: string, value: string) => { - const attribute = formik.values.attributes - const index: number = attribute.findIndex(o => o.fieldId === attributeId) + const next = [...formik.values.attributes] + const index = next.findIndex((o) => o.fieldId === attributeId) if (index > -1) { - attribute[index].value = value + next[index] = { ...next[index], value } } else { - attribute.push({ fieldId: attributeId, value: value }) + next.push({ fieldId: attributeId, value }) } - formik.setFieldValue('attributes', attribute) + formik.setFieldValue('attributes', next) } @@ -44,6 +48,7 @@ const ManageAttribute: FC = (props) => { handleChange(item.id, e.target.value)} />
@@ -121,6 +129,7 @@ const ManageAttribute: FC = (props) => {