import { useEffect, useRef, useState } from 'react'; import { useGetBasketQuery } from '@akinon/next/data/client/basket'; import { checkoutApi } from '@akinon/next/data/client/checkout'; import { useAppDispatch } from '@akinon/next/redux/hooks'; import { useLocalization } from '@akinon/next/hooks'; import PluginModule, { Component } from '@akinon/next/components/plugin-module'; import { BasketItem } from '@theme/views/basket'; import { Icon, Link, Button, LoaderSpinner } from '@theme/components'; import { ROUTES } from '@theme/routes'; import settings from '@theme/settings'; const BasketSection = () => { const { t } = useLocalization(); const dispatch = useAppDispatch(); const { data: basket, isLoading, isSuccess } = useGetBasketQuery(); const [collapsed, setCollapsed] = useState(false); const multiBasket = (settings as { plugins?: { multiBasket?: boolean } }) ?.plugins?.multiBasket ?? false; const totalQuantity = basket?.total_quantity ?? 0; const totalAmount = basket?.total_amount ?? '0'; const initialSnapshotRef = useRef<{ qty: number; amount: string; } | null>(null); useEffect(() => { if (!isSuccess) return; if (initialSnapshotRef.current === null) { initialSnapshotRef.current = { qty: totalQuantity, amount: totalAmount }; return; } const prev = initialSnapshotRef.current; if (prev.qty !== totalQuantity || prev.amount !== totalAmount) { initialSnapshotRef.current = { qty: totalQuantity, amount: totalAmount }; dispatch(checkoutApi.util.invalidateTags(['Checkout'])); } }, [isSuccess, totalQuantity, totalAmount, dispatch]); if (isLoading) { return (
); } if ( isSuccess && (!basket?.basketitem_set || basket.basketitem_set.length === 0) ) { return (

{t('basket.empty.title')}

{t('basket.empty.content_first')}

); } if (!basket) return null; const itemCount = basket.basketitem_set.length; return (

{t('checkout.one_page.basket.title')}

{itemCount} {t('checkout.summary.items')}
{!collapsed && (
)}
); }; export default BasketSection;