2023-11-08 12:51:40 +00:00
|
|
|
import ContactFormModal from "@ui_kit/ContactForm";
|
|
|
|
import ImageCrop from "@ui_kit/Modal/ImageCrop";
|
|
|
|
import dayjs from "dayjs";
|
|
|
|
import "dayjs/locale/ru";
|
|
|
|
import SigninDialog from "./pages/auth/Signin";
|
|
|
|
import SignupDialog from "./pages/auth/Signup";
|
2023-11-30 17:39:57 +00:00
|
|
|
import { ViewPage } from "./pages/ViewPublicationPage";
|
2023-12-29 13:32:57 +00:00
|
|
|
import { DesignPage } from "./pages/startPage/DesignPage";
|
2023-12-16 13:02:27 +00:00
|
|
|
import { Route, Routes, useLocation, useNavigate, Navigate } from "react-router-dom";
|
2023-11-08 12:51:40 +00:00
|
|
|
import "./index.css";
|
|
|
|
import ContactFormPage from "./pages/ContactFormPage/ContactFormPage";
|
|
|
|
import InstallQuiz from "./pages/InstallQuiz/InstallQuiz";
|
|
|
|
import Landing from "./pages/Landing/Landing";
|
|
|
|
import QuestionsPage from "./pages/Questions/QuestionsPage";
|
2023-12-07 21:30:26 +00:00
|
|
|
import { Result } from "./pages/ResultPage/Result";
|
2023-12-07 22:56:31 +00:00
|
|
|
import { ResultSettings } from "./pages/ResultPage/ResultSettings";
|
2023-11-08 12:51:40 +00:00
|
|
|
import MyQuizzesFull from "./pages/createQuize/MyQuizzesFull";
|
|
|
|
import Main from "./pages/main";
|
2023-12-07 22:56:31 +00:00
|
|
|
import EditPage from "./pages/startPage/EditPage";
|
2023-12-27 15:30:40 +00:00
|
|
|
import { clearAuthToken, getMessageFromFetchError, useUserFetcher, UserAccount, makeRequest, devlog, createUserAccount } from "@frontend/kitui";
|
2023-12-19 16:06:09 +00:00
|
|
|
import { clearUserData, setUser, setUserAccount, useUserStore } from "@root/user";
|
2023-11-08 12:51:40 +00:00
|
|
|
import { enqueueSnackbar } from "notistack";
|
2023-12-14 16:50:02 +00:00
|
|
|
import PrivateRoute from "@ui_kit/PrivateRoute";
|
2023-11-08 12:51:40 +00:00
|
|
|
|
2023-12-16 13:02:27 +00:00
|
|
|
import { Restore } from "./pages/startPage/Restore";
|
2023-11-08 12:51:40 +00:00
|
|
|
|
2023-12-27 15:30:40 +00:00
|
|
|
import { isAxiosError } from "axios";
|
|
|
|
import { useEffect, useLayoutEffect, useRef } from "react";
|
|
|
|
export function useUserAccountFetcher({ onError, onNewUserAccount, url, userId }: {
|
|
|
|
url: string;
|
|
|
|
userId: string | null;
|
|
|
|
onNewUserAccount: (response: UserAccount) => void;
|
|
|
|
onError?: (error: any) => void;
|
|
|
|
}) {
|
|
|
|
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();
|
|
|
|
makeRequest<never, UserAccount>({
|
|
|
|
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);
|
|
|
|
onNewUserAccountRef.current(result);
|
|
|
|
}).catch(error => {
|
|
|
|
devlog("Error creating user account", error);
|
|
|
|
onErrorRef.current?.(error);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
onErrorRef.current?.(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return () => controller.abort();
|
|
|
|
}, [url, userId]);
|
|
|
|
}
|
|
|
|
|
2023-11-08 12:51:40 +00:00
|
|
|
dayjs.locale("ru");
|
|
|
|
|
2023-12-27 15:30:40 +00:00
|
|
|
|
2023-11-08 12:51:40 +00:00
|
|
|
const routeslink = [
|
2023-12-19 16:06:09 +00:00
|
|
|
{ path: "/list", page: <MyQuizzesFull />, header: false, sidebar: false },
|
|
|
|
{ path: "/questions/:quizId", page: <QuestionsPage />, header: true, sidebar: true },
|
|
|
|
{ path: "/contacts", page: <ContactFormPage />, header: true, sidebar: true },
|
|
|
|
{ path: "/result", page: <Result />, header: true, sidebar: true },
|
|
|
|
{ path: "/settings", page: <ResultSettings />, header: true, sidebar: true },
|
2023-11-08 12:51:40 +00:00
|
|
|
] as const;
|
|
|
|
|
|
|
|
export default function App() {
|
2023-12-19 16:06:09 +00:00
|
|
|
const userId = useUserStore((state) => state.userId);
|
|
|
|
const location = useLocation();
|
|
|
|
const navigate = useNavigate();
|
2023-11-08 12:51:40 +00:00
|
|
|
|
2023-12-19 16:06:09 +00:00
|
|
|
useUserFetcher({
|
|
|
|
url: `https://hub.pena.digital/user/${userId}`,
|
|
|
|
userId,
|
|
|
|
onNewUser: setUser,
|
|
|
|
onError: (error) => {
|
|
|
|
const errorMessage = getMessageFromFetchError(error);
|
|
|
|
if (errorMessage) {
|
|
|
|
enqueueSnackbar(errorMessage);
|
|
|
|
clearUserData();
|
|
|
|
clearAuthToken();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2023-11-08 12:51:40 +00:00
|
|
|
|
2023-12-19 16:06:09 +00:00
|
|
|
useUserAccountFetcher({
|
2023-12-27 15:30:40 +00:00
|
|
|
url: "https://squiz.pena.digital/squiz/account/get",
|
2023-12-19 16:06:09 +00:00
|
|
|
userId,
|
|
|
|
onNewUserAccount: setUserAccount,
|
|
|
|
onError: (error) => {
|
|
|
|
const errorMessage = getMessageFromFetchError(error);
|
|
|
|
if (errorMessage) {
|
|
|
|
enqueueSnackbar(errorMessage);
|
|
|
|
clearUserData();
|
|
|
|
clearAuthToken();
|
|
|
|
navigate("/signin");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2023-12-16 13:02:27 +00:00
|
|
|
|
2023-12-19 16:06:09 +00:00
|
|
|
if (location.state?.redirectTo)
|
|
|
|
return <Navigate to={location.state.redirectTo} replace state={{ backgroundLocation: location }} />;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ContactFormModal />
|
|
|
|
{location.state?.backgroundLocation && (
|
|
|
|
<Routes>
|
|
|
|
<Route path="/signin" element={<SigninDialog />} />
|
|
|
|
<Route path="/signup" element={<SignupDialog />} />
|
|
|
|
<Route path="/restore" element={<Restore />} />
|
|
|
|
</Routes>
|
|
|
|
)}
|
|
|
|
<Routes location={location.state?.backgroundLocation || location}>
|
|
|
|
<Route path="/" element={<Landing />} />
|
|
|
|
<Route path="/signin" element={<Navigate to="/" replace state={{ redirectTo: "/signin" }} />} />
|
|
|
|
<Route path="/signup" element={<Navigate to="/" replace state={{ redirectTo: "/signup" }} />} />
|
|
|
|
<Route path="/restore" element={<Navigate to="/" replace state={{ redirectTo: "/restore" }} />} />
|
|
|
|
<Route element={<PrivateRoute />}>
|
|
|
|
{routeslink.map((e, i) => (
|
|
|
|
<Route key={i} path={e.path} element={<Main page={e.page} header={e.header} sidebar={e.sidebar} />} />
|
|
|
|
))}
|
|
|
|
|
|
|
|
<Route path="edit" element={<EditPage />} />
|
|
|
|
<Route path="crop" element={<ImageCrop />} />
|
|
|
|
<Route path="/view" element={<ViewPage />} />
|
2023-12-29 13:32:57 +00:00
|
|
|
<Route path="/design" element={<DesignPage />} />
|
2023-12-19 16:06:09 +00:00
|
|
|
</Route>
|
|
|
|
</Routes>
|
|
|
|
</>
|
|
|
|
);
|
2023-12-16 13:02:27 +00:00
|
|
|
}
|