fix favorite list
This commit is contained in:
@@ -4,6 +4,8 @@ import { useRouter } from 'next/navigation'
|
|||||||
import { ArrowLeft } from 'iconsax-react'
|
import { ArrowLeft } from 'iconsax-react'
|
||||||
import Image from 'next/image'
|
import Image from 'next/image'
|
||||||
import { useGetFavorites } from './hooks/useFavoriteData'
|
import { useGetFavorites } from './hooks/useFavoriteData'
|
||||||
|
import { useGetProducts } from '@/app/[name]/(Main)/hooks/useMenuData'
|
||||||
|
import { getPrimaryVariantId } from '@/app/[name]/(Main)/types/Types'
|
||||||
import MenuItem from '@/components/listview/MenuItem'
|
import MenuItem from '@/components/listview/MenuItem'
|
||||||
import MenuItemRenderer from '@/components/listview/MenuItemRenderer'
|
import MenuItemRenderer from '@/components/listview/MenuItemRenderer'
|
||||||
import VerticalScrollView from '@/components/listview/VerticalScrollView'
|
import VerticalScrollView from '@/components/listview/VerticalScrollView'
|
||||||
@@ -13,6 +15,15 @@ import type { Product, ProductCategory, ProductVariant } from '@/app/[name]/(Mai
|
|||||||
function FavoritePage() {
|
function FavoritePage() {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { data: favoritesData, isLoading } = useGetFavorites()
|
const { data: favoritesData, isLoading } = useGetFavorites()
|
||||||
|
const { data: productsData } = useGetProducts()
|
||||||
|
const menuProducts = useMemo(() => productsData?.data ?? [], [productsData?.data])
|
||||||
|
const productById = useMemo(() => {
|
||||||
|
const map: Record<string, Product> = {}
|
||||||
|
for (const p of menuProducts) {
|
||||||
|
map[p.id] = p
|
||||||
|
}
|
||||||
|
return map
|
||||||
|
}, [menuProducts])
|
||||||
|
|
||||||
const favorites = useMemo(() => {
|
const favorites = useMemo(() => {
|
||||||
return favoritesData?.data || []
|
return favoritesData?.data || []
|
||||||
@@ -20,8 +31,11 @@ function FavoritePage() {
|
|||||||
|
|
||||||
const productItems = useMemo(() => {
|
const productItems = useMemo(() => {
|
||||||
return favorites.map((favorite: Favorite) => {
|
return favorites.map((favorite: Favorite) => {
|
||||||
const favProduct = favorite.food
|
const favProduct = favorite.product
|
||||||
// تبدیل FavoriteProduct به Product برای استفاده در MenuItem
|
const menuProduct = productById[favProduct.id]
|
||||||
|
if (menuProduct) {
|
||||||
|
return { product: menuProduct, variantId: getPrimaryVariantId(menuProduct) }
|
||||||
|
}
|
||||||
const category: ProductCategory = {
|
const category: ProductCategory = {
|
||||||
id: favProduct.category,
|
id: favProduct.category,
|
||||||
createdAt: '',
|
createdAt: '',
|
||||||
@@ -30,7 +44,7 @@ function FavoritePage() {
|
|||||||
parent: null,
|
parent: null,
|
||||||
title: '',
|
title: '',
|
||||||
isActive: true,
|
isActive: true,
|
||||||
shop: '',
|
shop: favProduct.shop,
|
||||||
avatarUrl: '',
|
avatarUrl: '',
|
||||||
order: 0,
|
order: 0,
|
||||||
}
|
}
|
||||||
@@ -41,25 +55,22 @@ function FavoritePage() {
|
|||||||
deletedAt: favProduct.deletedAt,
|
deletedAt: favProduct.deletedAt,
|
||||||
product: favProduct.id,
|
product: favProduct.id,
|
||||||
value: '',
|
value: '',
|
||||||
price: favProduct.price,
|
price: favProduct.price ?? 0,
|
||||||
}
|
}
|
||||||
const product: Product = {
|
const product: Product = {
|
||||||
id: favProduct.id,
|
id: favProduct.id,
|
||||||
createdAt: favProduct.createdAt,
|
createdAt: favProduct.createdAt,
|
||||||
updatedAt: favProduct.updatedAt,
|
updatedAt: favProduct.updatedAt,
|
||||||
deletedAt: favProduct.deletedAt,
|
deletedAt: favProduct.deletedAt,
|
||||||
shop: favProduct.restaurant,
|
shop: favProduct.shop,
|
||||||
category,
|
category,
|
||||||
attribute: '',
|
attribute: favProduct.attribute ?? '',
|
||||||
title: favProduct.title,
|
title: favProduct.title,
|
||||||
desc: favProduct.desc,
|
desc: favProduct.desc,
|
||||||
content: favProduct.content,
|
|
||||||
price: favProduct.price,
|
price: favProduct.price,
|
||||||
order: favProduct.order || 0,
|
order: favProduct.order ?? 0,
|
||||||
isActive: favProduct.isActive,
|
isActive: favProduct.isActive,
|
||||||
images: favProduct.images,
|
images: favProduct.images,
|
||||||
inPlaceServe: favProduct.inPlaceServe,
|
|
||||||
pickupServe: favProduct.pickupServe,
|
|
||||||
score: favProduct.score,
|
score: favProduct.score,
|
||||||
discount: favProduct.discount,
|
discount: favProduct.discount,
|
||||||
isSpecialOffer: favProduct.isSpecialOffer,
|
isSpecialOffer: favProduct.isSpecialOffer,
|
||||||
@@ -67,9 +78,9 @@ function FavoritePage() {
|
|||||||
description: favProduct.desc,
|
description: favProduct.desc,
|
||||||
variants: [singleVariant],
|
variants: [singleVariant],
|
||||||
}
|
}
|
||||||
return product
|
return { product, variantId: favProduct.id }
|
||||||
})
|
})
|
||||||
}, [favorites])
|
}, [favorites, productById])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='overflow-y-auto h-full noscrollbar flex flex-col'>
|
<div className='overflow-y-auto h-full noscrollbar flex flex-col'>
|
||||||
@@ -108,9 +119,9 @@ function FavoritePage() {
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<VerticalScrollView className="overflow-y-auto h-full">
|
<VerticalScrollView className="overflow-y-auto h-full">
|
||||||
{productItems.map((product) => (
|
{productItems.map(({ product, variantId }) => (
|
||||||
<MenuItemRenderer key={product.id}>
|
<MenuItemRenderer key={product.id}>
|
||||||
<MenuItem product={product} />
|
<MenuItem product={product} variantId={variantId} />
|
||||||
</MenuItemRenderer>
|
</MenuItemRenderer>
|
||||||
))}
|
))}
|
||||||
</VerticalScrollView>
|
</VerticalScrollView>
|
||||||
|
|||||||
@@ -1,29 +1,23 @@
|
|||||||
import { BaseResponse } from "@/app/[name]/(Main)/types/Types";
|
import { BaseResponse } from "@/app/[name]/(Main)/types/Types";
|
||||||
|
|
||||||
export type MealType = "breakfast" | "lunch" | "dinner" | "snack";
|
/** محصول تو در تو در پاسخ API علاقهمندیها */
|
||||||
|
|
||||||
export interface FavoriteProduct {
|
export interface FavoriteProduct {
|
||||||
id: string;
|
id: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
deletedAt: string | null;
|
deletedAt: string | null;
|
||||||
restaurant: string;
|
shop: string;
|
||||||
category: string;
|
category: string;
|
||||||
|
attribute: string | null;
|
||||||
title: string;
|
title: string;
|
||||||
desc: string;
|
desc: string;
|
||||||
content: string[];
|
|
||||||
price: number;
|
|
||||||
order: number | null;
|
order: number | null;
|
||||||
prepareTime: number | null;
|
|
||||||
weekDays: number[];
|
|
||||||
mealTypes: MealType[];
|
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
images: string[];
|
images: string[];
|
||||||
inPlaceServe: boolean;
|
|
||||||
pickupServe: boolean;
|
|
||||||
score: number;
|
score: number;
|
||||||
discount: number;
|
discount: number;
|
||||||
isSpecialOffer: boolean;
|
isSpecialOffer: boolean;
|
||||||
|
price: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Favorite {
|
export interface Favorite {
|
||||||
@@ -32,8 +26,7 @@ export interface Favorite {
|
|||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
deletedAt: string | null;
|
deletedAt: string | null;
|
||||||
user: string;
|
user: string;
|
||||||
/** API returns "food" key */
|
product: FavoriteProduct;
|
||||||
food: FavoriteProduct;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FavoritesResponse = BaseResponse<Favorite[]>;
|
export type FavoritesResponse = BaseResponse<Favorite[]>;
|
||||||
|
|||||||
Reference in New Issue
Block a user