Michelle Sepkap Sime | b3dd312 | 2022-11-03 02:12:39 -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 | */ |
| 18 | import { CircularProgress, Container } from '@mui/material'; |
| 19 | import { Account } from 'jami-web-common'; |
| 20 | import { useEffect, useState } from 'react'; |
| 21 | import { useParams } from 'react-router'; |
| 22 | |
| 23 | import authManager from '../AuthManager'; |
| 24 | import AccountPreferences from '../components/AccountPreferences'; |
| 25 | import Header from '../components/Header'; |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 26 | import { setAccountId } from '../redux/appSlice'; |
Michelle Sepkap Sime | b3dd312 | 2022-11-03 02:12:39 -0400 | [diff] [blame] | 27 | import { useAppDispatch } from '../redux/hooks'; |
| 28 | |
| 29 | type AccountSettingsProps = { |
| 30 | accountId?: string; |
| 31 | account?: Account; |
| 32 | }; |
| 33 | |
| 34 | const DeprecatedAccountSettings = (props: AccountSettingsProps) => { |
| 35 | console.log('ACCOUNT SETTINGS', props.account); |
| 36 | const params = useParams(); |
| 37 | const accountId = props.accountId || params.accountId; |
| 38 | |
| 39 | if (accountId == null) { |
| 40 | throw new Error('Missing accountId'); |
| 41 | } |
| 42 | |
| 43 | const dispatch = useAppDispatch(); |
| 44 | |
| 45 | const [localAccount, setLocalAccount] = useState<Account | null>(null); |
| 46 | |
| 47 | useEffect(() => { |
| 48 | dispatch(setAccountId(accountId)); |
| 49 | |
| 50 | const controller = new AbortController(); |
| 51 | authManager |
| 52 | .fetch(`/api/accounts/${accountId}`, { signal: controller.signal }) |
| 53 | .then((res) => res.json()) |
| 54 | .then((result) => { |
| 55 | console.log(result); |
| 56 | const account = Account.from(result); |
| 57 | account.setDevices(result.devices); |
Michelle Sepkap Sime | b3dd312 | 2022-11-03 02:12:39 -0400 | [diff] [blame] | 58 | setLocalAccount(account); |
| 59 | }) |
| 60 | .catch((e) => console.log(e)); |
| 61 | // return () => controller.abort() // crash on React18 |
| 62 | }, [accountId, dispatch]); |
| 63 | |
| 64 | return ( |
| 65 | <Container maxWidth="sm"> |
| 66 | <Header /> |
| 67 | {localAccount != null ? <AccountPreferences account={localAccount} /> : <CircularProgress />} |
| 68 | </Container> |
| 69 | ); |
| 70 | }; |
| 71 | |
| 72 | export default DeprecatedAccountSettings; |