2022-04-25 15:15:17 +02:00
|
|
|
import {
|
|
|
|
ColorScheme,
|
|
|
|
Container,
|
|
|
|
LoadingOverlay,
|
|
|
|
MantineProvider,
|
2022-05-11 14:18:36 +02:00
|
|
|
Stack,
|
2022-04-25 15:15:17 +02:00
|
|
|
} from "@mantine/core";
|
2022-10-10 22:30:04 +02:00
|
|
|
import { useColorScheme } from "@mantine/hooks";
|
2022-04-25 15:15:17 +02:00
|
|
|
import { ModalsProvider } from "@mantine/modals";
|
|
|
|
import { NotificationsProvider } from "@mantine/notifications";
|
2022-10-10 22:30:04 +02:00
|
|
|
import { getCookie, setCookies } from "cookies-next";
|
2022-04-25 15:15:17 +02:00
|
|
|
import { GetServerSidePropsContext } from "next";
|
|
|
|
import type { AppProps } from "next/app";
|
|
|
|
import { useEffect, useState } from "react";
|
2022-05-11 14:18:36 +02:00
|
|
|
import Footer from "../components/Footer";
|
2022-04-25 15:15:17 +02:00
|
|
|
import Header from "../components/navBar/NavBar";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { UserContext } from "../hooks/user.hook";
|
|
|
|
import authService from "../services/auth.service";
|
|
|
|
import userService from "../services/user.service";
|
2022-05-16 13:11:51 +02:00
|
|
|
import GlobalStyle from "../styles/global.style";
|
|
|
|
import globalStyle from "../styles/mantine.style";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { CurrentUser } from "../types/user.type";
|
2022-04-25 15:15:17 +02:00
|
|
|
import { GlobalLoadingContext } from "../utils/loading.util";
|
|
|
|
|
2022-05-02 22:42:27 +02:00
|
|
|
function App(
|
|
|
|
props: AppProps & { colorScheme: ColorScheme; environmentVariables: any }
|
|
|
|
) {
|
|
|
|
const { Component, pageProps } = props;
|
2022-10-10 22:30:04 +02:00
|
|
|
|
|
|
|
const systemTheme = useColorScheme();
|
|
|
|
|
2022-04-25 15:15:17 +02:00
|
|
|
const [colorScheme, setColorScheme] = useState<ColorScheme>(
|
|
|
|
props.colorScheme
|
|
|
|
);
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
2022-10-09 22:30:32 +02:00
|
|
|
const [user, setUser] = useState<CurrentUser | null>(null);
|
2022-05-02 08:22:53 +02:00
|
|
|
|
|
|
|
const getInitalData = async () => {
|
2022-04-25 15:15:17 +02:00
|
|
|
setIsLoading(true);
|
2022-10-11 10:22:10 +02:00
|
|
|
await authService.refreshAccessToken();
|
2022-10-09 22:30:32 +02:00
|
|
|
setUser(await userService.getCurrentUser());
|
2022-04-25 15:15:17 +02:00
|
|
|
setIsLoading(false);
|
|
|
|
};
|
2022-10-10 22:30:04 +02:00
|
|
|
|
2022-04-25 15:15:17 +02:00
|
|
|
useEffect(() => {
|
2022-10-09 22:30:32 +02:00
|
|
|
setInterval(async () => await authService.refreshAccessToken(), 30 * 1000);
|
2022-05-02 08:22:53 +02:00
|
|
|
getInitalData();
|
2022-04-25 15:15:17 +02:00
|
|
|
}, []);
|
2022-10-10 22:30:04 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-10-11 10:22:10 +02:00
|
|
|
setCookies("color-schema", systemTheme, {
|
2022-10-10 22:30:04 +02:00
|
|
|
maxAge: 60 * 60 * 24 * 30,
|
|
|
|
});
|
|
|
|
setColorScheme(systemTheme);
|
|
|
|
}, [systemTheme]);
|
|
|
|
|
2022-04-25 15:15:17 +02:00
|
|
|
return (
|
2022-10-10 22:30:04 +02:00
|
|
|
<MantineProvider
|
|
|
|
withGlobalStyles
|
|
|
|
withNormalizeCSS
|
|
|
|
theme={{ colorScheme, ...globalStyle }}
|
|
|
|
>
|
|
|
|
<GlobalStyle />
|
|
|
|
<NotificationsProvider>
|
|
|
|
<ModalsProvider>
|
|
|
|
<GlobalLoadingContext.Provider value={{ isLoading, setIsLoading }}>
|
|
|
|
{isLoading ? (
|
|
|
|
<LoadingOverlay visible overlayOpacity={1} />
|
|
|
|
) : (
|
|
|
|
<UserContext.Provider value={user}>
|
|
|
|
<LoadingOverlay visible={isLoading} overlayOpacity={1} />
|
|
|
|
<Stack justify="space-between" sx={{ minHeight: "100vh" }}>
|
|
|
|
<div>
|
|
|
|
<Header />
|
|
|
|
<Container>
|
|
|
|
<Component {...pageProps} />
|
|
|
|
</Container>
|
|
|
|
</div>
|
|
|
|
<Footer />
|
|
|
|
</Stack>
|
|
|
|
</UserContext.Provider>
|
|
|
|
)}
|
|
|
|
</GlobalLoadingContext.Provider>
|
|
|
|
</ModalsProvider>
|
|
|
|
</NotificationsProvider>
|
2022-04-25 15:15:17 +02:00
|
|
|
</MantineProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|
|
|
|
|
2022-05-02 22:42:27 +02:00
|
|
|
App.getInitialProps = ({ ctx }: { ctx: GetServerSidePropsContext }) => {
|
|
|
|
return {
|
2022-10-11 22:30:06 +02:00
|
|
|
colorScheme: getCookie("color-schema") || "light",
|
2022-05-02 22:42:27 +02:00
|
|
|
};
|
|
|
|
};
|