frontPanel/src/App.tsx

300 lines
8.2 KiB
TypeScript
Raw Normal View History

import type { SuspenseProps } from "react";
import { lazy, Suspense, useEffect, useLayoutEffect, useRef } 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 dayjs from "dayjs";
import "dayjs/locale/ru";
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,
createUserAccount,
devlog,
2023-12-31 02:53:25 +00:00
getMessageFromFetchError,
makeRequest,
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-02-20 11:54:08 +00:00
2024-03-20 12:16:21 +00:00
const MyQuizzesFull = lazy(() => import("./pages/createQuize/MyQuizzesFull"));
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 { QuizAnswersPage } = lazily(
() => import("./pages/QuizAnswersPage/QuizAnswersPage"),
);
const ChatImageNewWindow = lazy(
() => import("@ui_kit/FloatingSupportChat/ChatImageNewWindow"),
);
dayjs.locale("ru");
const routeslink = [
{
path: "/edit",
page: EditPage,
header: true,
sidebar: true,
footer: true,
},
{
path: "/design",
page: DesignPage,
header: true,
sidebar: true,
footer: true,
},
] as const;
const LazyLoading = ({ children, fallback }: SuspenseProps) => (
<Suspense fallback={fallback ?? <></>}>{children}</Suspense>
);
export function useUserAccountFetcher<T = UserAccount>({
2023-12-31 02:53:25 +00:00
onError,
onNewUserAccount,
url,
userId,
}: {
url: string;
userId: string | null;
2024-02-20 11:54:08 +00:00
onNewUserAccount: (response: T) => void;
2023-12-31 02:53:25 +00:00
onError?: (error: any) => void;
2023-12-27 15:30:40 +00:00
}) {
2023-12-31 02:53:25 +00:00
const onNewUserAccountRef = useRef(onNewUserAccount);
const onErrorRef = useRef(onError);
useLayoutEffect(() => {
onNewUserAccountRef.current = onNewUserAccount;
onErrorRef.current = onError;
}, [onError, onNewUserAccount]);
useEffect(() => {
if (!userId) return;
const controller = new AbortController();
2024-02-20 11:54:08 +00:00
makeRequest<never, T>({
2023-12-31 02:53:25 +00:00
url,
contentType: true,
method: "GET",
useToken: true,
withCredentials: false,
signal: controller.signal,
})
.then((result) => {
devlog("User account", result);
onNewUserAccountRef.current(result);
})
.catch((error) => {
devlog("Error fetching user account", error);
if (isAxiosError(error) && error.response?.status === 404) {
createUserAccount(controller.signal, url.replace("get", "create"))
.then((result) => {
devlog("Created user account", result);
2024-02-20 11:54:08 +00:00
onNewUserAccountRef.current(result as T);
2023-12-31 02:53:25 +00:00
})
.catch((error) => {
devlog("Error creating user account", error);
onErrorRef.current?.(error);
});
} else {
onErrorRef.current?.(error);
}
});
return () => controller.abort();
}, [url, userId]);
2023-12-27 15:30:40 +00:00
}
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({
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>({
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>({
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-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 />} />}
/>
2023-12-31 02:53:25 +00:00
<Route element={<PrivateRoute />}>
<Route path={"/image/:srcImage"} element={<ChatImageNewWindow />} />
2023-12-31 02:53:25 +00:00
{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
}