update a resutarant
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
ReportsResponse,
|
||||
SystemRolesResponse,
|
||||
RestaurantAdminsResponse,
|
||||
UpdateRestaurantType,
|
||||
} from "../types/Types";
|
||||
|
||||
export const useGetIcons = () => {
|
||||
@@ -98,3 +99,23 @@ export const useGetSystemRoles = () => {
|
||||
queryFn: api.getSystemRoles,
|
||||
});
|
||||
};
|
||||
|
||||
export const useUpdateRestaurant = () => {
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
id,
|
||||
params,
|
||||
}: {
|
||||
id: string;
|
||||
params: UpdateRestaurantType;
|
||||
}) => api.updateRestaurant(id, params),
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetRestaurant = (id: string) => {
|
||||
return useQuery({
|
||||
queryKey: ["restaurant", id],
|
||||
queryFn: () => api.getRestaurant(id),
|
||||
enabled: !!id,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -5,11 +5,13 @@ import PageLoading from '../../../components/PageLoading'
|
||||
import Td from '../../../components/Td'
|
||||
import { RestaurantType, RestaurantsResponse } from '../types/Types'
|
||||
import moment from 'moment-jalaali'
|
||||
import { Copy } from 'iconsax-react'
|
||||
import { Copy, Edit } from 'iconsax-react'
|
||||
import { toast } from 'react-toastify'
|
||||
import Button from '../../../components/Button'
|
||||
import RestaurantAdminsModal from './components/RestaurantAdminsModal'
|
||||
import Pagination from '../../../components/Pagination'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { Pages } from '../../../config/Pages'
|
||||
|
||||
const RestaurantsList: FC = () => {
|
||||
|
||||
@@ -106,7 +108,15 @@ const RestaurantsList: FC = () => {
|
||||
</div>
|
||||
</Td>
|
||||
<Td text={''}>
|
||||
<div className='flex items-center gap-2'>
|
||||
<div className='flex items-center gap-4'>
|
||||
<Link
|
||||
to={Pages.dmenu.restaurants.update + item.id}
|
||||
>
|
||||
<Edit
|
||||
size={18}
|
||||
color='#8C90A3'
|
||||
/>
|
||||
</Link>
|
||||
<Button
|
||||
className='w-fit px-4'
|
||||
onClick={() => handleOpenAdminsModal(item.id)}
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
import { useFormik } from 'formik'
|
||||
import { FC, useEffect } from 'react'
|
||||
import { UpdateRestaurantType, RestaurantResponse } from '../types/Types'
|
||||
import { useParams, useNavigate } from 'react-router-dom'
|
||||
import { useGetRestaurant, useUpdateRestaurant } from '../hooks/useIconData'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import Input from '../../../components/Input'
|
||||
import Select from '../../../components/Select'
|
||||
import SwitchComponent from '../../../components/Switch'
|
||||
import Button from '../../../components/Button'
|
||||
import DatePickerComponent from '../../../components/DatePicker'
|
||||
import PageLoading from '../../../components/PageLoading'
|
||||
import { toast } from 'react-toastify'
|
||||
import { ErrorType } from '../../../helpers/types'
|
||||
import { TickCircle } from 'iconsax-react'
|
||||
import { Pages } from '../../../config/Pages'
|
||||
|
||||
const UpdateRestaurant: FC = () => {
|
||||
|
||||
const { id } = useParams()
|
||||
const { t } = useTranslation('global')
|
||||
const navigate = useNavigate()
|
||||
const { data: restaurantData, isLoading } = useGetRestaurant(id || '')
|
||||
const updateRestaurant = useUpdateRestaurant()
|
||||
|
||||
const restaurant = (restaurantData as RestaurantResponse | undefined)?.data
|
||||
|
||||
const formik = useFormik<UpdateRestaurantType>({
|
||||
initialValues: {
|
||||
name: undefined,
|
||||
slug: undefined,
|
||||
establishedYear: undefined,
|
||||
phone: undefined,
|
||||
plan: undefined,
|
||||
subscriptionId: undefined,
|
||||
subscriptionEndDate: undefined,
|
||||
subscriptionStartDate: undefined,
|
||||
isActive: undefined,
|
||||
},
|
||||
onSubmit: (values) => {
|
||||
if (!id) return
|
||||
|
||||
updateRestaurant.mutate(
|
||||
{ id, params: values },
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success(t('success'))
|
||||
navigate(Pages.dmenu.restaurants.list)
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast.error(error.response?.data?.error?.message?.[0] || 'خطا در بروزرسانی رستوران')
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (restaurant) {
|
||||
formik.setValues({
|
||||
name: restaurant.name || undefined,
|
||||
slug: restaurant.slug || undefined,
|
||||
establishedYear: restaurant.establishedYear || undefined,
|
||||
phone: restaurant.phone || undefined,
|
||||
plan: restaurant.plan || undefined,
|
||||
subscriptionId: restaurant.subscriptionId || undefined,
|
||||
subscriptionEndDate: restaurant.subscriptionEndDate || undefined,
|
||||
subscriptionStartDate: restaurant.subscriptionStartDate || undefined,
|
||||
isActive: restaurant.isActive,
|
||||
})
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [restaurant])
|
||||
|
||||
const planOptions = [
|
||||
{ value: 'base', label: 'پایه' },
|
||||
{ value: 'premium', label: 'پرمیوم' },
|
||||
]
|
||||
|
||||
if (isLoading) {
|
||||
return <PageLoading />
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='w-full mt-4'>
|
||||
<div className='flex w-full justify-between items-center'>
|
||||
<div>
|
||||
{t('restaurant.list_restaurant')}
|
||||
</div>
|
||||
<div>
|
||||
<Button
|
||||
className='px-5'
|
||||
onClick={() => formik.handleSubmit()}
|
||||
isLoading={updateRestaurant.isPending}
|
||||
>
|
||||
<div className='flex gap-2'>
|
||||
<TickCircle
|
||||
className='size-5'
|
||||
color='#fff'
|
||||
/>
|
||||
<div>{t('save')}</div>
|
||||
</div>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className='flex gap-6 mt-10'>
|
||||
<div className='flex-1'>
|
||||
<div className='flex-1 bg-white py-8 xl:px-10 px-4 rounded-3xl'>
|
||||
<div className='rowTwoInput'>
|
||||
<Input
|
||||
label={t('restaurant.name')}
|
||||
{...formik.getFieldProps('name')}
|
||||
error_text={formik.touched.name && formik.errors.name ? formik.errors.name : ''}
|
||||
/>
|
||||
|
||||
<Input
|
||||
label={t('restaurant.slug')}
|
||||
{...formik.getFieldProps('slug')}
|
||||
error_text={formik.touched.slug && formik.errors.slug ? formik.errors.slug : ''}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='mt-8 rowTwoInput'>
|
||||
<Input
|
||||
label={t('restaurant.phone')}
|
||||
{...formik.getFieldProps('phone')}
|
||||
error_text={formik.touched.phone && formik.errors.phone ? formik.errors.phone : ''}
|
||||
/>
|
||||
|
||||
<Input
|
||||
label={t('restaurant.established_year')}
|
||||
type='number'
|
||||
{...formik.getFieldProps('establishedYear')}
|
||||
error_text={formik.touched.establishedYear && formik.errors.establishedYear ? formik.errors.establishedYear : ''}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='mt-8 rowTwoInput'>
|
||||
<Select
|
||||
label={t('restaurant.plan')}
|
||||
items={planOptions}
|
||||
{...formik.getFieldProps('plan')}
|
||||
placeholder={t('select')}
|
||||
error_text={formik.touched.plan && formik.errors.plan ? formik.errors.plan : ''}
|
||||
/>
|
||||
|
||||
<Input
|
||||
label={t('restaurant.subscription_id')}
|
||||
{...formik.getFieldProps('subscriptionId')}
|
||||
error_text={formik.touched.subscriptionId && formik.errors.subscriptionId ? formik.errors.subscriptionId : ''}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='mt-8 rowTwoInput'>
|
||||
<DatePickerComponent
|
||||
label={t('restaurant.subscription_start_date')}
|
||||
onChange={(value) => formik.setFieldValue('subscriptionStartDate', value)}
|
||||
placeholder={t('select')}
|
||||
defaulValue={formik.values.subscriptionStartDate}
|
||||
error_text={formik.touched.subscriptionStartDate && formik.errors.subscriptionStartDate ? formik.errors.subscriptionStartDate : ''}
|
||||
/>
|
||||
|
||||
<DatePickerComponent
|
||||
label={t('restaurant.subscription_end_date')}
|
||||
onChange={(value) => formik.setFieldValue('subscriptionEndDate', value)}
|
||||
placeholder={t('select')}
|
||||
defaulValue={formik.values.subscriptionEndDate}
|
||||
error_text={formik.touched.subscriptionEndDate && formik.errors.subscriptionEndDate ? formik.errors.subscriptionEndDate : ''}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='mt-8'>
|
||||
<SwitchComponent
|
||||
label={t('restaurant.status')}
|
||||
active={formik.values.isActive || false}
|
||||
onChange={(value) => formik.setFieldValue('isActive', value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UpdateRestaurant
|
||||
@@ -6,10 +6,12 @@ import {
|
||||
IconsResponse,
|
||||
ReportsResponse,
|
||||
RestaurantsResponse,
|
||||
RestaurantResponse,
|
||||
RestaurantSmsCountResponse,
|
||||
AddAdminRestaurantType,
|
||||
SystemRolesResponse,
|
||||
RestaurantAdminsResponse,
|
||||
UpdateRestaurantType,
|
||||
} from "../types/Types";
|
||||
|
||||
export const getIcons = async (): Promise<IconsResponse> => {
|
||||
@@ -95,3 +97,18 @@ export const getSystemRoles = async (): Promise<SystemRolesResponse> => {
|
||||
const { data } = await axios.get(`/admin/dmenu/system-roles`);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const updateRestaurant = async (
|
||||
id: string,
|
||||
params: UpdateRestaurantType
|
||||
) => {
|
||||
const { data } = await axios.patch(`/admin/dmenu/restaurants/${id}`, params);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getRestaurant = async (
|
||||
id: string
|
||||
): Promise<RestaurantResponse> => {
|
||||
const { data } = await axios.get(`/admin/dmenu/restaurants/${id}`);
|
||||
return data;
|
||||
};
|
||||
|
||||
@@ -121,6 +121,10 @@ export interface RestaurantsResponse extends IResponse<RestaurantsData> {
|
||||
statusCode: number;
|
||||
}
|
||||
|
||||
export interface RestaurantResponse extends IResponse<RestaurantType> {
|
||||
statusCode: number;
|
||||
}
|
||||
|
||||
export type RestaurantAdminRoleType = {
|
||||
id: string;
|
||||
createdAt: string;
|
||||
@@ -199,3 +203,15 @@ export type SystemRoleType = {
|
||||
export interface SystemRolesResponse extends IResponse<SystemRoleType[]> {
|
||||
statusCode: number;
|
||||
}
|
||||
|
||||
export type UpdateRestaurantType = {
|
||||
name?: string;
|
||||
slug?: string;
|
||||
establishedYear?: number;
|
||||
phone?: string;
|
||||
plan?: string;
|
||||
subscriptionId?: string;
|
||||
subscriptionEndDate?: string;
|
||||
subscriptionStartDate?: string;
|
||||
isActive?: boolean;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user