-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
170 lines (149 loc) · 6.97 KB
/
App.tsx
File metadata and controls
170 lines (149 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React, { useState, useEffect } from 'react';
import { Order, CartItem, Product } from './types';
import { generateAndPredictCart } from './services/geminiService';
import SmartCart from './components/SmartCart';
import OrderSuccess from './components/OrderSuccess';
import HomeScreen from './components/HomeScreen';
import Toast from './components/Toast';
import { ChevronLeftIcon } from './components/Icons';
type View = 'home' | 'smartCart' | 'orderSuccess';
const App: React.FC = () => {
const [view, setView] = useState<View>('home');
const [cartItems, setCartItems] = useState<CartItem[]>([]);
const [pastOrders, setPastOrders] = useState<Order[]>([]);
// Loading state for initial data fetch
const [isLoading, setIsLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);
const [finalOrder, setFinalOrder] = useState<{ items: CartItem[], total: number } | null>(null);
const [toastMessage, setToastMessage] = useState<string>('');
// Load initial data
useEffect(() => {
const fetchCartData = async () => {
setIsLoading(true);
setError(null);
try {
const data = await generateAndPredictCart();
// Ensure initial items are marked as source: 'ai'
const taggedItems = data.predictedCart.map(item => ({
...item,
source: 'ai' as const
}));
setCartItems(taggedItems);
setPastOrders(data.pastOrders);
} catch (err) {
if (err instanceof Error) {
setError(`Failed to load smart cart. ${err.message}. Please check if your API key is configured correctly.`);
} else {
setError("An unknown error occurred.");
}
} finally {
setIsLoading(false);
}
};
fetchCartData();
}, []);
const showToast = (message: string) => {
setToastMessage(message);
};
const handleViewSmartCart = () => {
setView('smartCart');
};
const handleBackToHome = () => {
setView('home');
};
// Centralized Cart Logic
const handleUpdateCart = (newItems: CartItem[]) => {
setCartItems(newItems);
};
const handleAddItemToCart = (itemToAdd: Product) => {
setCartItems(currentItems => {
const existingItem = currentItems.find(item => item.id === itemToAdd.id);
if (existingItem) {
return currentItems.map(item =>
item.id === itemToAdd.id ? { ...item, quantity: item.quantity + 1, doNotRepeat: false } : item
);
} else {
return [...currentItems, {
...itemToAdd,
quantity: 1,
doNotRepeat: false,
status: 'AVAILABLE',
source: 'manual'
}];
}
});
};
const handleCheckout = (cart: CartItem[], total: number) => {
setFinalOrder({ items: cart, total });
setView('orderSuccess');
};
const handleNewOrder = () => {
setFinalOrder(null);
// Reset or Fetch new cart logic here if needed, for now navigate home
setView('home');
};
const renderContent = () => {
switch (view) {
case 'home':
return <HomeScreen
onViewSmartCart={handleViewSmartCart}
isLoading={isLoading}
error={error}
currentCart={cartItems}
pastOrders={pastOrders}
onAddItem={handleAddItemToCart}
showToast={showToast}
/>;
case 'smartCart':
return (
<div className="w-full h-full">
<SmartCart
cartItems={cartItems}
pastOrders={pastOrders}
onUpdateCart={handleUpdateCart}
onCheckout={handleCheckout}
/>
</div>
);
case 'orderSuccess':
return finalOrder && <OrderSuccess order={finalOrder} onNewOrder={handleNewOrder} />;
default:
return null;
}
};
return (
<div className="min-h-screen bg-slate-50 flex flex-col items-center p-0 sm:p-6 lg:p-8 font-sans">
<style>{`
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');
body { font-family: 'Inter', sans-serif; }
.animate-fade-in { animation: fade-in 0.3s ease-out forwards; }
@keyframes fade-in { 0% { opacity: 0; } 100% { opacity: 1; } }
.animate-fade-in-up { animation: fade-in-up 0.5s ease-out forwards; }
@keyframes fade-in-up { 0% { opacity: 0; transform: translateY(20px); } 100% { opacity: 1; transform: translateY(0); } }
.animate-slide-in-up { animation: slide-in-up 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards; }
@keyframes slide-in-up { 0% { transform: translateY(100%); } 100% { transform: translateY(0); } }
.animate-toast { animation: toast-in-out 3s ease-in-out forwards; }
@keyframes toast-in-out { 0% { bottom: -100px; opacity: 0; } 10% { bottom: 24px; opacity: 1; } 90% { bottom: 24px; opacity: 1; } 100% { bottom: -100px; opacity: 0; } }
.confetti { position: absolute; width: 8px; height: 8px; background-color: #facc15; opacity: 0.7; animation: confetti-fall 2s ease-out forwards; }
@keyframes confetti-fall { 0% { transform: translateY(-10vh) rotate(0deg); opacity: 1; } 100% { transform: translateY(100vh) rotate(720deg); opacity: 0; } }
`}</style>
{view === 'smartCart' && (
<header className="mb-4 w-full max-w-4xl mx-auto pt-4 sm:pt-0 px-4 sm:px-0">
<div className="relative text-center">
<button onClick={handleBackToHome} className="absolute left-0 top-1/2 -translate-y-1/2 p-2 rounded-full hover:bg-slate-200 transition-colors">
<ChevronLeftIcon className="w-6 h-6 text-slate-600" />
</button>
<div>
<h1 className="text-3xl sm:text-4xl font-extrabold text-slate-800 tracking-tight">Your Smart Cart</h1>
</div>
</div>
</header>
)}
<main className="w-full max-w-4xl mx-auto flex-grow">
{renderContent()}
</main>
<Toast message={toastMessage} onDismiss={() => setToastMessage('')} />
</div>
);
};
export default App;