simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame^] | 1 | import { CircularProgress, Container } from '@mui/material'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 2 | import { useEffect, useState } from 'react'; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 3 | import { useParams } from 'react-router'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame^] | 4 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 5 | import Account from '../../../model/Account'; |
ervinanoh | 9965564 | 2022-09-01 15:11:31 -0400 | [diff] [blame] | 6 | import { setAccountId, setAccountObject } from '../../redux/appSlice'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame^] | 7 | import { useAppDispatch } from '../../redux/hooks'; |
| 8 | import authManager from '../AuthManager'; |
| 9 | import AccountPreferences from '../components/AccountPreferences'; |
| 10 | import Header from '../components/Header'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 11 | |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 12 | const AccountSettings = (props) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 13 | console.log('ACCOUNT SETTINGS', props.account); |
| 14 | const accountId = props.accountId || useParams().accountId; |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 15 | const dispatch = useAppDispatch(); |
| 16 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 17 | const [state, setState] = useState({ loaded: false }); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 18 | |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 19 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 20 | dispatch(setAccountId(accountId)); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 21 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 22 | const controller = new AbortController(); |
| 23 | authManager |
| 24 | .fetch(`/api/accounts/${accountId}`, { signal: controller.signal }) |
| 25 | .then((res) => res.json()) |
| 26 | .then((result) => { |
| 27 | let account = Account.from(result); |
| 28 | account.setDevices(result.devices); |
| 29 | dispatch(setAccountObject(account)); |
| 30 | setState({ loaded: true, account: account }); |
| 31 | }) |
| 32 | .catch((e) => console.log(e)); |
| 33 | // return () => controller.abort() // crash on React18 |
| 34 | }, [accountId]); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 35 | |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 36 | return ( |
| 37 | <Container maxWidth="sm"> |
| 38 | <Header /> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 39 | {state.loaded ? ( |
| 40 | <AccountPreferences |
| 41 | account={state.account} |
| 42 | onAccontChanged={(account) => |
| 43 | setState((state) => { |
| 44 | state.account = account; |
| 45 | return state; |
| 46 | }) |
| 47 | } |
| 48 | /> |
| 49 | ) : ( |
| 50 | <CircularProgress /> |
| 51 | )} |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 52 | </Container> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 53 | ); |
| 54 | }; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 55 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 56 | export default AccountSettings; |