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 | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 18 | import { CircularProgress, Container } from '@mui/material'; |
simon | 2007698 | 2022-10-11 15:04:13 -0400 | [diff] [blame] | 19 | import { Account } from 'jami-web-common'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 20 | import { useEffect, useState } from 'react'; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 21 | import { useParams } from 'react-router'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 22 | |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | import authManager from '../AuthManager'; |
| 24 | import AccountPreferences from '../components/AccountPreferences'; |
| 25 | import Header from '../components/Header'; |
simon | d8ca2f2 | 2022-10-11 23:30:55 -0400 | [diff] [blame] | 26 | import { setAccountId, setAccountObject } from '../redux/appSlice'; |
| 27 | import { useAppDispatch } from '../redux/hooks'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 28 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 29 | type AccountSettingsProps = { |
| 30 | accountId?: string; |
| 31 | account?: Account; |
| 32 | }; |
| 33 | |
| 34 | const AccountSettings = (props: AccountSettingsProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 35 | console.log('ACCOUNT SETTINGS', props.account); |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 36 | const params = useParams(); |
| 37 | const accountId = props.accountId || params.accountId; |
| 38 | |
| 39 | if (accountId == null) { |
| 40 | throw new Error('Missing accountId'); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 41 | } |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 42 | |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 43 | const dispatch = useAppDispatch(); |
| 44 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 45 | const [account, setAccount] = useState<Account | null>(null); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 46 | |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 47 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 48 | dispatch(setAccountId(accountId)); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 49 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 50 | const controller = new AbortController(); |
| 51 | authManager |
| 52 | .fetch(`/api/accounts/${accountId}`, { signal: controller.signal }) |
| 53 | .then((res) => res.json()) |
| 54 | .then((result) => { |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 55 | const account = Account.from(result); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 56 | account.setDevices(result.devices); |
| 57 | dispatch(setAccountObject(account)); |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 58 | setAccount(account); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 59 | }) |
| 60 | .catch((e) => console.log(e)); |
| 61 | // return () => controller.abort() // crash on React18 |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 62 | }, [accountId, dispatch]); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 63 | |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 64 | return ( |
| 65 | <Container maxWidth="sm"> |
| 66 | <Header /> |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 67 | {account != null ? ( |
| 68 | <AccountPreferences account={account} onAccountChanged={(account: Account) => setAccount(account)} /> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 69 | ) : ( |
| 70 | <CircularProgress /> |
| 71 | )} |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 72 | </Container> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 73 | ); |
| 74 | }; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 75 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 76 | export default AccountSettings; |