blob: ebebd81e93c7bc6f2bd691eaf575cdeb7ea556d4 [file] [log] [blame]
Larbi Gharibe9af9732021-03-31 15:08:01 +01001/*
simon26e79f72022-10-05 22:16:08 -04002 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
idillon9e542ca2022-12-15 17:54:07 -050018import './dayjsInitializer'; // Initialized once and globally to ensure available locales are always imported
19
simon94fe53e2022-11-10 12:51:58 -050020import axios from 'axios';
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040021import { useState } from 'react';
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050022import { json, LoaderFunctionArgs, Outlet, redirect } from 'react-router-dom';
idillon-sfl0e1a0d92022-10-25 16:52:44 -040023
Ziwei Wang2c0f3f82023-02-02 17:30:49 -050024import WelcomeAnimation from './components/WelcomeAnimation';
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050025import { getAccessToken } from './utils/auth';
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050026import { apiUrl } from './utils/constants';
27
28export async function checkSetupStatus(): Promise<boolean> {
simonf929a362022-11-18 16:53:45 -050029 try {
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -050030 const { data } = await axios.get<{ isSetupComplete: boolean }>('/setup/check', { baseURL: apiUrl });
simonf929a362022-11-18 16:53:45 -050031 return data.isSetupComplete;
32 } catch (e) {
33 throw new Error('Cannot connect to server', { cause: e });
34 }
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050035}
36
37export async function appLoader({ request }: LoaderFunctionArgs) {
38 const initialUrl = new URL(request.url);
39 const isSetupComplete = await checkSetupStatus();
40
41 if (!isSetupComplete && initialUrl.pathname !== '/setup/login') {
42 return redirect('/setup/login');
43 }
44 return json({ isSetupComplete }, { status: 200 });
45}
Adrien Béraudab519ff2022-05-03 15:34:48 -040046
simon80b7b3b2022-09-28 17:50:10 -040047const App = () => {
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050048 const [displayWelcome, setDisplayWelcome] = useState<boolean>(getAccessToken() === undefined);
Adrien Béraud6c934962021-06-07 10:13:26 -040049
simond47ef9e2022-09-28 22:24:28 -040050 console.log('App render');
idillond858c182022-09-16 13:18:26 -040051
52 if (displayWelcome) {
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040053 return <WelcomeAnimation onComplete={() => setDisplayWelcome(false)} />;
idillond858c182022-09-16 13:18:26 -040054 }
55
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040056 return <Outlet />;
idillond858c182022-09-16 13:18:26 -040057};
Larbi Gharibe9af9732021-03-31 15:08:01 +010058
simond47ef9e2022-09-28 22:24:28 -040059export default App;