Requests
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import Tabs from '@/components/Tabs'
|
||||
import { useState, type FC } from 'react'
|
||||
import { TabMyRequestsEnum } from './enum/RequestEnum'
|
||||
import Filters from '@/components/Filters'
|
||||
import Button from '@/components/Button'
|
||||
import { AddSquare, Eye } from 'iconsax-react'
|
||||
import Table from '@/components/Table'
|
||||
import { useGetMyRequests } from './hooks/useRequestData'
|
||||
import moment from 'moment-jalaali'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { Paths } from '@/config/Paths'
|
||||
|
||||
const MyRequests: FC = () => {
|
||||
const [page, setPage] = useState<number>(1)
|
||||
const [activeTab, setActiveTab] = useState<TabMyRequestsEnum>(TabMyRequestsEnum.ALL)
|
||||
const { data } = useGetMyRequests(page, activeTab)
|
||||
|
||||
return (
|
||||
<div className='mt-5'>
|
||||
<h1>
|
||||
سفارشات من
|
||||
</h1>
|
||||
|
||||
<div className='mt-10'>
|
||||
<Tabs
|
||||
items={[
|
||||
{ label: 'همه', value: TabMyRequestsEnum.ALL },
|
||||
{ label: 'در حال انجام', value: TabMyRequestsEnum.IN_PROGRESS },
|
||||
{ label: 'تکمیل داده شده', value: TabMyRequestsEnum.DELIVERED },
|
||||
{ label: 'کنسل شده', value: TabMyRequestsEnum.CANCELLED },
|
||||
]}
|
||||
activeTab={activeTab}
|
||||
onTabChange={(tab) => setActiveTab(tab as TabMyRequestsEnum)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='mt-16 flex justify-between items-center'>
|
||||
<Filters
|
||||
fields={[
|
||||
{ type: 'input', name: 'search', placeholder: 'جستجو' },
|
||||
{ type: 'date', name: 'date', placeholder: 'تاریخ' },
|
||||
{
|
||||
type: 'select', name: 'status', placeholder: 'وضعیت', options: [
|
||||
{ label: 'همه', value: 'all' },
|
||||
{ label: 'در حال انجام', value: 'in_progress' },
|
||||
{ label: 'تایید شده', value: 'confirmed' },
|
||||
{ label: 'تحویل داده شده', value: 'delivered' },
|
||||
{ label: 'کنسل شده', value: 'cancelled' },
|
||||
]
|
||||
},
|
||||
]}
|
||||
onChange={() => { }}
|
||||
/>
|
||||
|
||||
<Link to={Paths.newRequest}>
|
||||
<Button
|
||||
className='w-fit px-5'
|
||||
>
|
||||
<div className='flex items-center gap-2'>
|
||||
<AddSquare
|
||||
size={16}
|
||||
color='#000000'
|
||||
/>
|
||||
<span>
|
||||
سفارش جدید
|
||||
</span>
|
||||
</div>
|
||||
</Button>
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
<div className='mt-10'>
|
||||
<Table
|
||||
columns={[
|
||||
{
|
||||
key: 'orderNumber',
|
||||
title: 'شماره',
|
||||
},
|
||||
{
|
||||
key: 'items',
|
||||
title: 'تعداد اقلام',
|
||||
render: (item) => {
|
||||
return (
|
||||
<div>{item.items.length}</div>
|
||||
)
|
||||
}
|
||||
},
|
||||
{
|
||||
key: 'createdAt',
|
||||
title: 'زمان ثبت سفارش',
|
||||
render: (item) => {
|
||||
return (
|
||||
<div>{moment(item.createdAt).format('jYYYY-jMM-jDD')}</div>
|
||||
)
|
||||
}
|
||||
},
|
||||
{
|
||||
key: 'designer',
|
||||
title: 'طراح',
|
||||
},
|
||||
{
|
||||
key: 'status',
|
||||
title: 'وضعیت سفارش',
|
||||
},
|
||||
{
|
||||
key: 'actions',
|
||||
title: '',
|
||||
render: (item) => {
|
||||
return (
|
||||
<Link to={Paths.requestDetails + item.id}>
|
||||
<Eye
|
||||
size={20}
|
||||
color='#000000'
|
||||
/>
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
}
|
||||
]}
|
||||
data={data?.data}
|
||||
pagination={{
|
||||
currentPage: page,
|
||||
onPageChange: setPage,
|
||||
totalPages: data?.meta?.totalPages || 1
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default MyRequests
|
||||
@@ -0,0 +1,64 @@
|
||||
import { useState, type FC } from 'react'
|
||||
import Request from './components/Request'
|
||||
import Button from '@/components/Button'
|
||||
import { TickSquare } from 'iconsax-react'
|
||||
import { useRequestStore } from './store/RequestStore'
|
||||
import { useSubmitRequest } from './hooks/useRequestData'
|
||||
import { toast } from '@/shared/toast'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { Paths } from '@/config/Paths'
|
||||
import { extractErrorMessage } from '@/config/func'
|
||||
|
||||
const NewRequest: FC = () => {
|
||||
|
||||
const navigate = useNavigate()
|
||||
const { mutate: submitRequest, isPending } = useSubmitRequest()
|
||||
const [showRequests, setShowRequests] = useState([0])
|
||||
const { items } = useRequestStore()
|
||||
|
||||
const onSubmit = () => {
|
||||
if (items.length > 0) {
|
||||
submitRequest(items, {
|
||||
onSuccess: () => {
|
||||
toast('سفارش با موفقیت ثبت شد', 'success')
|
||||
navigate(Paths.myRequests)
|
||||
},
|
||||
onError: (error) => {
|
||||
toast(extractErrorMessage(error), 'error')
|
||||
}
|
||||
})
|
||||
} else {
|
||||
toast('حداقل یک سفارش باید اضافه کنید.', 'error')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div className='mt-5'>
|
||||
<div className='justify-between items-center flex'>
|
||||
<h1 className='text-lg font-light'>
|
||||
سفارش جدید
|
||||
</h1>
|
||||
<Button
|
||||
className='w-fit px-5'
|
||||
onClick={onSubmit}
|
||||
isLoading={isPending}
|
||||
>
|
||||
<div className='flex gap-2 items-center'>
|
||||
<TickSquare size={20} color='black' />
|
||||
<div>ثبت نهایی سفارش</div>
|
||||
</div>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{showRequests.map((item) => {
|
||||
return (
|
||||
<Request addNewItem={() => setShowRequests([...showRequests, showRequests.length])} key={item} />
|
||||
)
|
||||
})}
|
||||
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default NewRequest
|
||||
@@ -0,0 +1,83 @@
|
||||
import { type FC } from 'react'
|
||||
import { Receipt1 } from 'iconsax-react'
|
||||
import { useParams } from 'react-router-dom'
|
||||
import { useGetRequestDetails } from './hooks/useRequestData'
|
||||
import TicketSection from './components/TicketSection'
|
||||
|
||||
const RequestDetail: FC = () => {
|
||||
|
||||
const { id } = useParams()
|
||||
const { data } = useGetRequestDetails(id!)
|
||||
|
||||
return (
|
||||
<div className="w-full min-h-screen bg-[#eceef6] p-6">
|
||||
{/* Header Section */}
|
||||
<div className="flex items-start justify-between mb-6">
|
||||
<div className="text-sm text-[#8C90A3]">
|
||||
سفارش #{data?.data?.orderNumber}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Order Information Section */}
|
||||
<div className="bg-white rounded-2xl p-6 mb-6">
|
||||
<div className='flex justify-between items-center'>
|
||||
<h2 className="text-lg font-light mb-6">اطلاعات سفارش</h2>
|
||||
|
||||
<a href="#" className="flex items-center gap-2 text-[#0037FF] text-sm">
|
||||
<Receipt1 size={20} color="#3B82F6" />
|
||||
<span>پیش فاکتور</span>
|
||||
</a>
|
||||
</div>
|
||||
<div className='flex flex-col gap-10'>
|
||||
{
|
||||
data?.data?.items?.map((item) => {
|
||||
return (
|
||||
<div key={item.product.id}>
|
||||
<div className="flex items-center gap-6">
|
||||
{/* Product Image */}
|
||||
<div className='size-[86px] rounded-lg overflow-hidden'>
|
||||
<img src={item.product?.images?.[0]} className='size-full' />
|
||||
</div>
|
||||
|
||||
{/* Order Details */}
|
||||
<div className="flex-1 flex gap-20 pr-10">
|
||||
<div className='flex items-center gap-2'>
|
||||
<div className="text-xs text-desc">عنوان:</div>
|
||||
<div className="text-sm font-medium text-black">{item.product.title}</div>
|
||||
</div>
|
||||
<div className='flex items-center gap-2'>
|
||||
<div className="text-xs text-desc">نام طرح:</div>
|
||||
<div className="text-sm font-medium text-black">{'orderData.designer'}</div>
|
||||
</div>
|
||||
<div className='flex items-center gap-2'>
|
||||
<div className="text-xs text-desc">تعداد:</div>
|
||||
<div className="text-sm font-medium text-black">{item.quantity}</div>
|
||||
</div>
|
||||
<div className='flex items-center gap-2'>
|
||||
<div className="text-xs text-desc">تخمین زمان:</div>
|
||||
<div className="text-sm font-medium text-black">{data?.data?.estimatedDays}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Order Description */}
|
||||
<div className="mt-6 pt-6 border-t border-dashed border-desc">
|
||||
<div className="text-sm font-medium mb-2 text-desc">شرح سفارش:</div>
|
||||
<p className="text-sm font-light text-black leading-6">
|
||||
{item.description}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Bottom Section - White Card */}
|
||||
<TicketSection />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default RequestDetail
|
||||
@@ -0,0 +1,138 @@
|
||||
import { type FC } from 'react'
|
||||
import type { AttributeType, RequestType } from '../type/Types'
|
||||
import { clx } from '@/helpers/utils'
|
||||
import { FieldTypeEnum } from '../enum/RequestEnum'
|
||||
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'
|
||||
|
||||
type Props = {
|
||||
attributes?: AttributeType[],
|
||||
formik: FormikProps<RequestType>
|
||||
}
|
||||
|
||||
const SERVICE_LINKS_LABEL = 'لینک سرویس ها'
|
||||
|
||||
const ManageAttribute: FC<Props> = (props) => {
|
||||
|
||||
const { attributes, formik } = props
|
||||
|
||||
const getAttributeLabel = (name: string) => name === SERVICE_LINKS_LABEL ? name : 'request'
|
||||
|
||||
const handleChange = (attributeId: string, 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={getAttributeLabel(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'>{getAttributeLabel(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'>{getAttributeLabel(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={getAttributeLabel(item.name)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
else if (item.type === FieldTypeEnum.number || item.type === FieldTypeEnum.text)
|
||||
return (
|
||||
<div key={item.id} className='mt-6'>
|
||||
<Input
|
||||
label={getAttributeLabel(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={getAttributeLabel(item.name)}
|
||||
onChange={(e) => handleChange(item.id, e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
|
||||
})
|
||||
}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ManageAttribute
|
||||
@@ -0,0 +1,28 @@
|
||||
import { type FC, type SelectHTMLAttributes } from 'react'
|
||||
import { useGetProducts } from '../hooks/useRequestData'
|
||||
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
|
||||
@@ -0,0 +1,191 @@
|
||||
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 ProductsSelect from './ProductsSelect'
|
||||
import { useFormik } from 'formik'
|
||||
import * as Yup from 'yup'
|
||||
import type { AttachmentsType, RequestType, ProductType } from '../type/Types'
|
||||
import { useGetAttributes, useGetProducts } from '../hooks/useRequestData'
|
||||
import ManageAttribute from './ManageAttribute'
|
||||
import { useMultiUpload, useSingleUpload } from '@/pages/uploader/hooks/useUploader'
|
||||
import { useRequestStore } from '../store/RequestStore'
|
||||
import { clx } from '@/helpers/utils'
|
||||
|
||||
type Props = {
|
||||
addNewItem: () => void,
|
||||
}
|
||||
|
||||
const Request: FC<Props> = ({ addNewItem }) => {
|
||||
|
||||
const { data } = useGetProducts()
|
||||
const { setItems, items } = useRequestStore()
|
||||
const [indexRequest, setIndexRequest] = 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<RequestType>({
|
||||
initialValues: {
|
||||
productId: undefined,
|
||||
attachments: [],
|
||||
quantity: undefined,
|
||||
description: '',
|
||||
attributes: []
|
||||
},
|
||||
validationSchema: Yup.object({
|
||||
productId: Yup.string().required('این فیلد اجباری می باشد'),
|
||||
quantity: 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 (indexRequest === -1) {
|
||||
values.attachments = attachments
|
||||
setIndexRequest(items.length)
|
||||
setItems([...items, values])
|
||||
addNewItem()
|
||||
setIsEditMode(true)
|
||||
} else {
|
||||
if (values.attachments.length) {
|
||||
values.attachments = [...values.attachments, ...attachments]
|
||||
|
||||
}
|
||||
const items_edit = [...items]
|
||||
items_edit[indexRequest] = values
|
||||
setItems(items_edit)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const handleChange = (e: ChangeEvent<HTMLSelectElement>) => {
|
||||
const productId: string = e.target.value
|
||||
const product = data?.data?.find(o => o.id === 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='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 Request
|
||||
@@ -0,0 +1,253 @@
|
||||
import Button from '@/components/Button'
|
||||
import UploadBox from '@/components/UploadBox'
|
||||
import { Microphone, Paperclip2 } from 'iconsax-react'
|
||||
import { useState, type FC } from 'react'
|
||||
import { useVoiceRecorder } from '@/hooks/useVoiceRecorder'
|
||||
import { Play, Pause } from 'iconsax-react'
|
||||
import { useMultiUpload, useSingleUpload } from '@/pages/uploader/hooks/useUploader'
|
||||
import type { AddTicketType } from '../type/Types'
|
||||
import { useAddTicket, useGetTickets } from '../hooks/useRequestData'
|
||||
import { useParams } from 'react-router-dom'
|
||||
import { toast } from '@/shared/toast'
|
||||
import { extractErrorMessage, getFileNameAndExtensionFromUrl } from '@/config/func'
|
||||
import VoicePlayer from '@/components/VoicePlayer'
|
||||
|
||||
|
||||
|
||||
const TicketSection: FC = () => {
|
||||
|
||||
const { id } = useParams()
|
||||
const [message, setMessage] = useState('')
|
||||
const [files, setFiles] = useState<File[]>([])
|
||||
const addTicket = useAddTicket()
|
||||
const { data, refetch } = useGetTickets(id!)
|
||||
const singleUpload = useSingleUpload()
|
||||
const multiUpload = useMultiUpload()
|
||||
|
||||
const {
|
||||
isRecording,
|
||||
isPlaying,
|
||||
audioUrl,
|
||||
audioRef,
|
||||
togglePlayPause,
|
||||
startRecording,
|
||||
stopRecording,
|
||||
progress,
|
||||
audioFile,
|
||||
resetRecorder
|
||||
} = useVoiceRecorder()
|
||||
|
||||
const handleSubmit = async () => {
|
||||
const params: AddTicketType = {
|
||||
attachments: [],
|
||||
content: message
|
||||
}
|
||||
if (audioFile) {
|
||||
await singleUpload.mutateAsync(audioFile, {
|
||||
onSuccess: (data) => {
|
||||
params.attachments.push({
|
||||
type: 'voice',
|
||||
url: data?.data?.url
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
if (files.length) {
|
||||
await multiUpload.mutateAsync(files, {
|
||||
onSuccess: (data) => {
|
||||
data?.data?.map((item) => {
|
||||
params.attachments.push({
|
||||
type: 'uploads_attach',
|
||||
url: item.url
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
addTicket.mutate({ requestId: id!, params: params }, {
|
||||
onSuccess: () => {
|
||||
toast('پیام شما با موفقیت ارسال شد')
|
||||
refetch()
|
||||
setMessage('')
|
||||
setFiles([])
|
||||
resetRecorder()
|
||||
},
|
||||
onError: (error) => {
|
||||
toast(extractErrorMessage(error), 'error')
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const handleOpenLink = (url: string) => {
|
||||
window.open(
|
||||
url,
|
||||
'_blank'
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-white rounded-2xl p-6">
|
||||
|
||||
{
|
||||
data?.data?.map((item) => {
|
||||
if (item.user)
|
||||
return (
|
||||
<div key={item.id} className='bg-[#F5F7FC] rounded-4xl rounded-tr-none mt-6 p-6 max-w-[55%]'>
|
||||
<div className='text-sm font-light text-black leading-6'>
|
||||
{item.content}
|
||||
</div>
|
||||
|
||||
{/* attachments (non-voice) */}
|
||||
<div className="flex gap-3 flex-wrap mt-3">
|
||||
{item.attachments
|
||||
?.filter((a) => a.type !== 'voice')
|
||||
.map((attach) => (
|
||||
<div
|
||||
key={attach.url}
|
||||
onClick={() => handleOpenLink(attach.url)}
|
||||
className="flex cursor-pointer items-center gap-1.5 text-[#0047FF]"
|
||||
>
|
||||
<Paperclip2 size={20} color="#0047FF" />
|
||||
<div className="text-xs">
|
||||
{getFileNameAndExtensionFromUrl(attach.url).fileName}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* voice messages */}
|
||||
{item.attachments
|
||||
?.filter((a) => a.type === 'voice')
|
||||
.map((voice) => (
|
||||
<VoicePlayer key={voice.url} url={voice.url} />
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
else if (item.admin)
|
||||
return (
|
||||
<div className='flex justify-end'>
|
||||
<div className='bg-[#F5F7FC] rounded-4xl rounded-tl-none mt-6 p-6 max-w-[55%]'>
|
||||
<div className='flex gap-1 text-sm mb-2'>
|
||||
<div className='font-bold'>طراح : </div>
|
||||
<div>{item.admin?.firstName + ' ' + item?.admin?.lastName}</div>
|
||||
</div>
|
||||
<div className='text-sm font-light text-black leading-6'>
|
||||
{item.content}
|
||||
</div>
|
||||
|
||||
{/* attachments (non-voice) */}
|
||||
<div className="flex gap-3 flex-wrap mt-3">
|
||||
{item.attachments
|
||||
?.filter((a) => a.type !== 'voice')
|
||||
.map((attach) => (
|
||||
<div
|
||||
key={attach.url}
|
||||
onClick={() => handleOpenLink(attach.url)}
|
||||
className="flex cursor-pointer items-center gap-1.5 text-[#0047FF]"
|
||||
>
|
||||
<Paperclip2 size={20} color="#0047FF" />
|
||||
<div className="text-xs">
|
||||
{getFileNameAndExtensionFromUrl(attach.url).fileName}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* voice messages */}
|
||||
{item.attachments
|
||||
?.filter((a) => a.type === 'voice')
|
||||
.map((voice) => (
|
||||
<VoicePlayer key={voice.url} url={voice.url} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<div className="mt-6">
|
||||
<div className="text-sm mb-2 text-black">پیام شما</div>
|
||||
|
||||
<div className="relative">
|
||||
<textarea
|
||||
value={message}
|
||||
onChange={(e) => setMessage(e.target.value)}
|
||||
className="w-full h-40 bg-white border border-border rounded-xl p-4 text-sm resize-none outline-none"
|
||||
placeholder=""
|
||||
/>
|
||||
|
||||
<button
|
||||
onClick={isRecording ? stopRecording : startRecording}
|
||||
className="absolute left-4 bottom-4 bg-[#FFF1D7] size-8 rounded-lg flex items-center justify-center"
|
||||
>
|
||||
<Microphone
|
||||
size={20}
|
||||
color={isRecording ? '#EF4444' : 'black'}
|
||||
variant={isRecording ? 'Bold' : 'Outline'}
|
||||
/>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{audioUrl && (
|
||||
<div className="w-full h-12 bg-white border border-border rounded-xl flex items-center px-3 gap-3 mt-3">
|
||||
<button
|
||||
onClick={togglePlayPause}
|
||||
className="w-8 h-8 rounded-full bg-black flex items-center justify-center flex-shrink-0"
|
||||
>
|
||||
{isPlaying ? (
|
||||
<Pause size={16} color="#fff" variant="Bold" />
|
||||
) : (
|
||||
<Play size={16} color="#fff" variant="Bold" />
|
||||
)}
|
||||
</button>
|
||||
|
||||
<div className="flex-1 flex items-center gap-[2px] h-8">
|
||||
{Array.from({ length: 60 }).map((_, i) => {
|
||||
const passed = i / 60 <= progress
|
||||
return (
|
||||
<div
|
||||
key={i}
|
||||
className="w-[2px] rounded-full transition-all"
|
||||
style={{
|
||||
height: `${Math.random() * 18 + 4}px`,
|
||||
backgroundColor: passed ? '#0047FF' : '#E5E7EB'
|
||||
}}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
<audio ref={audioRef} src={audioUrl} className="hidden" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
|
||||
<div className="mt-6">
|
||||
<UploadBox
|
||||
label="فایل های ضمیمه"
|
||||
isMultiple={true}
|
||||
onChange={setFiles}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex mt-10 justify-end">
|
||||
<Button
|
||||
label="ارسال"
|
||||
onClick={handleSubmit}
|
||||
className='w-[150px]'
|
||||
isLoading={singleUpload.isPending || multiUpload.isPending || addTicket.isPending}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default TicketSection
|
||||
@@ -0,0 +1,42 @@
|
||||
export const enum TabMyRequestsEnum {
|
||||
ALL = "all",
|
||||
IN_PROGRESS = "in_progress",
|
||||
CONFIRMED = "confirmed",
|
||||
DELIVERED = "delivered",
|
||||
CANCELLED = "cancelled",
|
||||
}
|
||||
|
||||
export const enum FieldTypeEnum {
|
||||
text = "text",
|
||||
textarea = "textarea",
|
||||
number = "number",
|
||||
select = "select",
|
||||
radio = "radio",
|
||||
checkbox = "checkbox",
|
||||
date = "date",
|
||||
}
|
||||
|
||||
export enum RequestStatusEnum {
|
||||
CREATED = "created",
|
||||
|
||||
INVOICED = "invoiced",
|
||||
WAITING_to_CONFIRM_INVOICE = "waiting_to_confirm_invoice",
|
||||
|
||||
DESIGN_INITIATED = "design_initiated",
|
||||
DESIGN_FINISHED = "design_finished",
|
||||
DESIGN_REVISION = "design_revision",
|
||||
|
||||
READY_TO_PRINTING = "ready_to_printing",
|
||||
IN_PRINTING = "in_printing",
|
||||
|
||||
READY_FOR_DELIVERY = "ready_for_delivery",
|
||||
DELIVERED = "delivered",
|
||||
|
||||
AFTER_PRINT_COVER = "after_print_cover",
|
||||
AFTER_PRINT_BOXING = "after_print_boxing",
|
||||
AFTER_PRINT_CARTONING = "after_print_cartoning",
|
||||
|
||||
FINISHED = "finished",
|
||||
|
||||
CANCELED = "canceled",
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import * as api from "../service/RequestService";
|
||||
import type { TabMyRequestsEnum } from "../enum/RequestEnum";
|
||||
import type { AddTicketType } from "../type/Types";
|
||||
|
||||
export const useGetProducts = () => {
|
||||
return useQuery({
|
||||
queryKey: ["products"],
|
||||
queryFn: api.getProducts,
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetAttributes = (id?: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["products-attributes", id],
|
||||
queryFn: () => api.getAttributes(id),
|
||||
enabled: !!id,
|
||||
});
|
||||
};
|
||||
|
||||
export const useSubmitRequest = () => {
|
||||
return useMutation({
|
||||
mutationFn: api.submitRequest,
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetMyRequests = (page: number = 1, statuses: TabMyRequestsEnum) => {
|
||||
return useQuery({
|
||||
queryKey: ["my-requests", page, statuses],
|
||||
queryFn: () => api.getMyRequests(page, statuses),
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetRequestDetails = (id: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["request", id],
|
||||
queryFn: () => api.getRequestDetails(id),
|
||||
enabled: !!id,
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetTickets = (requestId: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["tickets", requestId],
|
||||
queryFn: () => api.getTickets(requestId),
|
||||
enabled: !!requestId,
|
||||
});
|
||||
};
|
||||
|
||||
export const useAddTicket = () => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
requestId,
|
||||
params,
|
||||
}: {
|
||||
requestId: string;
|
||||
params: AddTicketType;
|
||||
}) => api.addTicket(requestId, params),
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,80 @@
|
||||
import axios from "@/config/axios";
|
||||
import type {
|
||||
AddTicketType,
|
||||
AttributeResponseType,
|
||||
MyRequestsResponseType,
|
||||
RequestDetailResponseType,
|
||||
RequestType,
|
||||
ProductsResponseType,
|
||||
TicketsResponseType,
|
||||
} from "../type/Types";
|
||||
import { RequestStatusEnum, TabMyRequestsEnum } from "../enum/RequestEnum";
|
||||
|
||||
export const getProducts = async () => {
|
||||
const { data } = await axios.get<ProductsResponseType>(`/public/products`);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getAttributes = async (productId?: string) => {
|
||||
const { data } = await axios.get<AttributeResponseType>(
|
||||
`/public/entity/${productId}/field`,
|
||||
);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const submitRequest = async (params: RequestType[]) => {
|
||||
const { data } = await axios.post(`/public/request`, { items: params });
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getMyRequests = async (
|
||||
page: number = 1,
|
||||
active: TabMyRequestsEnum,
|
||||
) => {
|
||||
const statuses =
|
||||
active === TabMyRequestsEnum.IN_PROGRESS
|
||||
? [
|
||||
RequestStatusEnum.DESIGN_FINISHED,
|
||||
RequestStatusEnum.DESIGN_INITIATED,
|
||||
RequestStatusEnum.DESIGN_REVISION,
|
||||
RequestStatusEnum.IN_PRINTING,
|
||||
RequestStatusEnum.AFTER_PRINT_BOXING,
|
||||
RequestStatusEnum.AFTER_PRINT_CARTONING,
|
||||
RequestStatusEnum.AFTER_PRINT_COVER,
|
||||
RequestStatusEnum.READY_FOR_DELIVERY,
|
||||
RequestStatusEnum.READY_TO_PRINTING,
|
||||
]
|
||||
: active === TabMyRequestsEnum.CANCELLED
|
||||
? [RequestStatusEnum.CANCELED]
|
||||
: active === TabMyRequestsEnum.DELIVERED
|
||||
? [RequestStatusEnum.DELIVERED]
|
||||
: undefined;
|
||||
|
||||
let query = `?page=${page}`;
|
||||
if (statuses) {
|
||||
query += `&statuses=${statuses}`;
|
||||
}
|
||||
const { data } = await axios.get<MyRequestsResponseType>(
|
||||
`/public/request${query}`,
|
||||
);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getRequestDetails = async (id: string) => {
|
||||
const { data } = await axios.get<RequestDetailResponseType>(
|
||||
`/public/request/${id}`,
|
||||
);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getTickets = async (id: string) => {
|
||||
const { data } = await axios.get<TicketsResponseType>(
|
||||
`/public/ticket/ref/${id}`,
|
||||
);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const addTicket = async (id: string, params: AddTicketType) => {
|
||||
const { data } = await axios.post(`/public/ticket/ref/${id}`, params);
|
||||
return data;
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
import { create } from "zustand";
|
||||
import type { StoreType } from "../type/Types";
|
||||
|
||||
export const useRequestStore = create<StoreType>((set) => ({
|
||||
items: [],
|
||||
setItems(value) {
|
||||
set({ items: value });
|
||||
},
|
||||
}));
|
||||
@@ -0,0 +1,124 @@
|
||||
import type { BaseResponse } from "@/shared/types/Types";
|
||||
import type { FieldTypeEnum } from "../enum/RequestEnum";
|
||||
import type { RowDataType } from "@/components/types/TableTypes";
|
||||
|
||||
export type CategoryType = {
|
||||
avatarUrl?: string;
|
||||
createdAt: string;
|
||||
id: string;
|
||||
isActive: boolean;
|
||||
order: number;
|
||||
parent?: CategoryType;
|
||||
title: string;
|
||||
};
|
||||
|
||||
export type AttachmentsType = {
|
||||
url: string;
|
||||
type: string;
|
||||
};
|
||||
|
||||
export type RequestType = {
|
||||
productId?: string;
|
||||
quantity?: number;
|
||||
attributes: {
|
||||
attributeId: string;
|
||||
value: string | number;
|
||||
}[];
|
||||
attachments: AttachmentsType[];
|
||||
description?: string;
|
||||
};
|
||||
|
||||
export type AttributeValueType = {
|
||||
attribute: string;
|
||||
createdAt: string;
|
||||
id: string;
|
||||
order: number;
|
||||
value: string;
|
||||
};
|
||||
|
||||
export type AttributeType = {
|
||||
createdAt: string;
|
||||
id: string;
|
||||
isRequired: boolean;
|
||||
name: string;
|
||||
order: number;
|
||||
product: string;
|
||||
type: FieldTypeEnum;
|
||||
options: AttributeValueType[];
|
||||
};
|
||||
|
||||
export type ProductType = {
|
||||
category: CategoryType;
|
||||
createdAt: string;
|
||||
desc: string;
|
||||
id: string;
|
||||
images?: string[];
|
||||
isActive: boolean;
|
||||
linkUrl: string;
|
||||
order: number;
|
||||
quantities: number[];
|
||||
title: string;
|
||||
};
|
||||
|
||||
export type ProductsResponseType = BaseResponse<ProductType[]>;
|
||||
|
||||
export type StoreType = {
|
||||
items: RequestType[];
|
||||
setItems: (value: RequestType[]) => void;
|
||||
};
|
||||
|
||||
export interface MyRequestType extends RowDataType {
|
||||
balance: number;
|
||||
createdAt: string;
|
||||
discount: number;
|
||||
enableTax: boolean;
|
||||
estimatedDays?: number;
|
||||
orderNumber: number;
|
||||
paidAmount: number;
|
||||
paymentMethod: string;
|
||||
status: string;
|
||||
subTotal: number;
|
||||
taxAmount: number;
|
||||
total: number;
|
||||
items: {
|
||||
adminDescription?: string;
|
||||
id: string;
|
||||
attachments: {
|
||||
type: string;
|
||||
url: string;
|
||||
}[];
|
||||
description: string;
|
||||
product: ProductType;
|
||||
quantity: number;
|
||||
subTotal: number;
|
||||
total: number;
|
||||
unitPrice: number;
|
||||
}[];
|
||||
}
|
||||
|
||||
export type MyRequestsResponseType = BaseResponse<MyRequestType[]>;
|
||||
|
||||
export type RequestDetailResponseType = BaseResponse<MyRequestType>;
|
||||
|
||||
export type AddTicketType = {
|
||||
content: string;
|
||||
attachments: AttachmentsType[];
|
||||
};
|
||||
|
||||
export type TicketType = {
|
||||
admin?: {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
phone: string;
|
||||
id: string;
|
||||
};
|
||||
attachments: AttachmentsType[];
|
||||
content: string;
|
||||
createdAt: string;
|
||||
id: string;
|
||||
user?: string;
|
||||
};
|
||||
|
||||
export type TicketsResponseType = BaseResponse<TicketType[]>;
|
||||
|
||||
export type AttributeResponseType = BaseResponse<AttributeType[]>;
|
||||
Reference in New Issue
Block a user