simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 18 | import CircularProgress from '@mui/material/CircularProgress'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 19 | import Container from '@mui/material/Container'; |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 20 | import { useEffect, useState } from 'react'; |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 21 | import { useParams } from 'react-router-dom'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 22 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 23 | import Account from '../../../model/Account'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 24 | import authManager from '../AuthManager'; |
| 25 | import AccountPreferences from '../components/AccountPreferences'; |
| 26 | import Header from '../components/Header'; |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 27 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 28 | type ServerOverviewProps = { |
| 29 | accountId?: string; |
| 30 | }; |
| 31 | |
| 32 | const ServerOverview = (props: ServerOverviewProps) => { |
| 33 | const [account, setAccount] = useState<Account | null>(null); |
| 34 | const params = useParams(); |
| 35 | const accountId = props.accountId || params.accountId; |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 36 | |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 37 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 38 | const controller = new AbortController(); |
| 39 | authManager |
| 40 | .fetch(`/api/serverConfig`, { signal: controller.signal }) |
| 41 | .then((res) => res.json()) |
| 42 | .then((result) => { |
| 43 | console.log(result); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 44 | setAccount(Account.from(result)); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 45 | }) |
| 46 | .catch((e) => console.log(e)); |
| 47 | // return () => controller.abort() // crash on React18 |
| 48 | }, [accountId]); |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 49 | |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 50 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 51 | <Container maxWidth="sm" className="app"> |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 52 | <Header /> |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 53 | {account != null ? <AccountPreferences account={account} /> : <CircularProgress />} |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 54 | </Container> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 55 | ); |
| 56 | }; |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 57 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 58 | export default ServerOverview; |