frontPanel/src/App.tsx

258 lines
7.0 KiB
TypeScript
Raw Normal View History

import type { SuspenseProps } from "react";
2024-05-16 13:40:52 +00:00
import { lazy, Suspense } from "react";
2024-03-20 12:16:21 +00:00
import { lazily } from "react-lazily";
2023-11-08 12:51:40 +00:00
import ContactFormModal from "@ui_kit/ContactForm";
import SigninDialog from "./pages/auth/Signin";
import SignupDialog from "./pages/auth/Signup";
2023-12-31 02:53:25 +00:00
import {
Navigate,
2023-12-31 02:53:25 +00:00
Route,
Routes,
useLocation,
useNavigate,
} from "react-router-dom";
2023-11-08 12:51:40 +00:00
import "./index.css";
import Landing from "./pages/Landing/Landing";
import Main from "./pages/main";
2023-12-31 02:53:25 +00:00
import {
clearAuthToken,
getMessageFromFetchError,
UserAccount,
useUserFetcher,
2023-12-31 02:53:25 +00:00
} from "@frontend/kitui";
import type { OriginalUserAccount } from "@root/user";
2023-12-31 02:53:25 +00:00
import {
clearUserData,
setCustomerAccount,
2023-12-31 02:53:25 +00:00
setUser,
setUserAccount,
useUserStore,
} from "@root/user";
2023-11-08 12:51:40 +00:00
import { enqueueSnackbar } from "notistack";
import PrivateRoute from "@ui_kit/PrivateRoute";
import FloatingSupportChat from "@ui_kit/FloatingSupportChat";
2023-11-08 12:51:40 +00:00
import { Restore } from "./pages/auth/Restore";
2023-11-08 12:51:40 +00:00
2023-12-27 15:30:40 +00:00
import { isAxiosError } from "axios";
import RecoverPassword from "./pages/auth/RecoverPassword";
2024-04-12 15:54:23 +00:00
import { InfoPrivilege } from "./pages/InfoPrivilege";
import OutdatedLink from "./pages/auth/OutdatedLink";
2024-04-01 14:49:00 +00:00
import { useAfterpay } from "@utils/hooks/useAfterpay";
2024-05-15 11:44:10 +00:00
import { useUserAccountFetcher } from "@utils/hooks/useUserAccountFetcher";
2024-02-20 11:54:08 +00:00
2024-03-20 12:16:21 +00:00
const MyQuizzesFull = lazy(() => import("./pages/createQuize/MyQuizzesFull"));
2024-05-17 14:13:05 +00:00
const QuizGallery = lazy(() => import("./pages/createQuize/QuizGallery"));
2024-03-20 12:16:21 +00:00
const ViewPage = lazy(() => import("./pages/ViewPublicationPage"));
const Analytics = lazy(() => import("./pages/Analytics/Analytics"));
const EditPage = lazy(() => import("./pages/startPage/EditPage"));
const { Tariffs } = lazily(() => import("./pages/Tariffs/Tariffs"));
const { DesignPage } = lazily(() => import("./pages/DesignPage/DesignPage"));
const { IntegrationsPage } = lazily(
() => import("./pages/IntegrationsPage/IntegrationsPage"),
);
2024-03-20 12:16:21 +00:00
const { QuizAnswersPage } = lazily(
() => import("./pages/QuizAnswersPage/QuizAnswersPage"),
);
const ChatImageNewWindow = lazy(
() => import("@ui_kit/FloatingSupportChat/ChatImageNewWindow"),
);
const routeslink = [
{
path: "/edit",
page: EditPage,
header: true,
sidebar: true,
footer: true,
},
{
path: "/design",
page: DesignPage,
header: true,
sidebar: true,
footer: true,
},
{
path: "/integrations",
page: IntegrationsPage,
header: true,
sidebar: true,
footer: true,
},
2024-03-20 12:16:21 +00:00
] as const;
const LazyLoading = ({ children, fallback }: SuspenseProps) => (
<Suspense fallback={fallback ?? <></>}>{children}</Suspense>
);
2023-11-08 12:51:40 +00:00
export default function App() {
2023-12-31 02:53:25 +00:00
const userId = useUserStore((state) => state.userId);
const location = useLocation();
const navigate = useNavigate();
2023-11-08 12:51:40 +00:00
2023-12-31 02:53:25 +00:00
useUserFetcher({
2024-05-15 11:44:10 +00:00
url: `${process.env.REACT_APP_DOMAIN}/user/${userId}`,
2023-12-31 02:53:25 +00:00
userId,
onNewUser: setUser,
onError: (error) => {
const errorMessage = getMessageFromFetchError(error);
if (errorMessage) {
enqueueSnackbar(errorMessage);
clearUserData();
clearAuthToken();
}
},
});
2023-11-08 12:51:40 +00:00
2024-02-20 11:54:08 +00:00
useUserAccountFetcher<UserAccount>({
2024-05-15 11:44:10 +00:00
url: `${process.env.REACT_APP_DOMAIN}/customer/account`,
2024-01-17 07:49:45 +00:00
userId,
onNewUserAccount: setCustomerAccount,
onError: (error) => {
const errorMessage = getMessageFromFetchError(error);
if (errorMessage) {
enqueueSnackbar(errorMessage);
clearUserData();
clearAuthToken();
navigate("/signin");
}
},
});
2024-02-20 11:54:08 +00:00
useUserAccountFetcher<OriginalUserAccount>({
2024-05-15 11:44:10 +00:00
url: `${process.env.REACT_APP_DOMAIN}/squiz/account/get`,
2023-12-31 02:53:25 +00:00
userId,
onNewUserAccount: setUserAccount,
onError: (error) => {
const errorMessage = getMessageFromFetchError(error);
if (errorMessage) {
enqueueSnackbar(errorMessage);
clearUserData();
clearAuthToken();
navigate("/signin");
}
},
});
2023-12-19 16:06:09 +00:00
2023-12-31 02:53:25 +00:00
if (location.state?.redirectTo)
2023-12-19 16:06:09 +00:00
return (
2023-12-31 02:53:25 +00:00
<Navigate
to={location.state.redirectTo}
replace
state={{ backgroundLocation: location }}
/>
2023-12-19 16:06:09 +00:00
);
2023-12-31 02:53:25 +00:00
2024-04-01 14:49:00 +00:00
useAfterpay();
2023-12-31 02:53:25 +00:00
return (
<>
<ContactFormModal />
2024-02-06 14:39:02 +00:00
<FloatingSupportChat />
2023-12-31 02:53:25 +00:00
{location.state?.backgroundLocation && (
<Routes>
<Route path="/signin" element={<SigninDialog />} />
<Route path="/signup" element={<SignupDialog />} />
<Route path="/recover" element={<Restore />} />
<Route path="/changepwd" element={<RecoverPassword />} />
<Route path="/changepwd/expired" element={<OutdatedLink />} />
2023-12-31 02:53:25 +00:00
</Routes>
)}
<Routes location={location.state?.backgroundLocation || location}>
<Route path="/" element={<Landing />} />
<Route
2023-12-31 02:53:25 +00:00
path="/signin"
element={
<Navigate to="/" replace state={{ redirectTo: "/signin" }} />
}
/>
<Route
path="/signup"
element={
<Navigate to="/" replace state={{ redirectTo: "/signup" }} />
}
/>
<Route
path="/recover"
2023-12-31 02:53:25 +00:00
element={
<Navigate to="/" replace state={{ redirectTo: "/recover" }} />
}
/>
<Route
path="/changepwd"
element={
<Navigate
to="/"
replace
state={{
redirectTo: window.location.pathname + window.location.search,
}}
/>
}
/>
<Route
path="/changepwd/expired"
element={
<Navigate
to="/"
replace
state={{ redirectTo: "/changepwd/expired" }}
/>
2023-12-31 02:53:25 +00:00
}
/>
2024-05-17 14:13:05 +00:00
<Route
path="/gallery"
element={<LazyLoading children={<QuizGallery />} />}
/>
2024-03-20 12:16:21 +00:00
<Route
path="/list"
element={<LazyLoading children={<MyQuizzesFull />} />}
/>
<Route
path={"/view/:quizId"}
element={<LazyLoading children={<ViewPage />} />}
/>
<Route
path={"/tariffs"}
element={<LazyLoading children={<Tariffs />} />}
/>
<Route
path={"/analytics"}
element={<LazyLoading children={<Analytics />} />}
/>
<Route
path={"/results/:quizId"}
element={<LazyLoading children={<QuizAnswersPage />} />}
/>
2024-04-12 15:54:23 +00:00
<Route
path={"/qaz"}
element={<LazyLoading children={<InfoPrivilege />} />}
/>
2024-05-10 13:50:27 +00:00
<Route path={"/image/:srcImage"} element={<ChatImageNewWindow />} />
2023-12-31 02:53:25 +00:00
<Route element={<PrivateRoute />}>
{routeslink.map((e, i) => (
<Route
key={i}
path={e.path}
element={
2024-03-20 12:16:21 +00:00
<LazyLoading
children={
<Main
Page={e.page}
header={e.header}
sidebar={e.sidebar}
footer={e.footer}
/>
}
/>
2023-12-31 02:53:25 +00:00
}
/>
))}
</Route>
2023-12-31 02:53:25 +00:00
</Routes>
</>
);
2023-12-16 13:02:27 +00:00
}