'use client'; import { useEffect, useRef } from 'react'; import { useAppDispatch, useAppSelector } from '@akinon/next/redux/hooks'; import { setCurrentStep } from '@akinon/next/redux/reducers/checkout'; import { CheckoutStep } from '@akinon/next/types'; import { RootState } from '@theme/redux/store'; import PluginModule, { Component } from '@akinon/next/components/plugin-module'; import { CheckoutPiece } from '@akinon/pz-theme/src/chrome/checkout-piece'; import { CheckoutStepList } from '@theme/views/checkout/step-list'; import { Summary } from '@theme/views/checkout/summary'; import ShippingStep from '@theme/views/checkout/steps/shipping'; import PaymentStep from '@theme/views/checkout/steps/payment'; import { pushAddPaymentInfo, pushAddShippingInfo } from '@theme/utils/gtm'; import type { CheckoutVariantProps } from '../types'; const MultiStepCheckout = (_props: CheckoutVariantProps) => { const { steps, preOrder } = useAppSelector( (state: RootState) => state.checkout ); const dispatch = useAppDispatch(); const initialStepChanged = useRef(false); useEffect(() => { if (steps.shipping.completed && !initialStepChanged.current) { dispatch(setCurrentStep(CheckoutStep.Payment)); initialStepChanged.current = true; } }, [steps.shipping.completed]); // eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { if (preOrder && !preOrder.shipping_option) { initialStepChanged.current = true; } }, [preOrder]); useEffect(() => { if (!preOrder?.basket?.basketitem_set) return; const products = preOrder.basket.basketitem_set.map((item) => ({ ...item.product })); if (steps.current === CheckoutStep.Shipping) { pushAddShippingInfo(products); } if (steps.current === CheckoutStep.Payment) { pushAddPaymentInfo(products, String(preOrder?.payment_option?.name)); } }, [steps.current, preOrder?.payment_option?.name]); // eslint-disable-line react-hooks/exhaustive-deps return (
{steps.current === CheckoutStep.Shipping && } {steps.current === CheckoutStep.Payment && }
); }; export default MultiStepCheckout;