Skip to content

Commit 3d2eb78

Browse files
pass location state
1 parent 1dbfe36 commit 3d2eb78

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

src/pages/AdvancedMode/OrderPage/order.jsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useEffect } from 'react';
22
import { BeakerIcon, XCircle, PlayCircle } from 'lucide-react';
3-
import { Navigate, useLocation, useNavigate } from '@tanstack/react-router';
3+
import { useLocation, useNavigate } from '@tanstack/react-router';
44
import useAuthStore from '../../../store/authStore';
55
import cocktailService from '../../../services/cocktail.service';
66
import DrinkCustomizer from './components/DrinkCustomizer';
@@ -140,8 +140,16 @@ const Order = () => {
140140
setAmountToProduce(value);
141141
};
142142

143-
if (!token) return <Navigate to="/login" />;
144-
if (!recipe) return <Navigate to="/drinks" />;
143+
// Handle redirects in useEffect to prevent infinite loops
144+
useEffect(() => {
145+
if (!token) {
146+
navigate({ to: '/login' });
147+
} else if (!recipe) {
148+
navigate({ to: '/drinks' });
149+
}
150+
}, [token, recipe, navigate]);
151+
152+
if (!token || !recipe) return null;
145153

146154
const canOrderDrink =
147155
feasibilityResult?.feasible &&

src/pages/simple-mode/simpleDrinkDetail/simpleDrinkDetail.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { useLocation, useNavigate, Navigate } from '@tanstack/react-router';
1+
import { useLocation, useNavigate } from '@tanstack/react-router';
2+
import { useEffect } from 'react';
23
import { Beaker, ArrowLeft } from 'lucide-react';
34
import { Button } from '@/components/ui/button';
45
import { Badge } from '@/components/ui/badge';
@@ -29,12 +30,18 @@ interface Recipe {
2930

3031
const SimpleDrinkDetail = () => {
3132
const location = useLocation();
32-
const navigate = useNavigate({ from: '/simple/drink/$id' });
33+
const navigate = useNavigate();
3334
const recipe = (location.state as any)?.recipe as Recipe | undefined;
3435

3536
// Redirect if no recipe data
37+
useEffect(() => {
38+
if (!recipe) {
39+
navigate({ to: '/simple/drinks' });
40+
}
41+
}, [recipe, navigate]);
42+
3643
if (!recipe) {
37-
return <Navigate to="/simple/drinks" />;
44+
return null;
3845
}
3946

4047
return (
@@ -73,7 +80,12 @@ const SimpleDrinkDetail = () => {
7380
type="button"
7481
size="lg"
7582
className="w-full h-12 sm:h-14 gap-2 sm:gap-3 text-sm sm:text-base font-semibold shadow-lg hover:shadow-xl transition-all duration-200 active:scale-95"
76-
onClick={() => navigate({ to: '/simple/order' } as any)}
83+
onClick={() => {
84+
navigate({
85+
to: '/simple/order',
86+
state: { recipe }
87+
} as any);
88+
}}
7789
>
7890
<Beaker className="w-4 h-4 sm:w-5 sm:h-5" />
7991
Make Drink

src/pages/simple-mode/simpleOrder/simpleOrder.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, useEffect, useCallback } from 'react';
22
import { BeakerIcon, ArrowLeft, Info } from 'lucide-react';
3-
import { Navigate, useLocation, useNavigate } from '@tanstack/react-router';
3+
import { useLocation, useNavigate } from '@tanstack/react-router';
44
import { Button } from '@/components/ui/button';
55
import { Badge } from '@/components/ui/badge';
66
import { Card, CardContent } from '@/components/ui/card';
@@ -164,8 +164,16 @@ const SimpleOrder = () => {
164164
orderDrink(recipe.id, orderConfig);
165165
}, [recipe, amountToProduce, boost, additionalIngredients, token]);
166166

167-
if (!token) return <Navigate to="/login" />;
168-
if (!recipe) return <Navigate to="/drinks" />;
167+
// Handle redirects in useEffect to prevent infinite loops
168+
useEffect(() => {
169+
if (!token) {
170+
navigate({ to: '/login' });
171+
} else if (!recipe) {
172+
navigate({ to: '/drinks' });
173+
}
174+
}, [token, recipe, navigate]);
175+
176+
if (!token || !recipe) return null;
169177

170178
const canOrderDrink =
171179
feasibilityResult?.feasible &&

0 commit comments

Comments
 (0)