Files
dashboards/apps/customer/src/pages/checkout/CheckoutFlow.tsx
T
Alireza HassaniandCursor cc94b929e0 Add e-payment UI, checkout payment result, and legacy migrate domain lookup.
Wire Mellat settings in business dashboard, customer bank return page with payment details, and super-admin migrate modal domain confirmation.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-13 09:51:22 +03:30

67 lines
2.1 KiB
TypeScript

import { Navigate, Outlet, useLocation } from 'react-router-dom'
import { RouteLoader } from '@meshkee/dashboard-ui'
import { useAuth } from '../../context/AuthContext'
import { CheckoutStepper, type CheckoutStepId } from '../../components/checkout/CheckoutStepper'
import { loginPathWithRedirect, safeRedirectPath } from '../../lib/redirect'
/** Cart and checkout login are public. Delivery, payment, and result require auth. */
const PUBLIC_STEPS = new Set<CheckoutStepId>(['cart', 'login'])
function stepFromPath(pathname: string): CheckoutStepId {
if (pathname.endsWith('/login')) return 'login'
if (pathname.endsWith('/delivery')) return 'delivery'
if (pathname.endsWith('/payment')) return 'payment'
if (
pathname.endsWith('/success') ||
pathname.endsWith('/failed') ||
pathname.endsWith('/result')
) {
return 'payment'
}
return 'cart'
}
export function CheckoutFlow() {
const { user, isLoading } = useAuth()
const location = useLocation()
const step = stepFromPath(location.pathname)
const isAuthenticated = Boolean(user)
if (isLoading) {
return <RouteLoader />
}
if (!isAuthenticated && !PUBLIC_STEPS.has(step)) {
const returnTo = `${location.pathname}${location.search}`
return (
<Navigate
to={loginPathWithRedirect(returnTo, '/checkout/login')}
replace
state={{ from: returnTo }}
/>
)
}
if (isAuthenticated && step === 'login') {
const params = new URLSearchParams(location.search)
const fromQuery = safeRedirectPath(params.get('redirect'), '')
const fromState = (location.state as { from?: string } | null)?.from
const destination = fromQuery || fromState || '/checkout/delivery'
return <Navigate to={destination} replace />
}
const hideStepper =
location.pathname === '/checkout/success' ||
location.pathname === '/checkout/failed' ||
location.pathname === '/checkout/result'
return (
<>
{step !== 'login' && !hideStepper && (
<CheckoutStepper current={step} isAuthenticated={isAuthenticated} />
)}
<Outlet />
</>
)
}