|
1 | | -import { useState, useEffect } from 'react' |
2 | 1 | import './App.css' |
3 | | -import Header from './components/Dashboard/Header' |
4 | | -import Footer from './components/Dashboard/Footer' |
5 | | -import Sidebar from './components/Dashboard/Sidebar' |
6 | | -import CVBuilder from './components/Dashboard/CVBuilder/CVBuilder' |
7 | | -import SystematicReviews from './components/Dashboard/SystematicReview/SystematicReviews' |
8 | | -import CaseReports from './components/Dashboard/CaseReports/CaseReports' |
9 | | -import Conferences from './components/Dashboard/Conferences/Conferences' |
10 | | -import Existingcv from './components/Dashboard/ExistingCV/Existingcv' |
11 | | -import Login from './components/Login/Login' |
12 | | -import Register from './components/Register/Register' |
| 2 | +import AuthManager from './components/AuthManager/AuthManager'; |
13 | 3 | import { ToastContainer } from 'react-toastify'; |
14 | 4 | import 'react-toastify/dist/ReactToastify.css'; |
15 | | -import api from './utils/api'; |
16 | 5 |
|
17 | 6 | function App() { |
18 | | - const [activeSection, setActiveSection] = useState('cv-builder'); |
19 | | - const [isAuthenticated, setIsAuthenticated] = useState(false); |
20 | | - const [showRegister, setShowRegister] = useState(false); |
21 | | - const [user, setUser] = useState(null); |
22 | | - const [loading, setLoading] = useState(true); |
23 | | - const [currentCVStep, setCurrentCVStep] = useState(1); |
24 | | - const [completedSteps, setCompletedSteps] = useState([]); |
25 | | - |
26 | | - useEffect(() => { |
27 | | - checkAuthStatus(); |
28 | | - }, []); |
29 | | - |
30 | | - const checkAuthStatus = async () => { |
31 | | - try { |
32 | | - const response = await api.get('/users/current-user', { withCredentials: true }); |
33 | | - if (response.data.success) { |
34 | | - setUser(response.data.data); |
35 | | - setIsAuthenticated(true); |
36 | | - } |
37 | | - } catch (error) { |
38 | | - setIsAuthenticated(false); |
39 | | - setUser(null); |
40 | | - } finally { |
41 | | - setLoading(false); |
42 | | - } |
43 | | - }; |
44 | | - |
45 | | - const handleLogin = (userData) => { |
46 | | - setUser(userData); |
47 | | - setIsAuthenticated(true); |
48 | | - setShowRegister(false); |
49 | | - }; |
50 | | - |
51 | | - const handleRegister = (userData) => { |
52 | | - setUser(userData); |
53 | | - setIsAuthenticated(true); |
54 | | - setShowRegister(false); |
55 | | - }; |
56 | | - |
57 | | - const handleLogout = async () => { |
58 | | - try { |
59 | | - await api.post('/users/logout'); |
60 | | - } catch (error) { |
61 | | - console.error('Logout error:', error); |
62 | | - } finally { |
63 | | - setUser(null); |
64 | | - setIsAuthenticated(false); |
65 | | - setActiveSection('cv-builder'); |
66 | | - // Reset CV builder states on logout |
67 | | - setCurrentCVStep(1); |
68 | | - setCompletedSteps([]); |
69 | | - } |
70 | | - }; |
71 | | - |
72 | | - const handleSectionChange = (section) => { |
73 | | - setActiveSection(section); |
74 | | - // Reset CV step when switching to CV builder from other sections |
75 | | - if (section === 'cv-builder' && activeSection !== 'cv-builder') { |
76 | | - setCurrentCVStep(1); |
77 | | - } |
78 | | - }; |
79 | | - |
80 | | - const handleCVStepChange = (step) => { |
81 | | - setCurrentCVStep(step); |
82 | | - }; |
83 | | - |
84 | | - const handleStepComplete = (step) => { |
85 | | - if (!completedSteps.includes(step)) { |
86 | | - setCompletedSteps(prev => [...prev, step].sort((a, b) => a - b)); |
87 | | - } |
88 | | - }; |
89 | | - |
90 | | - const renderActiveSection = () => { |
91 | | - switch (activeSection) { |
92 | | - case 'cv-builder': |
93 | | - return ( |
94 | | - <CVBuilder |
95 | | - key={user?._id || "guest"} |
96 | | - user={user} |
97 | | - currentStep={currentCVStep} |
98 | | - onStepChange={handleCVStepChange} |
99 | | - onStepComplete={handleStepComplete} |
100 | | - /> |
101 | | - ); |
102 | | - |
103 | | - case 'existing-cv': |
104 | | - return ( |
105 | | - <Existingcv onBack={() => setActiveSection('cv-builder')} /> |
106 | | - ); |
107 | | - |
108 | | - case 'systematic-reviews': |
109 | | - return ( |
110 | | - <SystematicReviews |
111 | | - onBack={() => setActiveSection('cv-builder')} |
112 | | - user={user} |
113 | | - /> |
114 | | - ); |
115 | | - |
116 | | - case 'case-reports': |
117 | | - return ( |
118 | | - <CaseReports |
119 | | - onBack={() => setActiveSection('cv-builder')} |
120 | | - user={user} |
121 | | - /> |
122 | | - ); |
123 | | - |
124 | | - case 'conferences': |
125 | | - return ( |
126 | | - <Conferences |
127 | | - onBack={() => setActiveSection('cv-builder')} |
128 | | - user={user} |
129 | | - /> |
130 | | - ); |
131 | | - |
132 | | - case 'workshops': |
133 | | - return ( |
134 | | - <Workshops |
135 | | - onBack={() => setActiveSection('cv-builder')} |
136 | | - user={user} |
137 | | - /> |
138 | | - ); |
139 | | - |
140 | | - case 'emr-training': |
141 | | - return ( |
142 | | - <EMRTraining |
143 | | - onBack={() => setActiveSection('cv-builder')} |
144 | | - user={user} |
145 | | - /> |
146 | | - ); |
147 | | - |
148 | | - default: |
149 | | - return ( |
150 | | - <CVBuilder |
151 | | - key={user?._id || "guest"} |
152 | | - user={user} |
153 | | - currentStep={currentCVStep} |
154 | | - onStepChange={handleCVStepChange} |
155 | | - onStepComplete={handleStepComplete} |
156 | | - /> |
157 | | - ); |
158 | | - } |
159 | | - }; |
160 | | - |
161 | | - if (loading) { |
162 | | - return ( |
163 | | - <div className="min-h-screen bg-gray-50 flex items-center justify-center"> |
164 | | - <div className="text-xl text-[#04445E]">Loading...</div> |
165 | | - </div> |
166 | | - ); |
167 | | - } |
168 | | - |
169 | 7 | return ( |
170 | 8 | <> |
171 | | - {!isAuthenticated ? ( |
172 | | - showRegister ? ( |
173 | | - <Register |
174 | | - onRegister={handleRegister} |
175 | | - onNavigateToLogin={() => setShowRegister(false)} |
176 | | - /> |
177 | | - ) : ( |
178 | | - <Login |
179 | | - onLogin={handleLogin} |
180 | | - onNavigateToRegister={() => setShowRegister(true)} |
181 | | - /> |
182 | | - ) |
183 | | - ) : ( |
184 | | - <div className="min-h-screen bg-gray-50 flex flex-col"> |
185 | | - <Header user={user} onLogout={handleLogout} /> |
186 | | - |
187 | | - <div className="flex-1 max-w-full w-full px-4 sm:px-6 lg:px-8 py-8"> |
188 | | - <div className="flex gap-8 h-full"> |
189 | | - <Sidebar |
190 | | - activeSection={activeSection} |
191 | | - onSectionChange={handleSectionChange} |
192 | | - user={user} |
193 | | - // CV Builder specific props - only pass when CV Builder is active |
194 | | - currentStep={activeSection === 'cv-builder' ? currentCVStep : null} |
195 | | - onStepChange={activeSection === 'cv-builder' ? handleCVStepChange : null} |
196 | | - completedSteps={activeSection === 'cv-builder' ? completedSteps : []} |
197 | | - /> |
198 | | - <div className="flex-1"> |
199 | | - {renderActiveSection()} |
200 | | - </div> |
201 | | - </div> |
202 | | - </div> |
203 | | - |
204 | | - <Footer /> |
205 | | - </div> |
206 | | - )} |
| 9 | + <AuthManager /> |
207 | 10 |
|
208 | 11 | <ToastContainer |
209 | 12 | position="top-right" |
|
0 commit comments