filter category id

This commit is contained in:
hamid zarghami
2026-02-07 12:39:45 +03:30
parent 1552830e76
commit d9012e524a
5 changed files with 47 additions and 20 deletions
+10 -8
View File
@@ -174,14 +174,16 @@ const Filters: FC<FiltersProps> = ({
case 'select': case 'select':
return ( return (
<Select <div className='mt-1'>
key={field.name} <Select
placeholder={field.placeholder} key={field.name}
onChange={(e) => handleChange(field.name, e.target.value)} placeholder={field.placeholder}
defaultValue={currentValue as string || field.defaultValue || ''} onChange={(e) => handleChange(field.name, e.target.value)}
items={field.options} defaultValue={currentValue as string || field.defaultValue || ''}
className='min-w-[170px]' items={field.options}
/> className='min-w-[170px]'
/>
</div>
); );
case 'multiselect': case 'multiselect':
+3 -1
View File
@@ -19,9 +19,11 @@ const FoodsList: FC = () => {
handlePageChange, handlePageChange,
} = useFoodFilters() } = useFoodFilters()
const { data: foodsData, isLoading } = useGetFoods(apiParams) const { data: foodsData, isLoading } = useGetFoods(apiParams, filters)
const { mutate: deleteFood, isPending: isDeleting } = useDeleteFood() const { mutate: deleteFood, isPending: isDeleting } = useDeleteFood()
const foods = foodsData?.data || [] const foods = foodsData?.data || []
const columns = getFoodTableColumns({ const columns = getFoodTableColumns({
onDelete: (id: string) => deleteFood(id), onDelete: (id: string) => deleteFood(id),
@@ -1,13 +1,27 @@
import { useMemo } from 'react' import { useMemo } from 'react'
import type { FieldType } from '@/components/Filters' import type { FieldType } from '@/components/Filters'
import { useGetCategories } from '../hooks/useFoodData'
export const useFoodFiltersFields = (): FieldType[] => { export const useFoodFiltersFields = (): FieldType[] => {
const { data } = useGetCategories()
return useMemo(() => [ return useMemo(() => [
{ {
type: 'input', type: 'input',
name: 'search', name: 'search',
placeholder: 'جستجو', placeholder: 'جستجو',
}, },
], []) {
type: 'select',
name: 'categoryId',
placeholder: 'انتخاب',
options: data?.data?.map((item) => {
return {
label: item.title,
value: item.id
}
}) || []
},
], [data?.data])
} }
+7 -3
View File
@@ -5,6 +5,7 @@ import type {
CreateFoodType, CreateFoodType,
GetFoodsParams, GetFoodsParams,
} from "../types/Types"; } from "../types/Types";
import type { FilterValues } from "@/components/Filters";
export const useCreateFood = () => { export const useCreateFood = () => {
return useMutation({ return useMutation({
@@ -12,10 +13,13 @@ export const useCreateFood = () => {
}); });
}; };
export const useGetFoods = (params?: GetFoodsParams) => { export const useGetFoods = (
params?: GetFoodsParams,
filters?: FilterValues,
) => {
return useQuery({ return useQuery({
queryKey: ["foods", params], queryKey: ["foods", params, filters],
queryFn: () => api.getFoods(params), queryFn: () => api.getFoods(params, filters),
}); });
}; };
+11 -6
View File
@@ -8,6 +8,7 @@ import type {
GetFoodsResponseType, GetFoodsResponseType,
GetIconsResponseType, GetIconsResponseType,
} from "../types/Types"; } from "../types/Types";
import type { FilterValues } from "@/components/Filters";
export const createFood = async (params: CreateFoodType) => { export const createFood = async (params: CreateFoodType) => {
const { data } = await axios.post(`/admin/foods`, params); const { data } = await axios.post(`/admin/foods`, params);
@@ -15,19 +16,23 @@ export const createFood = async (params: CreateFoodType) => {
}; };
export const getFoods = async ( export const getFoods = async (
params?: GetFoodsParams params?: GetFoodsParams,
filters?: FilterValues,
): Promise<GetFoodsResponseType> => { ): Promise<GetFoodsResponseType> => {
const { data } = await axios.get<GetFoodsResponseType>(`/admin/foods`, { const { data } = await axios.get<GetFoodsResponseType>(`/admin/foods`, {
params, params: {
...params,
...filters,
},
}); });
return data; return data;
}; };
export const getFoodDetails = async ( export const getFoodDetails = async (
id: string id: string,
): Promise<GetFoodDetailsResponseType> => { ): Promise<GetFoodDetailsResponseType> => {
const { data } = await axios.get<GetFoodDetailsResponseType>( const { data } = await axios.get<GetFoodDetailsResponseType>(
`/admin/foods/${id}` `/admin/foods/${id}`,
); );
return data; return data;
}; };
@@ -39,7 +44,7 @@ export const updateFood = async (id: string, params: CreateFoodType) => {
export const getCategories = async (): Promise<GetCategoriesResponseType> => { export const getCategories = async (): Promise<GetCategoriesResponseType> => {
const { data } = await axios.get<GetCategoriesResponseType>( const { data } = await axios.get<GetCategoriesResponseType>(
`/admin/categories` `/admin/categories`,
); );
return data; return data;
}; };
@@ -61,7 +66,7 @@ export const deleteCategory = async (id: string) => {
export const updateCategory = async ( export const updateCategory = async (
id: string, id: string,
params: CreateCategoryType params: CreateCategoryType,
) => { ) => {
const { data } = await axios.patch(`/admin/categories/${id}`, params); const { data } = await axios.patch(`/admin/categories/${id}`, params);
return data; return data;