Create order in admin
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import { type FC } from 'react'
|
||||
import type { CreateOrderType } from '../types/Types'
|
||||
import { clx } from '@/helpers/utils'
|
||||
import Select from '@/components/Select'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import RadioGroup from '@/components/RadioGroup'
|
||||
import DatePickerComponent from '@/components/DatePicker'
|
||||
import Input from '@/components/Input'
|
||||
import Textarea from '@/components/Textarea'
|
||||
import type { FormikProps } from 'formik'
|
||||
import { FieldTypeEnum } from '@/pages/product/enum/Enum'
|
||||
import type { FieldType } from '@/pages/product/types/Types'
|
||||
|
||||
type Props = {
|
||||
attributes?: FieldType[],
|
||||
formik: FormikProps<CreateOrderType>
|
||||
}
|
||||
|
||||
const ManageAttribute: FC<Props> = (props) => {
|
||||
|
||||
const { attributes, formik } = props
|
||||
|
||||
const handleChange = (attributeId: number, value: string) => {
|
||||
const attribute = formik.values.attributes
|
||||
const index: number = attribute.findIndex(o => o.attributeId === attributeId)
|
||||
if (index > -1) {
|
||||
attribute[index].value = value
|
||||
} else {
|
||||
attribute.push({ attributeId: attributeId, value: value })
|
||||
}
|
||||
formik.setFieldValue('attributes', attribute)
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div className={clx(
|
||||
!attributes && 'hidden'
|
||||
)}>
|
||||
{
|
||||
attributes?.map((item) => {
|
||||
if (item?.type === FieldTypeEnum.select)
|
||||
return (
|
||||
<div className='mt-6' key={item.id}>
|
||||
<Select
|
||||
placeholder={`انتخاب ${item.isRequired ? '(اجباری)' : '(اختاری)'}`}
|
||||
label={item.name}
|
||||
items={item.options?.map((value) => {
|
||||
return {
|
||||
label: value.value,
|
||||
value: value.value
|
||||
}
|
||||
})}
|
||||
onChange={(e) => handleChange(item.id, e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
else if (item.type === FieldTypeEnum.checkbox)
|
||||
return (
|
||||
<div key={item.id} className='mt-6'>
|
||||
<div className='text-sm mb-3'>{item.name}</div>
|
||||
<div className='flex gap-5 flex-wrap'>
|
||||
{
|
||||
item.options?.map((value) => {
|
||||
const object = formik.values.attributes.find(o => o.attributeId === item.id)
|
||||
return (
|
||||
<div key={value.id} className='flex gap-2 items-center'>
|
||||
<div>{value.value}</div>
|
||||
<Checkbox
|
||||
name={item.id + ''}
|
||||
checked={object?.value === value.value}
|
||||
onCheckedChange={(checked) => handleChange(item.id, checked ? value.value : '')}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
})
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
else if (item.type === FieldTypeEnum.radio) {
|
||||
const object = formik.values.attributes.find(o => o.attributeId === item.id)
|
||||
return (
|
||||
<div key={item.id} className='mt-6'>
|
||||
<div className='text-sm mb-3'>{item.name}</div>
|
||||
<RadioGroup
|
||||
items={item.options?.map((value) => {
|
||||
return {
|
||||
label: value.value,
|
||||
value: value.value
|
||||
}
|
||||
})}
|
||||
onChange={(v) => handleChange(item.id, v)}
|
||||
selected={object?.value + '' || ''}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
else if (item.type === FieldTypeEnum.date)
|
||||
return (
|
||||
<div key={item.id} className='mt-6'>
|
||||
<DatePickerComponent
|
||||
onChange={(date) => handleChange(item.id, date)}
|
||||
placeholder=''
|
||||
label={item.name}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
else if (item.type === FieldTypeEnum.number || item.type === FieldTypeEnum.text)
|
||||
return (
|
||||
<div key={item.id} className='mt-6'>
|
||||
<Input
|
||||
label={item.name}
|
||||
type={item.type}
|
||||
onChange={(e) => handleChange(item.id, e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
else if (item.type === FieldTypeEnum.textarea)
|
||||
return (
|
||||
<div key={item.id} className='mt-6'>
|
||||
<Textarea
|
||||
label={item.name}
|
||||
onChange={(e) => handleChange(item.id, e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
|
||||
})
|
||||
}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ManageAttribute
|
||||
@@ -0,0 +1,208 @@
|
||||
import { useState, type ChangeEvent, type FC } from 'react'
|
||||
import Button from '@/components/Button'
|
||||
import Input from '@/components/Input'
|
||||
import Select from '@/components/Select'
|
||||
import UploadBox from '@/components/UploadBox'
|
||||
import VoiceRecorder from '@/components/VoiceRecorder'
|
||||
import { COLORS } from '@/constants/colors'
|
||||
import { AddSquare, Edit } from 'iconsax-react'
|
||||
import { useFormik } from 'formik'
|
||||
import * as Yup from 'yup'
|
||||
import type { AttachmentsType, CreateOrderType } from '../types/Types'
|
||||
import { useGetAttributes, useGetProducts } from '../hooks/useOrderData'
|
||||
import ManageAttribute from './ManageAttribute'
|
||||
import { useMultiUpload, useSingleUpload } from '@/pages/uploader/hooks/useUploader'
|
||||
import { useOrderStore } from '../store/OrderStore'
|
||||
import { clx } from '@/helpers/utils'
|
||||
import ProductsSelect from './ProductsSelect'
|
||||
import type { ProductType } from '@/pages/product/types/Types'
|
||||
|
||||
type Props = {
|
||||
addNewItem: () => void,
|
||||
}
|
||||
|
||||
const Order: FC<Props> = ({ addNewItem }) => {
|
||||
|
||||
const { data } = useGetProducts()
|
||||
const { setItems, items } = useOrderStore()
|
||||
const [indexOrder, setIndexOrder] = useState<number>(-1)
|
||||
const [isEditMode, setIsEditMode] = useState<boolean>(false)
|
||||
const [productSelected, setProductSelected] = useState<ProductType>()
|
||||
const [voiceFile, setVoiceFile] = useState<File>()
|
||||
const [files, setFiles] = useState<File[]>([])
|
||||
const { data: attributes } = useGetAttributes(productSelected?.id)
|
||||
const singleUpload = useSingleUpload()
|
||||
const multiUpload = useMultiUpload()
|
||||
|
||||
const formik = useFormik<CreateOrderType>({
|
||||
initialValues: {
|
||||
productId: undefined,
|
||||
attachments: [],
|
||||
quantity: undefined,
|
||||
description: '',
|
||||
attributes: [],
|
||||
discount: undefined,
|
||||
unitPrice: undefined,
|
||||
},
|
||||
validationSchema: Yup.object({
|
||||
productId: Yup.number().required('این فیلد اجباری می باشد'),
|
||||
quantity: Yup.number().required('این فیلد اجباری می باشد'),
|
||||
unitPrice: Yup.number().required('این فیلد اجباری می باشد'),
|
||||
}),
|
||||
onSubmit: async (values) => {
|
||||
const attachments: AttachmentsType[] = []
|
||||
if (files.length) {
|
||||
await multiUpload.mutateAsync(files, {
|
||||
onSuccess: (data) => {
|
||||
data?.data?.map((item) => {
|
||||
attachments.push({
|
||||
type: 'uploads_attach',
|
||||
url: item.url
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
if (voiceFile) {
|
||||
await singleUpload.mutateAsync(voiceFile, {
|
||||
onSuccess: (data) => {
|
||||
attachments.push({
|
||||
type: 'voice',
|
||||
url: data?.data?.url
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
if (indexOrder === -1) {
|
||||
values.attachments = attachments
|
||||
setIndexOrder(items.length)
|
||||
setItems([...items, values])
|
||||
addNewItem()
|
||||
setIsEditMode(true)
|
||||
} else {
|
||||
if (values.attachments.length) {
|
||||
values.attachments = [...values.attachments, ...attachments]
|
||||
|
||||
}
|
||||
const items_edit = [...items]
|
||||
items_edit[indexOrder] = values
|
||||
setItems(items_edit)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const handleChange = (e: ChangeEvent<HTMLSelectElement>) => {
|
||||
const productId = e.target.value
|
||||
const product = data?.data?.find(o => Number(o.id) === Number(productId))
|
||||
if (product) {
|
||||
if (product.quantities[0] !== 0) {
|
||||
product.quantities.unshift(0)
|
||||
}
|
||||
setProductSelected(product)
|
||||
formik.setFieldValue('productId', productId)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div className='bg-white rounded-3xl p-6 mt-8'>
|
||||
<div className='font-light'>اطلاعات سفارش</div>
|
||||
|
||||
<div className='mt-6 rowTwoInput'>
|
||||
<ProductsSelect
|
||||
onChange={handleChange}
|
||||
error_text={formik.touched.productId && formik.errors.productId ? formik.errors.productId : ''}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<ManageAttribute
|
||||
attributes={attributes?.data}
|
||||
formik={formik}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='mt-6 rowTwoInput'>
|
||||
<Select
|
||||
items={productSelected?.quantities?.map((item) => {
|
||||
return {
|
||||
label: item === 0 ? 'انتخاب دلخواه' : item + '',
|
||||
value: item + ''
|
||||
}
|
||||
}) || []}
|
||||
label='تعداد'
|
||||
placeholder='انتخاب'
|
||||
{...formik.getFieldProps('quantity')}
|
||||
error_text={formik.touched.quantity && formik.errors.quantity ? formik.errors.quantity : ''}
|
||||
/>
|
||||
|
||||
<Input
|
||||
label='تعداد دلخواه'
|
||||
readOnly={Number(formik.values.quantity) !== 0}
|
||||
{...formik.getFieldProps('quantity')}
|
||||
type='number'
|
||||
error_text={formik.touched.quantity && formik.errors.quantity ? formik.errors.quantity : ''}
|
||||
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='rowTwoInput mt-6'>
|
||||
<Input
|
||||
{...formik.getFieldProps('unitPrice')}
|
||||
label='مبلغ واحد'
|
||||
error_text={formik.touched.unitPrice && formik.errors.unitPrice ? formik.errors.unitPrice : ''}
|
||||
/>
|
||||
|
||||
<Input
|
||||
{...formik.getFieldProps('discount')}
|
||||
label='مبلغ تخفیف به تومان'
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='mt-6'>
|
||||
<VoiceRecorder
|
||||
onChange={(value) => formik.setFieldValue('description', value)}
|
||||
label='پیام صوتی'
|
||||
onRecordingComplete={(blob) => {
|
||||
const file = new File([blob], "recording.wav", { type: blob.type });
|
||||
setVoiceFile(file);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
<div className='mt-6'>
|
||||
<UploadBox
|
||||
label='آپلود فایل'
|
||||
isMultiple
|
||||
onChange={setFiles}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='mt-6 flex justify-end'>
|
||||
<Button
|
||||
className={clx(
|
||||
'bg-transparent border border-primary text-primary w-fit px-6',
|
||||
isEditMode && 'bg-transparent border-[#3B82F6] text-[#3B82F6] '
|
||||
)}
|
||||
onClick={() => formik.handleSubmit()}
|
||||
isLoading={singleUpload.isPending || multiUpload.isPending}
|
||||
>
|
||||
<div className='flex gap-1'>
|
||||
{
|
||||
isEditMode ?
|
||||
<Edit color='#3B82F6' size={20} />
|
||||
:
|
||||
<AddSquare color={COLORS.primary} size={20} />
|
||||
}
|
||||
<div>
|
||||
{isEditMode ? 'ویرایش کردن' : 'اضافه کردن'}
|
||||
</div>
|
||||
</div>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Order
|
||||
@@ -0,0 +1,28 @@
|
||||
import { type FC, type SelectHTMLAttributes } from 'react'
|
||||
import { useGetProducts } from '../hooks/useOrderData'
|
||||
import Select from '@/components/Select'
|
||||
|
||||
type Props = {
|
||||
error_text?: string,
|
||||
} & SelectHTMLAttributes<HTMLSelectElement>
|
||||
|
||||
const ProductsSelect: FC<Props> = (props) => {
|
||||
|
||||
const { data } = useGetProducts()
|
||||
|
||||
return (
|
||||
<Select
|
||||
items={data?.data?.map((item) => {
|
||||
return {
|
||||
label: item.title,
|
||||
value: item.id + ''
|
||||
}
|
||||
}) || []}
|
||||
label='محصول'
|
||||
placeholder='انتخاب محصول'
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default ProductsSelect
|
||||
Reference in New Issue
Block a user