Fix calculator state persistence after browser back from Stripe
All checks were successful
Build and Push Docker Images / docker (push) Successful in 48s

Root causes fixed:
1. State only initialized on mount, not on navigation - Added useEffect to restore offset state when navigating to calculator page
2. CheckoutCancel auto-redirected too quickly (100ms) - Removed auto-redirect, user manually clicks Try Again
3. No restoration logic existed - New useEffect watches currentPage and savedState, restores when showOffsetOrder=true

Changes:
- App.tsx: Added restoration useEffect that triggers when currentPage='calculator' and savedState has offset state
- App.tsx: Added console logs for debugging state restoration
- CheckoutCancel.tsx: Removed auto-redirect useEffect and useEffect import
- CheckoutSuccess.tsx: Already correctly clears state on successful payment (verified)

User flow now:
1. Fill calculator → proceeds to offset order (state saved)
2. Click Proceed to Payment → Stripe checkout
3. Click browser back → lands on cancel page
4. Click Try Again → navigates to calculator
5. App detects savedState.showOffsetOrder=true → restores offset order screen with preserved data
This commit is contained in:
Matt 2025-10-30 16:48:53 +01:00
parent 569cf84cde
commit cb9c9f8291
2 changed files with 18 additions and 10 deletions

View File

@ -90,6 +90,22 @@ function App() {
return () => window.removeEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll);
}, [lastScrollY]); }, [lastScrollY]);
// Restore offset order state when navigating to calculator page
// This handles browser back from Stripe checkout
useEffect(() => {
if (currentPage === 'calculator' && savedState && savedState.showOffsetOrder) {
console.log('[State Restoration] Restoring offset order state from localStorage');
console.log('[State Restoration] Offset tons:', savedState.offsetTons);
console.log('[State Restoration] Monetary amount:', savedState.monetaryAmount);
console.log('[State Restoration] Calculator type:', savedState.calculatorTypeUsed);
setShowOffsetOrder(true);
setOffsetTons(savedState.offsetTons || 0);
setMonetaryAmount(savedState.monetaryAmount);
setCalculatorType(savedState.calculatorTypeUsed || 'trip');
}
}, [currentPage, savedState]);
const handleSearch = async (imo: string) => { const handleSearch = async (imo: string) => {
setLoading(true); setLoading(true);
setError(null); setError(null);

View File

@ -1,5 +1,4 @@
import { motion } from 'framer-motion'; import { motion } from 'framer-motion';
import { useEffect } from 'react';
interface CheckoutCancelProps { interface CheckoutCancelProps {
onNavigateHome: () => void; onNavigateHome: () => void;
@ -10,15 +9,8 @@ export default function CheckoutCancel({
onNavigateHome, onNavigateHome,
onNavigateCalculator onNavigateCalculator
}: CheckoutCancelProps) { }: CheckoutCancelProps) {
// Automatically redirect to calculator on mount // Note: Removed auto-redirect to allow offset order state restoration to work
useEffect(() => { // User can manually click "Try Again" to return to calculator
// Small delay to ensure smooth transition
const timer = setTimeout(() => {
onNavigateCalculator();
}, 100);
return () => clearTimeout(timer);
}, [onNavigateCalculator]);
return ( return (
<div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50 to-cyan-50 flex items-center justify-center p-6"> <div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50 to-cyan-50 flex items-center justify-center p-6">