From bf2367ace80b65b0551a3f88e21f74e6fefc1136 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Wed, 26 Nov 2025 10:49:39 +0330 Subject: [PATCH] change category ids to category id --- src/pages/food/Create.tsx | 12 ++--- src/pages/food/Update.tsx | 20 ++++----- .../food/components/BasicInfoSection.tsx | 8 ++-- src/pages/food/types/Types.ts | 45 ++++++++++++++++++- 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/pages/food/Create.tsx b/src/pages/food/Create.tsx index ca09674..63802d1 100644 --- a/src/pages/food/Create.tsx +++ b/src/pages/food/Create.tsx @@ -36,7 +36,7 @@ const CreateFood: FC = () => { title: '', desc: '', content: [], - categoryIds: [], + categoryId: '', price: 0, discount: 0, points: 0, @@ -61,7 +61,7 @@ const CreateFood: FC = () => { validationSchema: Yup.object().shape({ title: Yup.string().required('نام غذا الزامی است'), desc: Yup.string().required('توضیحات غذا الزامی است'), - categoryIds: Yup.array().min(1, 'حداقل یک دسته‌بندی الزامی است'), + categoryId: Yup.string().required('دسته‌بندی الزامی است'), price: Yup.number().required('قیمت الزامی است').min(0, 'قیمت باید مثبت باشد'), prepareTime: Yup.number().required('زمان آماده‌سازی الزامی است').min(0, 'زمان آماده‌سازی باید مثبت باشد'), stock: Yup.number().required('موجودی الزامی است').min(0, 'موجودی باید مثبت باشد'), @@ -143,13 +143,13 @@ const CreateFood: FC = () => { items={categories} label='دسته بندی' placeholder='انتخاب کنید' - name='categoryIds' - value={formik.values.categoryIds[0] || ''} + name='categoryId' + value={formik.values.categoryId} onChange={(e) => { const value = e.target.value - formik.setFieldValue('categoryIds', value ? [value] : []) + formik.setFieldValue('categoryId', value) }} - error_text={formik.touched.categoryIds && formik.errors.categoryIds ? String(formik.errors.categoryIds) : ''} + error_text={formik.touched.categoryId && formik.errors.categoryId ? String(formik.errors.categoryId) : ''} /> diff --git a/src/pages/food/Update.tsx b/src/pages/food/Update.tsx index 709bbbc..e15179b 100644 --- a/src/pages/food/Update.tsx +++ b/src/pages/food/Update.tsx @@ -3,7 +3,7 @@ import { useFormik } from 'formik' import * as Yup from 'yup' import { TickCircle } from 'iconsax-react' import { toast } from 'react-toastify' -import { useParams } from 'react-router-dom' +import { useNavigate, useParams } from 'react-router-dom' import Button from '@/components/Button' import CreateFoodSidebar from './components/CreateFoodSidebar' import BasicInfoSection from './components/BasicInfoSection' @@ -28,7 +28,7 @@ const UpdateFood: FC = () => { const [existingImages, setExistingImages] = useState([]) const { mutate: updateFood, isPending } = useUpdateFood() const { mutate: multipleUpload, isPending: isUploading } = useMultipleUpload() - + const navigate = useNavigate() const categories = categoriesData?.data?.map(category => ({ label: category.title, value: category.id @@ -41,7 +41,7 @@ const UpdateFood: FC = () => { title: '', desc: '', content: [], - categoryIds: [], + categoryId: '', price: 0, discount: 0, points: 0, @@ -66,7 +66,7 @@ const UpdateFood: FC = () => { validationSchema: Yup.object().shape({ title: Yup.string().required('نام غذا الزامی است'), desc: Yup.string().required('توضیحات غذا الزامی است'), - categoryIds: Yup.array().min(1, 'حداقل یک دسته‌بندی الزامی است'), + categoryId: Yup.string().required('دسته‌بندی الزامی است'), price: Yup.number().required('قیمت الزامی است').min(0, 'قیمت باید مثبت باشد'), prepareTime: Yup.number().required('زمان آماده‌سازی الزامی است').min(0, 'زمان آماده‌سازی باید مثبت باشد'), stock: Yup.number().required('موجودی الزامی است').min(0, 'موجودی باید مثبت باشد'), @@ -93,7 +93,7 @@ const UpdateFood: FC = () => { updateFood({ id, params: foodData }, { onSuccess: () => { toast.success('غذا با موفقیت به‌روزرسانی شد') - window.location.href = Pages.foods.list + navigate(Pages.foods.list) }, onError: (error: ErrorType) => { toast.error(extractErrorMessage(error)) @@ -123,7 +123,7 @@ const UpdateFood: FC = () => { title: food.title || '', desc: food.desc || '', content: food.content || [], - categoryIds: food.categories?.map(cat => cat.id) || [], + categoryId: food.category?.id || '', price: food.price || 0, discount: food.discount || 0, points: food.points || 0, @@ -134,10 +134,10 @@ const UpdateFood: FC = () => { noon: food.noon || false, dinner: food.dinner || false, mon: food.mon || false, - tue: food.tue || false, - wed: food.wed || false, - thu: food.thu || false, - fri: food.fri || false, + tue: false, + wed: false, + thu: false, + fri: false, sat: food.sat || false, sun: food.sun || false, pickupServe: food.pickupServe || false, diff --git a/src/pages/food/components/BasicInfoSection.tsx b/src/pages/food/components/BasicInfoSection.tsx index 59adbe9..b3d8ed7 100644 --- a/src/pages/food/components/BasicInfoSection.tsx +++ b/src/pages/food/components/BasicInfoSection.tsx @@ -27,13 +27,13 @@ const BasicInfoSection: FC = ({ formik, categories }) => items={categories} label='دسته بندی' placeholder='انتخاب کنید' - name='categoryIds' - value={formik.values.categoryIds[0] || ''} + name='categoryId' + value={formik.values.categoryId} onChange={(e) => { const value = e.target.value - formik.setFieldValue('categoryIds', value ? [value] : []) + formik.setFieldValue('categoryId', value) }} - error_text={formik.touched.categoryIds && formik.errors.categoryIds ? String(formik.errors.categoryIds) : ''} + error_text={formik.touched.categoryId && formik.errors.categoryId ? String(formik.errors.categoryId) : ''} /> diff --git a/src/pages/food/types/Types.ts b/src/pages/food/types/Types.ts index 53d4513..c361ad5 100644 --- a/src/pages/food/types/Types.ts +++ b/src/pages/food/types/Types.ts @@ -24,7 +24,7 @@ export type CreateFoodType = { inPlaceServe: boolean; pickupServe: boolean; discount: number; - categoryIds: string[]; + categoryId: string; }; export type Category = { @@ -39,6 +39,17 @@ export type Category = { order: number | null; }; +export type FoodDetailsCategory = { + id: string; + createdAt: string; + updatedAt: string; + deletedAt: string | null; + title: string; + isActive: boolean; + restaurant: string; + avatarUrl: string | null; +}; + export type Food = { id: string; createdAt: string; @@ -82,9 +93,39 @@ export type GetFoodsParams = { isActive?: boolean; }; +export type FoodDetails = { + id: string; + createdAt: string; + updatedAt: string; + deletedAt: string | null; + restaurant: string; + category: FoodDetailsCategory; + title: string; + desc: string | null; + content: string[]; + price: number; + points: number; + order: number | null; + prepareTime: number; + sat: boolean; + sun: boolean; + mon: boolean; + breakfast: boolean; + noon: boolean; + dinner: boolean; + stock: number; + stockDefault: number; + isActive: boolean; + images: string[]; + inPlaceServe: boolean; + pickupServe: boolean; + rate: number; + discount: number; +}; + export type GetCategoriesResponseType = IResponse; export type GetFoodsResponseType = IResponse; -export type GetFoodDetailsResponseType = IResponse; +export type GetFoodDetailsResponseType = IResponse; export type CreateCategoryType = { title: string;