permission
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { FC, useState } from 'react'
|
||||
import { FC, Fragment, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import Input from '../../components/Input'
|
||||
import Button from '../../components/Button'
|
||||
@@ -7,15 +7,13 @@ import { TickCircle } from 'iconsax-react'
|
||||
import DatePickerComponent from '../../components/DatePicker'
|
||||
import RadioGroup from '../../components/RadioGroup'
|
||||
import { useFormik } from 'formik'
|
||||
import { CreateDiscountType, DiscountCalculationType, DiscountType } from './types/DiscountTypes'
|
||||
import { CreateDiscountType } from './types/DiscountTypes'
|
||||
import * as Yup from 'yup'
|
||||
import { useGetAllServices, useGetPlans } from '../service/hooks/useServiceData'
|
||||
import { PlanItemType, ServiceItemType } from '../service/types/ServiceTypes'
|
||||
import { useGetAllServices } from '../service/hooks/useServiceData'
|
||||
import { ServiceItemType } from '../service/types/ServiceTypes'
|
||||
import CheckBoxComponent from '../../components/CheckBoxComponent'
|
||||
import { toast } from 'react-toastify'
|
||||
import { useCreateDiscount } from './hooks/useDiscountData'
|
||||
import { useGetCustomers } from '../customer/hooks/useCustomerData'
|
||||
import { CustomerItemType } from '../customer/types/CustomerTypes'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { Pages } from '../../config/Pages'
|
||||
import { ErrorType } from '../../helpers/types'
|
||||
@@ -25,55 +23,49 @@ const CreateDiscount: FC = () => {
|
||||
|
||||
const navigate = useNavigate()
|
||||
const { t } = useTranslation('global')
|
||||
const [serviceId, setServiceId] = useState<string>('')
|
||||
const [planId, setPlanId] = useState<string>('')
|
||||
const [customerId, setCustomerId] = useState('')
|
||||
const [serviceId, setServiceId] = useState<string[]>([])
|
||||
// const [planId, setPlanId] = useState<string>('')
|
||||
// const [customerId, setCustomerId] = useState('')
|
||||
const getServices = useGetAllServices('', 1, '', '', undefined, 50)
|
||||
const getPlans = useGetPlans(serviceId)
|
||||
const getCustomers = useGetCustomers()
|
||||
// const getPlans = useGetPlans(serviceId)
|
||||
// const getCustomers = useGetCustomers()
|
||||
const createDiscount = useCreateDiscount()
|
||||
|
||||
const formik = useFormik<CreateDiscountType>({
|
||||
initialValues: {
|
||||
amount: '',
|
||||
calculationType: DiscountCalculationType.FIXED,
|
||||
endDate: '',
|
||||
startDate: '',
|
||||
title: '',
|
||||
type: DiscountType.DIRECT,
|
||||
subscriptionPlanIds: [],
|
||||
userIds: []
|
||||
type: 1,
|
||||
targetServices: [],
|
||||
direct: true,
|
||||
value: 0,
|
||||
isActive: true
|
||||
},
|
||||
validationSchema: Yup.object({
|
||||
amount: Yup.string().required(t('errors.required')),
|
||||
calculationType: Yup.string().required(t('errors.required')),
|
||||
value: Yup.number().required(t('errors.required')),
|
||||
endDate: Yup.string().required(t('errors.required')),
|
||||
startDate: Yup.string().required(t('errors.required')),
|
||||
title: Yup.string().required(t('errors.required')),
|
||||
type: Yup.string().required(t('errors.required')),
|
||||
}),
|
||||
onSubmit(values) {
|
||||
if (serviceId && planId) {
|
||||
values.subscriptionPlanIds = [planId]
|
||||
values.userIds = [customerId]
|
||||
const params: CreateDiscountType = {
|
||||
...values,
|
||||
startDate: moment(values.startDate, 'jYYYY-jMM-jDD').format('YYYY-MM-DD'),
|
||||
endDate: moment(values.endDate, 'jYYYY-jMM-jDD').format('YYYY-MM-DD'),
|
||||
amount: +values.amount
|
||||
}
|
||||
createDiscount.mutate(params, {
|
||||
onSuccess: () => {
|
||||
toast.success(t('success'))
|
||||
navigate(Pages.discount.list)
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast.error(error.response?.data?.error?.message[0])
|
||||
}
|
||||
})
|
||||
} else {
|
||||
toast.error(t('discount.errors_plan'))
|
||||
values.targetServices = serviceId
|
||||
const params: CreateDiscountType = {
|
||||
...values,
|
||||
startDate: moment(values.startDate, 'jYYYY-jMM-jDD').format('YYYY-MM-DD'),
|
||||
endDate: moment(values.endDate, 'jYYYY-jMM-jDD').format('YYYY-MM-DD'),
|
||||
value: +values.value
|
||||
}
|
||||
createDiscount.mutate(params, {
|
||||
onSuccess: () => {
|
||||
toast.success(t('success'))
|
||||
navigate(Pages.discount.list)
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast.error(error.response?.data?.error?.message[0])
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
||||
return (
|
||||
@@ -103,15 +95,15 @@ const CreateDiscount: FC = () => {
|
||||
items={[
|
||||
{
|
||||
label: t('discount.direct_discount'),
|
||||
value: DiscountType.DIRECT
|
||||
value: true
|
||||
},
|
||||
{
|
||||
label: t('discount.discount_code'),
|
||||
value: DiscountType.CODE
|
||||
value: false
|
||||
}
|
||||
]}
|
||||
onChange={(value) => formik.setFieldValue('type', value)}
|
||||
selected={formik.values.type}
|
||||
onChange={(value) => formik.setFieldValue('direct', value)}
|
||||
selected={formik.values.direct}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -141,23 +133,23 @@ const CreateDiscount: FC = () => {
|
||||
items={[
|
||||
{
|
||||
label: t('discount.fixed_amount'),
|
||||
value: DiscountCalculationType.FIXED
|
||||
value: '1'
|
||||
},
|
||||
{
|
||||
label: t('discount.percent'),
|
||||
value: DiscountCalculationType.PERCENTAGE
|
||||
value: '0'
|
||||
}
|
||||
]}
|
||||
onChange={(value) => formik.setFieldValue('calculationType', value)}
|
||||
selected={formik.values.calculationType}
|
||||
onChange={(value) => formik.setFieldValue('type', value)}
|
||||
selected={formik.values.type.toString()}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<Input
|
||||
label={t('discount.price_or_percent')}
|
||||
placeholder={t('discount.enter_price_or_percent')}
|
||||
{...formik.getFieldProps('amount')}
|
||||
error_text={formik.touched.amount && formik.errors.amount ? formik.errors.amount : ''}
|
||||
{...formik.getFieldProps('value')}
|
||||
error_text={formik.touched.value && formik.errors.value ? formik.errors.value : ''}
|
||||
/>
|
||||
</div>
|
||||
<div className='mt-6 rowTwoInput'>
|
||||
@@ -190,30 +182,36 @@ const CreateDiscount: FC = () => {
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<p className='mt-10 text-sm'>{t('discount.availabe_service')}</p>
|
||||
<div className='p-2 text-sm border border-[##D0D0D0] rounded-lg mt-2'>
|
||||
<Input
|
||||
variant='search'
|
||||
placeholder={t('header.search')}
|
||||
/>
|
||||
{
|
||||
getServices.data?.data?.services?.map((item: ServiceItemType) => (
|
||||
<div key={item.id} className='flex gap-2 mt-3 py-2 items-center'>
|
||||
<div>
|
||||
<CheckBoxComponent
|
||||
checked={serviceId === item.id}
|
||||
onChange={(e) => setServiceId(e.target.checked ? item.id : '')}
|
||||
/>
|
||||
</div>
|
||||
<div className='text-xs leading-5'>
|
||||
{item.name}
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
}
|
||||
</div>
|
||||
|
||||
{
|
||||
formik.values.direct &&
|
||||
<Fragment>
|
||||
<p className='mt-10 text-sm'>{t('discount.availabe_service')}</p>
|
||||
<div className='p-2 text-sm border border-[##D0D0D0] rounded-lg mt-2'>
|
||||
<Input
|
||||
variant='search'
|
||||
placeholder={t('header.search')}
|
||||
/>
|
||||
{
|
||||
getServices.data?.data?.services?.map((item: ServiceItemType) => (
|
||||
<div key={item.id} className='flex gap-2 mt-3 py-2 items-center'>
|
||||
<div>
|
||||
<CheckBoxComponent
|
||||
checked={serviceId.includes(item.id)}
|
||||
onChange={(e) => setServiceId(e.target.checked ? [...serviceId, item.id] : serviceId.filter(id => id !== item.id))}
|
||||
/>
|
||||
</div>
|
||||
<div className='text-xs leading-5'>
|
||||
{item.name}
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
}
|
||||
</div>
|
||||
</Fragment>
|
||||
}
|
||||
|
||||
{/* {
|
||||
getPlans.data &&
|
||||
<div className='p-2 text-sm border border-[##D0D0D0] rounded-lg mt-2'>
|
||||
{
|
||||
@@ -253,7 +251,7 @@ const CreateDiscount: FC = () => {
|
||||
))
|
||||
}
|
||||
</div>
|
||||
}
|
||||
} */}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
export type CreateDiscountType = {
|
||||
title: string;
|
||||
type: string;
|
||||
calculationType: DiscountCalculationType;
|
||||
amount: string | number;
|
||||
type: number;
|
||||
direct: boolean;
|
||||
value: number;
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
subscriptionPlanIds: string[];
|
||||
userIds: string[];
|
||||
isActive: boolean;
|
||||
targetServices: string[];
|
||||
};
|
||||
|
||||
export enum DiscountType {
|
||||
|
||||
@@ -112,6 +112,9 @@ const RoleCreate: FC = () => {
|
||||
)
|
||||
})
|
||||
}
|
||||
<div className='flex flex-1 min-w-[23%] items-center'></div>
|
||||
<div className='flex flex-1 min-w-[23%] items-center'></div>
|
||||
<div className='flex flex-1 min-w-[23%] items-center'></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -51,3 +51,10 @@ export const useGetGroups = () => {
|
||||
queryFn: () => api.getGroups(),
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetAdminPermissions = () => {
|
||||
return useQuery({
|
||||
queryKey: ["admin-permissions"],
|
||||
queryFn: () => api.getAdminPermissions(),
|
||||
});
|
||||
};
|
||||
|
||||
@@ -39,3 +39,8 @@ export const getGroups = async () => {
|
||||
const { data } = await axios.get(`/users/user-group`);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getAdminPermissions = async () => {
|
||||
const { data } = await axios.get(`/users/admin-permissions`);
|
||||
return data;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user