import { useGetProductByPkQuery } from '@akinon/next/data/client/product'; import { useRemoveFavoriteMutation } from '@akinon/next/data/client/wishlist'; import { useAddProductToBasket } from '../../hooks'; import clsx from 'clsx'; import { FavoriteItem as FavoriteItemType } from '@akinon/next/types'; import { Price, Button, Icon, Select, Skeleton, SkeletonWrapper, Link } from '@theme/components'; import { useLocalization } from '@akinon/next/hooks'; import { Image } from '@akinon/next/components/image'; import { useState } from 'react'; interface Props { item: FavoriteItemType; index: number; } // item.produt is the product come from the favorites endpoint // data.product is the product come from the product endpoint // these are different because the favorites endpoint does not have the product variant and selected variants etc. export const FavoriteItem = (props: Props) => { const { t } = useLocalization(); const { item, index } = props; const [selectedProduct, setProduct] = useState(item.product); const { data, isLoading, isSuccess } = useGetProductByPkQuery( selectedProduct.pk ); const [removeFavorite, { isLoading: isRemoveFavoriteLoading }] = useRemoveFavoriteMutation(); const [addProductToBasket, { isLoading: isAddProductToBasketLoading }] = useAddProductToBasket(); const handleOnClick = (selectedProduct, favoriteProduct) => { if ( favoriteProduct.product.attributes.color === selectedProduct.attributes.color || favoriteProduct.product.pk === selectedProduct.pk ) removeFavorite(favoriteProduct.pk); addProductToBasket({ product: selectedProduct.pk, quantity: 1, attributes: {} }); }; return (
{selectedProduct.productimage_set[0] ? ( {selectedProduct.name} ) : ( {t('account.base.no_image')} )} removeFavorite(item.pk)} className={clsx( 'absolute top-4 right-4 cursor-pointer p-1 drop-shadow-[1px_2px_0px_rgb(255,255,255)]', isRemoveFavoriteLoading ? 'hover:cursor-wait' : 'hover:cursor-pointer' )} data-testid="favorites-remove" />
{selectedProduct.name}
{parseFloat(selectedProduct.retail_price) > parseFloat(selectedProduct.price) && ( )}
{isLoading && ( )} {isSuccess && (
{data.variants.map((variant, i) => { const variantOptions = variant.options .filter((option) => option.is_selectable) .map((option) => ({ is_selected: option.is_selected, label: option.label, value: option.value })); return (