Requests
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user