11"use client" ;
22
3- import { COUNTRY_SELECT_ITEMS } from "@/components/checkout/checkout.constants" ;
3+ import { useRouter } from "next/navigation" ;
4+ import { useEffect } from "react" ;
5+ import type { CheckoutStepSlug } from "@/components/checkout/checkout.constants" ;
6+ import { CheckoutStepContent } from "@/components/checkout/checkout-step-content" ;
7+ import {
8+ canAccessCheckoutStep ,
9+ resolveCheckoutStepHref ,
10+ resolveCheckoutStepIndexBySlug ,
11+ resolveRequiredCheckoutStepSlug ,
12+ } from "@/components/checkout/checkout-route.utils" ;
413import { useCheckoutController } from "@/components/checkout/use-checkout-controller" ;
5- import { CheckoutAddressSection } from "@/components/checkout/sections/checkout-address-section" ;
6- import { CheckoutCompleteSection } from "@/components/checkout/sections/checkout-complete-section" ;
714import { CheckoutCompletedOrderSection } from "@/components/checkout/sections/checkout-completed-order-section" ;
815import { CheckoutEmptyCartSection } from "@/components/checkout/sections/checkout-empty-cart-section" ;
916import { CheckoutFeedbackSection } from "@/components/checkout/sections/checkout-feedback-section" ;
10- import { CheckoutOrderSummarySection } from "@/components/checkout/sections/checkout-order-summary-section" ;
11- import { CheckoutPaymentSection } from "@/components/checkout/sections/checkout-payment-section" ;
12- import { CheckoutShippingSection } from "@/components/checkout/sections/checkout-shipping-section" ;
1317import { CheckoutStepsSection } from "@/components/checkout/sections/checkout-steps-section" ;
1418
15- export function StorefrontCheckoutFlow ( ) {
19+ type StorefrontCheckoutFlowProps = {
20+ activeStep : CheckoutStepSlug ;
21+ } ;
22+
23+ export function StorefrontCheckoutFlow ( {
24+ activeStep,
25+ } : StorefrontCheckoutFlowProps ) {
26+ const router = useRouter ( ) ;
1627 const controller = useCheckoutController ( ) ;
28+ const requiredStep = resolveRequiredCheckoutStepSlug ( {
29+ hasItems : controller . hasItems ,
30+ hasPayment : controller . hasPayment ,
31+ hasShipping : controller . hasShipping ,
32+ hasStoredAddress : controller . hasStoredAddress ,
33+ } ) ;
34+ const redirectStep =
35+ requiredStep === "kosik" && activeStep !== "kosik"
36+ ? "doprava-platba"
37+ : requiredStep ;
1738
18- return (
19- < main className = "mx-auto flex w-full max-w-max-w flex-col gap-500 px-400 py-550 lg:px-550" >
20- < header className = "space-y-200" >
21- < h1 className = "text-2xl font-semibold text-fg-primary" > Dokončenie objednávky</ h1 >
22- < p className = "text-sm text-fg-secondary" >
23- Vyplňte údaje, zvoľte dopravu a platbu, potom potvrďte objednávku.
24- </ p >
25- </ header >
39+ const canAccessStep = canAccessCheckoutStep ( {
40+ requestedStep : activeStep ,
41+ hasItems : controller . hasItems ,
42+ hasPayment : controller . hasPayment ,
43+ hasShipping : controller . hasShipping ,
44+ hasStoredAddress : controller . hasStoredAddress ,
45+ } ) ;
46+
47+ const isStepGateLoading =
48+ controller . cartQuery . isLoading || controller . cartQuery . isFetching ;
49+ const hasResolvedCart = typeof controller . cartQuery . cart !== "undefined" ;
50+ const shouldRedirectStep =
51+ hasResolvedCart &&
52+ ! isStepGateLoading &&
53+ ! canAccessStep &&
54+ ! controller . completedOrderId &&
55+ redirectStep !== activeStep ;
56+
57+ useEffect ( ( ) => {
58+ if ( ! shouldRedirectStep ) {
59+ return ;
60+ }
61+
62+ router . replace ( resolveCheckoutStepHref ( redirectStep ) ) ;
63+ } , [ redirectStep , router , shouldRedirectStep ] ) ;
64+
65+ if ( shouldRedirectStep ) {
66+ return < main className = "mx-auto min-h-dvh w-full max-w-max-w" /> ;
67+ }
2668
69+ const activeStepIndex = resolveCheckoutStepIndexBySlug ( activeStep ) ;
70+
71+ return (
72+ < main className = "checkout-main mx-auto flex w-full flex-col gap-750 px-400 pt-700 pb-850 lg:px-550" >
2773 < CheckoutStepsSection
28- checkoutStepIndex = { controller . checkoutStepIndex }
74+ checkoutStepIndex = { activeStepIndex }
2975 steps = { controller . checkoutSteps }
3076 />
3177
@@ -42,66 +88,7 @@ export function StorefrontCheckoutFlow() {
4288 { ! controller . completedOrderId && ! controller . hasItems ? < CheckoutEmptyCartSection /> : null }
4389
4490 { ! controller . completedOrderId && controller . hasItems ? (
45- < div className = "grid gap-500 xl:grid-cols-3" >
46- < div className = "space-y-350 xl:col-span-2" >
47- < CheckoutAddressSection
48- acceptTermsConsent = { controller . acceptTermsConsent }
49- addressForm = { controller . addressForm }
50- countryItems = { COUNTRY_SELECT_ITEMS }
51- createAccountConsent = { controller . createAccountConsent }
52- hasStoredAddress = { controller . hasStoredAddress }
53- isBusy = { controller . isBusy }
54- isSavingAddress = { controller . updateCartAddressMutation . isPending }
55- onAcceptTermsConsentChange = { controller . setAcceptTermsConsent }
56- onCreateAccountConsentChange = { controller . setCreateAccountConsent }
57- onSaveAddress = { controller . handleSaveAddress }
58- onUpdateAddressField = { controller . updateAddressField }
59- ready = { Boolean ( controller . cartQuery . cart ?. id ) }
60- />
61-
62- < CheckoutShippingSection
63- currencyCode = { controller . currencyCode }
64- hasShipping = { controller . hasShipping }
65- isBusy = { controller . isBusy }
66- onSelectShipping = { controller . handleSelectShipping }
67- selectedShippingMethodId = { controller . checkoutShippingQuery . selectedShippingMethodId }
68- shippingOptions = { controller . checkoutShippingQuery . shippingOptions }
69- shippingPrices = { controller . checkoutShippingQuery . shippingPrices }
70- />
71-
72- < CheckoutPaymentSection
73- canInitiatePayment = { controller . checkoutPaymentQuery . canInitiatePayment }
74- hasPayment = { controller . hasPayment }
75- isBusy = { controller . isBusy }
76- isInitiatingPayment = { controller . checkoutPaymentQuery . isInitiatingPayment }
77- onSelectPaymentProvider = { controller . handleSelectPaymentProvider }
78- paymentProviders = { controller . checkoutPaymentQuery . paymentProviders }
79- />
80-
81- < CheckoutCompleteSection
82- acceptTermsConsent = { controller . acceptTermsConsent }
83- canCompleteOrder = { controller . canCompleteOrder }
84- hasPayment = { controller . hasPayment }
85- hasShipping = { controller . hasShipping }
86- hasStoredAddress = { controller . hasStoredAddress }
87- isCompletingOrder = { controller . completeCartMutation . isPending }
88- onCompleteOrder = { controller . handleCompleteOrder }
89- />
90- </ div >
91-
92- < aside className = "space-y-300 xl:sticky xl:top-400 xl:self-start" >
93- < CheckoutOrderSummarySection
94- cartItems = { controller . cartItems }
95- cartSubtotalAmount = { controller . cartSubtotalAmount }
96- cartTotalAmount = { controller . cartTotalAmount }
97- currencyCode = { controller . currencyCode }
98- hasPayment = { controller . hasPayment }
99- hasShipping = { controller . hasShipping }
100- selectedOptionName = { controller . checkoutShippingQuery . selectedOption ?. name ?? undefined }
101- selectedShippingPrice = { controller . selectedShippingPrice }
102- />
103- </ aside >
104- </ div >
91+ < CheckoutStepContent activeStep = { activeStep } controller = { controller } />
10592 ) : null }
10693 </ main >
10794 ) ;
0 commit comments