reason return
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import Button from '@/components/Button'
|
||||
import DefaulModal from '@/components/DefaulModal'
|
||||
import Input from '@/components/Input'
|
||||
import Select from '@/components/Select'
|
||||
import Switch from '@/components/Switch'
|
||||
import { useState, type FC } from 'react'
|
||||
import type { CreateReturnReasonType } from '../types/Types'
|
||||
import { useCreateReturnReason } from '../hooks/useOrderData'
|
||||
import { useFormik } from 'formik'
|
||||
import * as Yup from 'yup'
|
||||
import { useGetFines } from '@/pages/payment/hooks/usePaymentData'
|
||||
|
||||
const ModalCreateReturnReason: FC = () => {
|
||||
|
||||
const [open, setOpen] = useState<boolean>(false)
|
||||
const createReturnReasonMutation = useCreateReturnReason()
|
||||
const { data: finesData } = useGetFines()
|
||||
|
||||
const formik = useFormik<CreateReturnReasonType>({
|
||||
initialValues: {
|
||||
fineRule: '',
|
||||
title: '',
|
||||
is_mandatory_picture: false,
|
||||
},
|
||||
validationSchema: Yup.object({
|
||||
fineRule: Yup.string().required('انتخاب قانون جریمه الزامی است'),
|
||||
title: Yup.string().required('عنوان دلیل بازگشت الزامی است'),
|
||||
is_mandatory_picture: Yup.boolean(),
|
||||
}),
|
||||
onSubmit: async (values) => {
|
||||
try {
|
||||
await createReturnReasonMutation.mutateAsync(values)
|
||||
setOpen(false)
|
||||
formik.resetForm()
|
||||
} catch (error) {
|
||||
console.error('خطا در ایجاد دلیل بازگشت:', error)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Button
|
||||
label='افزودن دلیل بازگشت'
|
||||
onClick={() => setOpen(true)}
|
||||
className='w-fit px-5 whitespace-nowrap'
|
||||
/>
|
||||
<DefaulModal
|
||||
open={open}
|
||||
close={() => setOpen(false)}
|
||||
title_header='افزودن دلیل بازگشت'
|
||||
isHeader
|
||||
>
|
||||
<form onSubmit={formik.handleSubmit} className='space-y-4 w-[400px] mt-6'>
|
||||
<Input
|
||||
label='عنوان دلیل بازگشت'
|
||||
placeholder='عنوان دلیل بازگشت را وارد کنید'
|
||||
name='title'
|
||||
value={formik.values.title}
|
||||
onChange={formik.handleChange}
|
||||
onBlur={formik.handleBlur}
|
||||
error_text={formik.touched.title && formik.errors.title ? formik.errors.title : undefined}
|
||||
/>
|
||||
|
||||
<Select
|
||||
label='قانون جریمه'
|
||||
placeholder='قانون جریمه را انتخاب کنید'
|
||||
name='fineRule'
|
||||
value={formik.values.fineRule}
|
||||
onChange={formik.handleChange}
|
||||
onBlur={formik.handleBlur}
|
||||
error_text={formik.touched.fineRule && formik.errors.fineRule ? formik.errors.fineRule : undefined}
|
||||
items={finesData?.results?.fineRules?.map(fine => ({
|
||||
value: fine._id,
|
||||
label: `${fine.title} (${fine.fine_percentage}%)`
|
||||
})) || []}
|
||||
/>
|
||||
|
||||
<Switch
|
||||
label='تصویر اجباری'
|
||||
active={formik.values.is_mandatory_picture}
|
||||
onChange={(checked) => formik.setFieldValue('is_mandatory_picture', checked)}
|
||||
/>
|
||||
|
||||
<div className='flex gap-2 pt-4'>
|
||||
<Button
|
||||
type='submit'
|
||||
label={createReturnReasonMutation.isPending ? 'در حال ذخیره...' : 'ذخیره'}
|
||||
disabled={createReturnReasonMutation.isPending}
|
||||
className='flex-1'
|
||||
/>
|
||||
<Button
|
||||
type='button'
|
||||
label='لغو'
|
||||
variant='outline'
|
||||
onClick={() => setOpen(false)}
|
||||
className='flex-1'
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
</DefaulModal>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ModalCreateReturnReason
|
||||
Reference in New Issue
Block a user