import { type FC, useMemo } from 'react' import { useFormik } from 'formik' import * as Yup from 'yup' import { TickCircle } from 'iconsax-react' import { toast } from 'react-toastify' import { useNavigate, useParams } from 'react-router-dom' import { useTranslation } from 'react-i18next' import Button from '@/components/Button' import Input from '@/components/Input' import Select from '@/components/Select' import Switch from '@/components/Switch' import Textarea from '@/components/Textarea' import type { CreateRestaurantPaymentMethodType } from './types/Types' import { useGetRestaurantPaymentMethod, useUpdateRestaurantPaymentMethod } from './hooks/usePaymentMethodData' import { PaymentMethodEnum, PaymentGatewayEnum } from './enum/Enum' import { Pages } from '@/config/Pages' import type { ErrorType } from '@/helpers/types' import { extractErrorMessage } from '@/config/func' const UpdatePaymentMethod: FC = () => { const { t } = useTranslation('global') const navigate = useNavigate() const { id } = useParams<{ id: string }>() const { mutate: updateRestaurantPaymentMethod, isPending } = useUpdateRestaurantPaymentMethod() const { data: paymentMethodData, isLoading } = useGetRestaurantPaymentMethod(id || '') const paymentMethods = [ { label: t(`payment.${PaymentMethodEnum.Online}`), value: PaymentMethodEnum.Online }, { label: t(`payment.${PaymentMethodEnum.Cash}`), value: PaymentMethodEnum.Cash }, { label: t(`payment.${PaymentMethodEnum.CardOnDelivery}`), value: PaymentMethodEnum.CardOnDelivery }, { label: t(`payment.${PaymentMethodEnum.Wallet}`), value: PaymentMethodEnum.Wallet }, ] const paymentGateways = [ { label: t(`payment.${PaymentGatewayEnum.ZarinPal}`), value: PaymentGatewayEnum.ZarinPal }, ] const initialValues = useMemo(() => { if (paymentMethodData?.data) { const data = paymentMethodData.data as CreateRestaurantPaymentMethodType & { id: string } return { method: data.method || '', gateway: data.gateway || '', description: data.description || '', enabled: data.enabled ?? true, order: data.order || 0, merchantId: data.merchantId || '', } } return { method: '', gateway: '', description: '', enabled: true, order: 0, merchantId: '', } }, [paymentMethodData]) const formik = useFormik({ initialValues, enableReinitialize: true, validationSchema: Yup.object().shape({ method: Yup.string().required('روش پرداخت الزامی است'), gateway: Yup.string().when('method', { is: PaymentMethodEnum.Online, then: (schema) => schema.required('درگاه پرداخت الزامی است'), otherwise: (schema) => schema.notRequired(), }), description: Yup.string().required('توضیحات الزامی است'), enabled: Yup.boolean(), order: Yup.number().required('ترتیب الزامی است').min(0, 'ترتیب باید مثبت باشد'), merchantId: Yup.string().when('method', { is: PaymentMethodEnum.Online, then: (schema) => schema.required('شناسه مرچنت الزامی است'), otherwise: (schema) => schema.notRequired(), }), }), onSubmit: (values) => { if (!id) return const submitValues: CreateRestaurantPaymentMethodType = { method: values.method, description: values.description, enabled: values.enabled, order: values.order, ...(values.method === PaymentMethodEnum.Online ? { gateway: values.gateway, merchantId: values.merchantId, } : {}), } updateRestaurantPaymentMethod( { id, params: submitValues }, { onSuccess: () => { toast.success('روش پرداخت با موفقیت ویرایش شد') navigate(Pages.payment_methods.list) }, onError: (error: ErrorType) => { toast.error(extractErrorMessage(error)) }, } ) }, }) if (isLoading) { return (
در حال بارگذاری...
) } if (!paymentMethodData?.data) { return (
روش پرداخت یافت نشد
) } return (
ویرایش روش پرداخت
)}