|
1 | 1 | import React, { lazy, Suspense, useEffect, useState } from "react";
|
2 | 2 | import { Route, Routes, Navigate } from "react-router-dom";
|
3 | 3 | import { useDispatch, useSelector } from "react-redux";
|
4 |
| -import { userLogin } from "@app/store/authReducer/actions"; |
5 | 4 | import { red } from "@mui/material/colors";
|
6 | 5 | import { ThemeProvider, createTheme } from "@mui/material/styles";
|
7 | 6 | import { indigo, pink } from "@mui/material/colors";
|
| 7 | +import { useNylas } from "@nylas/nylas-react"; |
| 8 | +import { userGetToken } from "@app/store/authReducer/actions"; |
8 | 9 |
|
9 | 10 | const theme = createTheme({
|
10 | 11 | breakpoints: {
|
@@ -222,31 +223,91 @@ const darkTheme = createTheme({
|
222 | 223 | });
|
223 | 224 |
|
224 | 225 | const App = () => {
|
225 |
| - const { loading, userInfo, error } = useSelector((state) => state.auth); |
| 226 | + const { loading, currentUser, error } = useSelector((state) => state.auth); |
| 227 | + const [currentAuthUser, setCurrentAuthUser] = useState(currentUser); |
226 | 228 | const [currentTheme, setCurrentTheme] = useState("light");
|
227 | 229 |
|
228 | 230 | const dispatch = useDispatch();
|
229 |
| - |
230 |
| - const submitForm = (data) => { |
231 |
| - dispatch(userLogin({ email: "test", password: "test" })); |
232 |
| - }; |
233 |
| - |
234 | 231 | const Landing = lazy(() => import("@app/pages/Landing"));
|
| 232 | + const Login = lazy(() => import("@app/pages/Login")); |
| 233 | + const MailApp = lazy(() => import("@app/pages/MailApp")); |
| 234 | + |
| 235 | + const nylas = useNylas(); |
235 | 236 |
|
236 | 237 | useEffect(() => {
|
237 | 238 | setCurrentTheme(localStorage.getItem("theme"));
|
238 |
| - }, [dispatch, localStorage.getItem("user"), localStorage.getItem("theme")]); |
| 239 | + }, [dispatch, localStorage.getItem("theme")]); |
| 240 | + |
| 241 | + useEffect(() => { |
| 242 | + setCurrentAuthUser(JSON.parse(localStorage.getItem("user"))); |
| 243 | + }, []); |
| 244 | + |
| 245 | + useEffect(() => { |
| 246 | + if (!nylas) { |
| 247 | + return; |
| 248 | + } |
| 249 | + const params = new URLSearchParams(window.location.search); |
| 250 | + if (params.has("code")) { |
| 251 | + dispatch(userGetToken({ nylas: nylas })); |
| 252 | + } |
| 253 | + }, [nylas]); |
239 | 254 |
|
240 | 255 | return (
|
241 | 256 | <ThemeProvider theme={currentTheme === "light" ? theme : darkTheme}>
|
242 | 257 | <Suspense>
|
243 | 258 | <Routes>
|
244 |
| - <Route exact path="/" element={<Landing />} /> |
| 259 | + <Route |
| 260 | + exact |
| 261 | + path="/" |
| 262 | + element={ |
| 263 | + <> |
| 264 | + {currentAuthUser ? ( |
| 265 | + <Navigate to={"/mail"} replace /> |
| 266 | + ) : ( |
| 267 | + <Landing /> |
| 268 | + )} |
| 269 | + </> |
| 270 | + } |
| 271 | + /> |
| 272 | + <Route |
| 273 | + exact |
| 274 | + path="/mail" |
| 275 | + element={<>{currentAuthUser ? <MailApp /> : <Login />}</>} |
| 276 | + /> |
| 277 | + <Route |
| 278 | + exact |
| 279 | + path="/login" |
| 280 | + element={ |
| 281 | + <> |
| 282 | + {currentAuthUser ? ( |
| 283 | + <Navigate to={"/mail"} replace /> |
| 284 | + ) : ( |
| 285 | + <Login /> |
| 286 | + )} |
| 287 | + </> |
| 288 | + } |
| 289 | + /> |
245 | 290 | <Route exact path="/home" element={<Navigate to={"/"} replace />} />
|
| 291 | + <Route path="/:url*" element={<RedirectToNylas />} /> |
246 | 292 | </Routes>
|
247 | 293 | </Suspense>
|
248 | 294 | </ThemeProvider>
|
249 | 295 | );
|
250 | 296 | };
|
251 | 297 |
|
| 298 | +function RedirectToNylas() { |
| 299 | + // A hack to redirect to nylas api |
| 300 | + // Get the current URL from window.location.href |
| 301 | + const currentUrl = window.location.href; |
| 302 | + // Extract the URL part between quotation marks |
| 303 | + const parts = currentUrl.split("%22"); |
| 304 | + |
| 305 | + if (parts.length >= 2) { |
| 306 | + const extractedContent = parts[1]; |
| 307 | + // Redirect to the extracted URL |
| 308 | + window.location.href = extractedContent; |
| 309 | + } |
| 310 | + |
| 311 | + return null; |
| 312 | +} |
252 | 313 | export default App;
|
0 commit comments