2022-04-25 15:15:17 +02:00
|
|
|
import {
|
|
|
|
ColorScheme,
|
|
|
|
Container,
|
|
|
|
LoadingOverlay,
|
|
|
|
MantineProvider,
|
|
|
|
} 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";
|
|
|
|
import type { AppProps } from "next/app";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import Header from "../components/navBar/NavBar";
|
2022-11-28 17:50:36 +01:00
|
|
|
import { ConfigContext } from "../hooks/config.hook";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { UserContext } from "../hooks/user.hook";
|
|
|
|
import authService from "../services/auth.service";
|
2022-11-28 17:50:36 +01:00
|
|
|
import configService from "../services/config.service";
|
2022-10-09 22:30:32 +02:00
|
|
|
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-11-28 17:50:36 +01:00
|
|
|
import Config from "../types/config.type";
|
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-10-14 12:04:47 +02:00
|
|
|
function App({ Component, pageProps }: AppProps) {
|
2022-10-10 22:30:04 +02:00
|
|
|
const systemTheme = useColorScheme();
|
|
|
|
|
2022-10-14 12:04:47 +02:00
|
|
|
const [colorScheme, setColorScheme] = useState<ColorScheme>();
|
2022-04-25 15:15:17 +02:00
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
2022-10-09 22:30:32 +02:00
|
|
|
const [user, setUser] = useState<CurrentUser | null>(null);
|
2022-11-28 17:50:36 +01:00
|
|
|
const [config, setConfig] = useState<Config[] | null>(null);
|
2022-05-02 08:22:53 +02:00
|
|
|
|
|
|
|
const getInitalData = async () => {
|
2022-04-25 15:15:17 +02:00
|
|
|
setIsLoading(true);
|
2022-11-28 17:50:36 +01:00
|
|
|
setConfig(await configService.getAll());
|
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(() => {
|
|
|
|
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} />
|
|
|
|
) : (
|
2022-11-28 17:50:36 +01:00
|
|
|
<ConfigContext.Provider value={config}>
|
|
|
|
<UserContext.Provider value={user}>
|
|
|
|
<LoadingOverlay visible={isLoading} overlayOpacity={1} />
|
|
|
|
<Header />
|
|
|
|
<Container>
|
|
|
|
<Component {...pageProps} />
|
|
|
|
</Container>
|
|
|
|
</UserContext.Provider>{" "}
|
|
|
|
</ConfigContext.Provider>
|
2022-10-10 22:30:04 +02:00
|
|
|
)}
|
|
|
|
</GlobalLoadingContext.Provider>
|
|
|
|
</ModalsProvider>
|
|
|
|
</NotificationsProvider>
|
2022-04-25 15:15:17 +02:00
|
|
|
</MantineProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|