-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
277 lines (248 loc) · 11.1 KB
/
Copy pathApp.tsx
File metadata and controls
277 lines (248 loc) · 11.1 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import React, { useState, useEffect, useMemo } from 'react';
import { UserRole, ServiceCategory, ServiceDetail, MarketplaceJob, MarketplaceBid, Booking, MamaWallet, User } from './types';
import { Layout } from './components/Layout';
import { HeroAI } from './components/HeroAI';
import { CustomerDashboard } from './components/CustomerDashboard';
import { MamaDashboard } from './components/MamaDashboard';
import { SubscriptionPage } from './components/SubscriptionPage';
import { ServiceDetailsPage } from './components/ServiceDetailsPage';
import { LoginPage } from './components/LoginPage';
import { ProfilePage } from './components/ProfilePage';
import { ServiceExplorer } from './components/ServiceExplorer';
const App: React.FC = () => {
const [user, setUser] = useState<User | null>(null);
const [view, setView] = useState<'home' | 'subscriptions' | 'service-details' | 'login' | 'dashboard' | 'profile'>('home');
const [selectedService, setSelectedService] = useState<ServiceDetail | null>(null);
const [recommendedCategory, setRecommendedCategory] = useState<ServiceCategory | null>(null);
const [aiResponse, setAiResponse] = useState<any>(null);
const [marketplaceJobs, setMarketplaceJobs] = useState<MarketplaceJob[]>([]);
const [marketplaceBids, setMarketplaceBids] = useState<MarketplaceBid[]>([]);
const [directBookings, setDirectBookings] = useState<Booking[]>([]);
const [mamaWallet, setMamaWallet] = useState<MamaWallet>({
balance: 84500,
totalWithdrawn: 120000,
pendingPayout: 0,
transactions: [
{ id: 'tx_1', amount: 2200, type: 'credit', description: 'AC Service Payout', date: new Date().toISOString() }
]
});
const [isMamaPremium, setIsMamaPremium] = useState(false);
const MAMA_ID = user?.id || 'mama_current';
const handleWithdraw = (amount: number) => {
if (amount <= 0 || amount > mamaWallet.balance) return;
setMamaWallet(prev => ({
...prev,
balance: prev.balance - amount,
totalWithdrawn: prev.totalWithdrawn + amount,
transactions: [{ id: 'tx_' + Date.now(), amount, type: 'debit', description: 'Withdrawal', date: new Date().toISOString() }, ...prev.transactions]
}));
};
useEffect(() => {
const savedJobs = localStorage.getItem('expert_mama_jobs');
const savedBids = localStorage.getItem('expert_mama_bids');
const savedBookings = localStorage.getItem('expert_mama_bookings');
const savedWallet = localStorage.getItem('expert_mama_wallet');
const savedUser = localStorage.getItem('expert_mama_user');
if (savedJobs) setMarketplaceJobs(JSON.parse(savedJobs));
if (savedBids) setMarketplaceBids(JSON.parse(savedBids));
if (savedBookings) setDirectBookings(JSON.parse(savedBookings));
if (savedWallet) setMamaWallet(JSON.parse(savedWallet));
if (savedUser) setUser(JSON.parse(savedUser));
}, []);
useEffect(() => {
localStorage.setItem('expert_mama_jobs', JSON.stringify(marketplaceJobs));
localStorage.setItem('expert_mama_bids', JSON.stringify(marketplaceBids));
localStorage.setItem('expert_mama_bookings', JSON.stringify(directBookings));
localStorage.setItem('expert_mama_wallet', JSON.stringify(mamaWallet));
if (user) {
localStorage.setItem('expert_mama_user', JSON.stringify(user));
} else {
localStorage.removeItem('expert_mama_user');
}
}, [marketplaceJobs, marketplaceBids, directBookings, mamaWallet, user]);
const mamaActiveJobsCount = useMemo(() => {
if (!user || user.role !== UserRole.MAMA) return 0;
const activeDirect = directBookings.filter(b => b.mamaId === user.id && b.status === 'in-progress').length;
const activeMarket = marketplaceJobs.filter(j => j.selectedMamaId === user.id && j.status === 'in-progress').length;
return activeDirect + activeMarket;
}, [directBookings, marketplaceJobs, user]);
const maxCapacity = isMamaPremium ? 5 : 1;
const hasCapacity = mamaActiveJobsCount < maxCapacity;
const handleLoginSuccess = (loggedInUser: User) => {
setUser(loggedInUser);
setView('home');
};
const handleLogout = () => {
setUser(null);
setView('home');
localStorage.removeItem('expert_mama_user');
};
const handleRecommendation = (category: ServiceCategory, recommendation: any) => {
setRecommendedCategory(category);
setAiResponse(recommendation);
setView('home');
setTimeout(() => {
const el = document.getElementById('discovery-section');
if (el) el.scrollIntoView({ behavior: 'smooth' });
}, 100);
};
const postMarketplaceJob = (jobData: Partial<MarketplaceJob>) => {
if (!user) return setView('login');
const newJob: MarketplaceJob = {
id: 'job_' + Date.now(),
title: jobData.title || 'Untitled',
description: jobData.description || '',
category: jobData.category || ServiceCategory.CLEANING,
customerId: user.id,
customerName: user.name,
budget: jobData.budget || 0,
createdAt: new Date().toISOString(),
status: 'open'
};
setMarketplaceJobs([newJob, ...marketplaceJobs]);
setView('dashboard');
};
const placeMarketplaceBid = (jobId: string, bidData: Partial<MarketplaceBid>) => {
if (!user || user.role !== UserRole.MAMA) return;
if (!hasCapacity) return alert(`Capacity Reached!`);
const newBid: MarketplaceBid = {
id: 'bid_' + Date.now(),
jobId,
mamaId: user.id,
mamaName: user.name,
price: bidData.price || 0,
eta: bidData.eta || 'N/A',
note: bidData.note || '',
createdAt: new Date().toISOString(),
status: 'pending'
};
setMarketplaceBids([newBid, ...marketplaceBids]);
};
const selectMarketplaceBid = (jobId: string, bid: MarketplaceBid) => {
setMarketplaceJobs(prev => prev.map(j =>
j.id === jobId ? { ...j, status: 'pending-payment', selectedMamaId: bid.mamaId, selectedBidId: bid.id } : j
));
setMarketplaceBids(prev => prev.map(b =>
b.jobId === jobId ? { ...b, status: b.id === bid.id ? 'accepted' : 'rejected' } : b
));
setView('dashboard');
};
const confirmMarketplacePayment = (jobId: string) => {
setMarketplaceJobs(prev => prev.map(j =>
j.id === jobId ? { ...j, status: 'in-progress', unlockedContact: { phone: '01712-XXXXXX', address: 'Dhanmondi, Dhaka' } } : j
));
};
const completeMarketplaceJob = (jobId: string) => {
setMarketplaceJobs(prev => prev.map(j => j.id === jobId ? { ...j, status: 'completed' } : j));
};
const initiateDirectBooking = (service: ServiceDetail, mamaName: string) => {
if (!user) return setView('login');
const newBooking: Booking = {
id: 'bk_' + Date.now(),
serviceId: service.id,
serviceName: service.name,
category: service.category,
customerId: user.id,
customerName: user.name,
mamaId: 'mama_current',
mamaName,
status: 'pending-acceptance',
scheduledAt: 'ASAP',
location: 'Dhanmondi',
amount: service.price,
workflowType: 'DIRECT'
};
setDirectBookings([newBooking, ...directBookings]);
setView('dashboard');
};
const handleUpdateProfile = (updatedUser: User) => {
setUser(updatedUser);
};
const renderCurrentView = () => {
switch(view) {
case 'login':
return <LoginPage onLoginSuccess={handleLoginSuccess} onBack={() => setView('home')} />;
case 'profile':
return user ? (
<ProfilePage user={user} onUpdate={handleUpdateProfile} onBack={() => setView('home')} />
) : <LoginPage onLoginSuccess={handleLoginSuccess} onBack={() => setView('home')} />;
case 'dashboard':
if (!user) return <LoginPage onLoginSuccess={handleLoginSuccess} onBack={() => setView('home')} />;
return user.role === UserRole.MAMA ? (
<div className="max-w-7xl mx-auto px-4 py-12">
<MamaDashboard
directLeads={directBookings.filter(b => b.status === 'pending-acceptance')}
onAcceptLead={(id) => setDirectBookings(prev => prev.map(b => b.id === id ? { ...b, status: 'pending-payment' } : b))}
activeWork={[
...directBookings.filter(b => b.mamaId === MAMA_ID && ['pending-payment', 'in-progress', 'disputed'].includes(b.status)),
...marketplaceJobs.filter(j => j.selectedMamaId === MAMA_ID && ['pending-payment', 'in-progress', 'disputed'].includes(j.status))
]}
marketplaceJobs={marketplaceJobs}
myBids={marketplaceBids.filter(b => b.mamaId === user.id)}
onPlaceBid={placeMarketplaceBid}
mamaWallet={mamaWallet}
onWithdraw={handleWithdraw}
activeJobsCount={mamaActiveJobsCount}
maxCapacity={maxCapacity}
onUpgrade={() => setIsMamaPremium(true)}
isPremium={isMamaPremium}
/>
</div>
) : (
<CustomerDashboard
user={user}
activeBookings={directBookings.filter(b => b.customerId === user.id && b.status !== 'completed')}
marketplaceJobs={marketplaceJobs.filter(j => j.customerId === user.id)}
marketplaceBids={marketplaceBids}
onConfirmPayment={(id) => setDirectBookings(prev => prev.map(b => b.id === id ? { ...b, status: 'in-progress', unlockedContact: { phone: '01888-YYYYYY', address: 'Banani, Dhaka' } } : b))}
onMarkComplete={(id) => setDirectBookings(prev => prev.map(b => b.id === id ? { ...b, status: 'completed' } : b))}
onConfirmMarketplacePayment={confirmMarketplacePayment}
onCompleteMarketplaceJob={completeMarketplaceJob}
onRaiseMarketplaceDispute={(id) => setMarketplaceJobs(prev => prev.map(j => j.id === id ? { ...j, status: 'disputed' } : j))}
/>
);
case 'subscriptions':
return <SubscriptionPage />;
case 'service-details':
return selectedService ? (
<ServiceDetailsPage
service={selectedService}
onBack={() => { setView('home'); setSelectedService(null); }}
onSelectMama={(mamaName) => initiateDirectBooking(selectedService, mamaName)}
/>
) : null;
case 'home':
default:
return (
<>
<HeroAI onRecommendation={handleRecommendation} />
<div id="discovery-section">
<ServiceExplorer
recommendedCategory={recommendedCategory}
aiRecommendation={aiResponse}
onClearAI={() => { setRecommendedCategory(null); setAiResponse(null); }}
onServiceClick={(s) => { setSelectedService(s); setView('service-details'); }}
marketplaceJobs={marketplaceJobs}
marketplaceBids={marketplaceBids}
onPostMarketplaceJob={postMarketplaceJob}
onSelectMarketplaceBid={selectMarketplaceBid}
/>
</div>
</>
);
}
};
return (
<Layout
user={user}
onLoginClick={() => setView('login')}
onLogout={handleLogout}
onHeroClick={() => setView('subscriptions')}
currentView={view}
setView={(v) => { setView(v as any); setSelectedService(null); }}
>
{renderCurrentView()}
</Layout>
);
};
export default App;