category service

This commit is contained in:
hamid zarghami
2025-01-26 15:45:46 +03:30
parent bfb9f3cbcb
commit 22482aa20a
34 changed files with 1146 additions and 204 deletions
@@ -0,0 +1,123 @@
import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import SwitchComponent from '../../../components/Switch'
import Input from '../../../components/Input'
import Select from '../../../components/Select'
import UploadBoxDraggble from '../../../components/UploadBoxDraggble'
import Button from '../../../components/Button'
import { TickCircle } from 'iconsax-react'
import { useCreateServiceCategory, useGetCategoryParents } from '../hooks/useServiceData'
import { CreateServiceCategoryType, ServiceCategoryType } from '../types/ServiceTypes'
import * as Yup from 'yup'
import { useFormik } from 'formik'
import { toast } from 'react-toastify'
import { ErrorType } from '../../../helpers/types'
const CreateCategory: FC = () => {
const { t } = useTranslation('global')
const getCategory = useGetCategoryParents()
const createCategory = useCreateServiceCategory()
const formik = useFormik<CreateServiceCategoryType>({
initialValues: {
title: '',
isActive: true,
parentId: ''
},
validationSchema: Yup.object({
title: Yup.string().required(t('errors.required')),
}),
onSubmit: (values) => {
const params = {
...values,
icon: 'https://picsum.photos/200/300',
parentId: values.parentId ? values.parentId : undefined
}
createCategory.mutate(params, {
onSuccess: () => {
formik.resetForm()
getCategory.refetch()
toast.success(t('success'))
},
onError: (error: ErrorType) => {
toast.error(error?.response?.data?.error?.message[0])
}
})
}
})
return (
<div className='bg-white p-8 w-sidebar hidden xl:block rounded-3xl overflow-hidden relative'>
<div>
{t('service.add_category')}
</div>
<div className='mt-8 flex justify-between'>
<div className='text-sm'>
{t('service.status_category')}
</div>
<div className='flex'>
<SwitchComponent
active={formik.values.isActive}
onChange={(value) => formik.setFieldValue('isActive', value)}
/>
</div>
</div>
<div className='mt-8'>
<Input
label={t('service.title_category')}
value={formik.values.title}
name='title'
onChange={formik.handleChange}
error_text={formik.touched.title && formik.errors.title ? formik.errors.title : ''}
/>
</div>
{
getCategory.data &&
<div className='mt-6'>
<Select
label={t('service.category_parent_2')}
items={getCategory.data?.data?.categories?.map((item: ServiceCategoryType) => {
return {
label: item.title,
value: item.id
}
})}
placeholder={t('select')}
name='parentId'
onChange={formik.handleChange}
error_text={formik.touched.parentId && formik.errors.parentId ? formik.errors.parentId : ''}
/>
</div>
}
<div className='mt-6'>
<UploadBoxDraggble
label={t('service.icon_serice_category')}
onChange={() => null}
/>
</div>
<div className='flex flex-1 justify-end items-end mt-8'>
<Button
className='w-fit px-5'
onClick={() => formik.handleSubmit()}
isLoading={createCategory.isPending}
>
<div className='flex items-center gap-2'>
<TickCircle
className='size-5'
color='white'
/>
<div>{t('save')}</div>
</div>
</Button>
</div>
</div>
)
}
export default CreateCategory
@@ -0,0 +1,28 @@
import { FC, useState } from 'react'
import SwitchComponent from '../../../components/Switch'
import { useChangeStatusCategory } from '../hooks/useServiceData'
type Props = {
defaultActive: boolean,
id: string
}
const StatusCategory: FC<Props> = (props: Props) => {
const [isActive, setIsActive] = useState<boolean>(props.defaultActive)
const toggleStatus = useChangeStatusCategory()
const handleChange = (value: boolean) => {
setIsActive(value)
toggleStatus.mutate({ id: props.id })
}
return (
<SwitchComponent
active={isActive}
onChange={handleChange}
/>
)
}
export default StatusCategory