front-hub/src/index.tsx

130 lines
5.4 KiB
TypeScript
Raw Normal View History

import React from "react";
2022-11-17 12:25:23 +00:00
import ReactDOM from "react-dom/client";
2023-06-24 18:17:43 +00:00
import { BrowserRouter, Navigate, Route, Routes, useLocation, useNavigate } 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";
2023-06-24 12:04:06 +00:00
import AccountSettings from "./pages/AccountSettings/AccountSettings";
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";
2023-06-24 18:17:43 +00:00
import { clearUserData, setUser, setUserAccount, useUserStore } from "./stores/user";
2023-05-27 11:50:21 +00:00
import TariffConstructor from "./pages/TariffConstructor/TariffConstructor";
2023-06-06 13:13:58 +00:00
import { useUser } from "./utils/hooks/useUser";
2023-06-24 18:17:43 +00:00
import { clearAuthToken, getMessageFromFetchError } from "@frontend/kitui";
import { useUserAccount } from "./utils/hooks/useUserAccount";
2023-07-10 20:22:06 +00:00
import { useSetDiscountsStore } from "./utils/hooks/useSetDiscountsStore";
import { setCustomTariffs} from "@root/stores/customTariffs";
import { useCustomTariffs } from "@root/utils/hooks/useCustomTariffs";
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-06-24 18:17:43 +00:00
const navigate = useNavigate();
2023-07-10 20:22:06 +00:00
useCustomTariffs({
url: "https://admin.pena.digital/strator/privilege/service",
onNewUser: setCustomTariffs,
onError: error => {
const errorMessage = getMessageFromFetchError(error, "Не удалось получить кастомные тарифы");
if (errorMessage) enqueueSnackbar(errorMessage);
}
});
2023-07-10 20:22:06 +00:00
useSetDiscountsStore();
2023-06-06 13:13:58 +00:00
useUser({
url: `https://hub.pena.digital/user/${userId}`,
userId,
onNewUser: setUser,
onError: error => {
2023-06-20 10:23:30 +00:00
const errorMessage = getMessageFromFetchError(error);
2023-06-24 12:00:15 +00:00
if (errorMessage) {
enqueueSnackbar(errorMessage);
2023-06-24 18:17:43 +00:00
clearUserData();
clearAuthToken();
2023-06-24 12:00:15 +00:00
}
}
2023-06-06 13:13:58 +00:00
});
2023-05-17 11:20:11 +00:00
2023-06-24 18:17:43 +00:00
useUserAccount({
url: "https://hub.pena.digital/customer/account",
userId,
onNewUserAccount: setUserAccount,
onError: error => {
2023-06-24 18:17:43 +00:00
const errorMessage = getMessageFromFetchError(error);
if (errorMessage) {
enqueueSnackbar(errorMessage);
clearUserData();
clearAuthToken();
navigate("/signin");
}
}
2023-06-24 18:17:43 +00:00
});
2023-06-17 14:31:32 +00:00
if (location.state?.redirectTo) return <Navigate to={location.state.redirectTo} 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 element={<PrivateRoute />}>
<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={<TariffConstructor />} />
<Route path="/basket" element={<Basket />} />
2023-05-17 11:20:11 +00:00
<Route path="/wallet" element={<Wallet />} />
<Route path="/payment" element={<Payment />} />
2023-06-24 12:03:12 +00:00
<Route path="/settings" element={<AccountSettings />} />
2023-05-17 11:20:11 +00:00
<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();