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')} {t('basket.empty.button')} ); } if (!basket) return null; const itemCount = basket.basketitem_set.length; return ( {t('checkout.one_page.basket.title')} {itemCount} {t('checkout.summary.items')} setCollapsed((prev) => !prev)} aria-expanded={!collapsed} aria-controls="basket-section-content" className="rounded-lg px-3 py-1.5 text-xs font-medium text-gray-700 transition-all hover:bg-gray-100 hover:text-black-800 focus:outline-none focus-visible:ring-2 focus-visible:ring-secondary" data-testid="basket-section-toggle" > {collapsed ? t('checkout.one_page.basket.show') : t('checkout.one_page.basket.hide')} {!collapsed && ( {multiBasket ? ( ) : ( basket.basketitem_set.map((basketItem, index) => ( )) )} )} ); }; export default BasketSection;
{t('basket.empty.content_first')}