front-hub/src/index.tsx

97 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-05-17 11:20:11 +00:00
import React, { useEffect } from "react";
2022-11-17 12:25:23 +00:00
import ReactDOM from "react-dom/client";
2023-05-19 09:44:18 +00:00
import { BrowserRouter, Navigate, Route, Routes, useLocation } from "react-router-dom";
2023-05-17 11:20:11 +00:00
import { CssBaseline, ThemeProvider } from "@mui/material";
import Faq from "./pages/Faq/Faq";
import Wallet from "./pages/Wallet";
import Payment from "./pages/Payment/Payment";
import Support from "./pages/Support/Support";
import CustomTariff from "./pages/CustomTariff/CustomTariff";
2023-03-27 12:45:44 +00:00
import AccountSetup from "./pages/AccountSetup";
import Landing from "./pages/Landing/Landing";
import Tariffs from "./pages/Tariffs/Tariffs";
2023-05-13 14:13:57 +00:00
import SigninDialog from "./pages/auth/Signin";
import SignupDialog from "./pages/auth/Signup";
2023-03-20 13:30:48 +00:00
import PaymentHistory from "./pages/PaymentHistory/PaymentHistory";
2023-03-27 12:45:44 +00:00
import Basket from "./pages/Basket/Basket";
import TariffPage from "./pages/Tariffs/TariffsPage";
import lightTheme from "@utils/themes/light";
2023-03-27 12:45:44 +00:00
import PrivateRoute from "@utils/routes/privateRoute";
import reportWebVitals from "./reportWebVitals";
2023-05-17 11:20:11 +00:00
import { SnackbarProvider, enqueueSnackbar } from "notistack";
2023-03-27 12:45:44 +00:00
import "./index.css";
2023-05-17 11:20:11 +00:00
import Layout from "./components/Layout";
import { getOrCreateUser } from "./api/auth";
import { setUser, useUserStore } from "./stores/user";
2023-03-27 12:45:44 +00:00
2023-05-13 14:13:57 +00:00
2023-03-19 12:32:23 +00:00
const App = () => {
2023-05-13 14:13:57 +00:00
const location = useLocation();
2023-05-17 11:20:11 +00:00
const userId = useUserStore(state => state.userId);
2023-05-17 11:20:11 +00:00
useEffect(function fetchUserData() {
if (!userId) return;
2023-05-17 15:26:10 +00:00
getOrCreateUser(userId).then(result => {
2023-05-17 11:20:11 +00:00
setUser(result);
}).catch(error => {
console.log("Error fetching user", error);
enqueueSnackbar(error.response?.data?.message ?? error.message ?? "Error fetching user");
2023-05-17 15:26:10 +00:00
setUser(null);
2023-05-17 11:20:11 +00:00
});
2023-05-17 13:18:26 +00:00
}, [userId]);
2023-05-17 11:20:11 +00:00
2023-05-19 09:44:18 +00:00
if (location.state?.redirectTo === "/signin") return <Navigate to="/signin" replace state={{ backgroundLocation: location }} />;
if (location.state?.redirectTo === "/signup") return <Navigate to="/signup" replace state={{ backgroundLocation: location }} />;
2023-05-13 14:13:57 +00:00
return (
<>
2023-05-19 09:44:18 +00:00
{location.state?.backgroundLocation &&
2023-05-13 14:13:57 +00:00
<Routes>
<Route path="/signin" element={<SigninDialog />} />
<Route path="/signup" element={<SignupDialog />} />
</Routes>
}
2023-05-19 09:44:18 +00:00
<Routes location={location.state?.backgroundLocation || location}>
2023-05-17 11:20:11 +00:00
<Route path="/" element={<Landing />} />
2023-05-19 09:44:18 +00:00
<Route path="/signin" element={<Navigate to="/" replace state={{ redirectTo: "/signin" }} />} />
<Route path="/signup" element={<Navigate to="/" replace state={{ redirectTo: "/signup" }} />} />
2023-05-17 11:20:11 +00:00
<Route element={<Layout />}>
<Route path="/tariffs" element={<Tariffs />} />
<Route path="/tariffs/time" element={<TariffPage />} />
<Route path="/tariffs/volume" element={<TariffPage />} />
<Route path="/faq" element={<Faq />} />
<Route path="/support" element={<Support />} />
<Route path="/support/:ticketId" element={<Support />} />
<Route path="/tariffconstructor" element={<CustomTariff />} />
<Route path="/basket" element={<Basket />} />
<Route element={<PrivateRoute />}>
<Route path="/wallet" element={<Wallet />} />
<Route path="/payment" element={<Payment />} />
<Route path="/settings" element={<AccountSetup />} />
<Route path="/paymenthistory" element={<PaymentHistory />} />
</Route>
</Route>
2023-05-13 14:13:57 +00:00
</Routes>
</>
);
};
const root = ReactDOM.createRoot(document.getElementById("root") as HTMLElement);
2022-11-17 12:25:23 +00:00
root.render(
2023-05-13 14:13:57 +00:00
<React.StrictMode>
<ThemeProvider theme={lightTheme}>
<BrowserRouter>
<CssBaseline />
<SnackbarProvider />
<App />
</BrowserRouter>
</ThemeProvider>
</React.StrictMode>
2022-11-17 12:25:23 +00:00
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();