Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 1 | /* |
simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 2 | * 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 | */ |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 18 | import './dayjsInitializer'; // Initialized once and globally to ensure available locales are always imported |
| 19 | |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 20 | import axios from 'axios'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 21 | import { useState } from 'react'; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 22 | import { json, LoaderFunctionArgs, Outlet, redirect } from 'react-router-dom'; |
idillon-sfl | 0e1a0d9 | 2022-10-25 16:52:44 -0400 | [diff] [blame] | 23 | |
Ziwei Wang | 2c0f3f8 | 2023-02-02 17:30:49 -0500 | [diff] [blame] | 24 | import WelcomeAnimation from './components/WelcomeAnimation'; |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 25 | import { getAccessToken } from './utils/auth'; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 26 | import { apiUrl } from './utils/constants'; |
| 27 | |
| 28 | export async function checkSetupStatus(): Promise<boolean> { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 29 | try { |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 30 | const { data } = await axios.get<{ isSetupComplete: boolean }>('/setup/check', { baseURL: apiUrl }); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 31 | return data.isSetupComplete; |
| 32 | } catch (e) { |
| 33 | throw new Error('Cannot connect to server', { cause: e }); |
| 34 | } |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | export 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éraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 46 | |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 47 | const App = () => { |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 48 | const [displayWelcome, setDisplayWelcome] = useState<boolean>(getAccessToken() === undefined); |
Adrien Béraud | 6c93496 | 2021-06-07 10:13:26 -0400 | [diff] [blame] | 49 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 50 | console.log('App render'); |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 51 | |
| 52 | if (displayWelcome) { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 53 | return <WelcomeAnimation onComplete={() => setDisplayWelcome(false)} />; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 56 | return <Outlet />; |
idillon | d858c18 | 2022-09-16 13:18:26 -0400 | [diff] [blame] | 57 | }; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 58 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 59 | export default App; |