blob: ed580e2e3b4fee23301b301ffc2bfc80b19e6532 [file] [log] [blame]
simon07b4eb02022-09-29 17:50:26 -04001import { CircularProgress, Container } from '@mui/material';
simond47ef9e2022-09-28 22:24:28 -04002import { useEffect, useState } from 'react';
Adrien Béraud150b4782021-04-21 19:40:59 -04003import { useParams } from 'react-router';
simon07b4eb02022-09-29 17:50:26 -04004
simond47ef9e2022-09-28 22:24:28 -04005import Account from '../../../model/Account';
ervinanoh99655642022-09-01 15:11:31 -04006import { setAccountId, setAccountObject } from '../../redux/appSlice';
simon07b4eb02022-09-29 17:50:26 -04007import { useAppDispatch } from '../../redux/hooks';
8import authManager from '../AuthManager';
9import AccountPreferences from '../components/AccountPreferences';
10import Header from '../components/Header';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040011
Adrien Béraud150b4782021-04-21 19:40:59 -040012const AccountSettings = (props) => {
simond47ef9e2022-09-28 22:24:28 -040013 console.log('ACCOUNT SETTINGS', props.account);
14 const accountId = props.accountId || useParams().accountId;
idillon531b6f22022-09-16 14:02:00 -040015 const dispatch = useAppDispatch();
16
simond47ef9e2022-09-28 22:24:28 -040017 const [state, setState] = useState({ loaded: false });
Adrien Béraud6ecaa402021-04-06 17:37:25 -040018
Adrien Béraud150b4782021-04-21 19:40:59 -040019 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040020 dispatch(setAccountId(accountId));
idillon531b6f22022-09-16 14:02:00 -040021
simond47ef9e2022-09-28 22:24:28 -040022 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éraud6ecaa402021-04-06 17:37:25 -040035
Adrien Béraud150b4782021-04-21 19:40:59 -040036 return (
37 <Container maxWidth="sm">
38 <Header />
simond47ef9e2022-09-28 22:24:28 -040039 {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éraud150b4782021-04-21 19:40:59 -040052 </Container>
simond47ef9e2022-09-28 22:24:28 -040053 );
54};
Adrien Béraud6ecaa402021-04-06 17:37:25 -040055
simond47ef9e2022-09-28 22:24:28 -040056export default AccountSettings;