diff --git a/src/components/cart-panel/cart-item-list/index.tsx b/src/components/cart-panel/cart-item-list/index.tsx new file mode 100644 index 0000000..abd1a26 --- /dev/null +++ b/src/components/cart-panel/cart-item-list/index.tsx @@ -0,0 +1,28 @@ +import { useContext } from 'react'; + +import { CartContext } from '../../../context/cart.context'; + +import CartItem from '../../cart-item'; + +const CartItemList = () => { + const { cartItems: items } = useContext(CartContext); + + return ( + + ); +}; + +export default CartItemList; diff --git a/src/components/cart-panel/index.tsx b/src/components/cart-panel/index.tsx index ed63524..7c6ad78 100644 --- a/src/components/cart-panel/index.tsx +++ b/src/components/cart-panel/index.tsx @@ -3,19 +3,42 @@ import axios from 'axios'; import cn from 'classnames'; import { AppContext } from '../../context/app.context'; -import { CartContext, ICartContext } from '../../context/cart.context'; +import { CartContext } from '../../context/cart.context'; import { API } from '../../utils/api'; import { formatRuPrice } from '../../utils/formatRuPrice'; import { SneakersItem } from '../../interfaces/sneakers.interface'; -import CartItem from '../cart-item'; import InfoBlock from '../info-block'; import Button from '../button'; +import CartItemList from './cart-item-list'; import './styles/cart-panel.scss'; import emptyBoxSrc from '../../assets/images/empty-box.png'; import orderConfirmedSrc from '../../assets/images/order-confirmed.png'; +const renderNoItems = (isOrderConfirmed: boolean, closeCartPanel: () => void, orderData: OrderData | null, ) => { + if (isOrderConfirmed) { + return ( + + ); + } + + return ( + + ); +}; + interface CartPanelProps { isOpened: boolean, close: () => void, @@ -29,68 +52,6 @@ interface OrderData { items: SneakersItem[], } -// todo: refactor -const renderCartPanelBody = ( - items: ICartContext['cartItems'], - isOrderConfirmed: boolean, - handleCartButtonClick: () => void, - close: CartPanelProps['close'], - orderData: OrderData | null, - isLoading: boolean, - totalPrice: number -) => { - if (items.length > 0) { - return <> - -
-

- Итого: - - {formatRuPrice(totalPrice)} -

-

- Кэшбэк 5%: - - {formatRuPrice(Math.floor(totalPrice * 0.05))} -

-
- - ; - } - - if (isOrderConfirmed) { - return ; - } - - return ; -}; - const CartPanel = ({ isOpened, close }: CartPanelProps) => { const { isLoading, showLoader, hideLoader } = useContext(AppContext); const { cartItems: items, totalPrice, clearCart } = useContext(CartContext); @@ -107,7 +68,12 @@ const CartPanel = ({ isOpened, close }: CartPanelProps) => { clearCart(); setOrderData(data); - setTimeout(close, 10000); + setTimeout(() => { + close(); + setTimeout(() => { + setIsOrderConfirmed(false); + }, 1000); + }, 10000); } catch (err) { alert('Не удалось оформить заказ :('); console.error(err); @@ -120,7 +86,30 @@ const CartPanel = ({ isOpened, close }: CartPanelProps) => {
evt.stopPropagation()}>

Корзина

- {renderCartPanelBody(items, isOrderConfirmed, handleCartButtonClick, close, orderData, isLoading, totalPrice)} + + {items.length > 0 + ? ( + <> + +
+

+ Итого: + + {formatRuPrice(totalPrice)} +

+

+ Кэшбэк 5%: + + {formatRuPrice(Math.floor(totalPrice * 0.05))} +

+
+ + + ) + : renderNoItems(isOrderConfirmed, close, orderData) + }
);