blob: da631d331acbab8a16e5a24a7816b14e27188d2e [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 */
simon07b4eb02022-09-29 17:50:26 -040018import { CircularProgress, Container } from '@mui/material';
simond47ef9e2022-09-28 22:24:28 -040019import { useEffect, useState } from 'react';
Adrien Béraud150b4782021-04-21 19:40:59 -040020import { useParams } from 'react-router';
simon07b4eb02022-09-29 17:50:26 -040021
simond47ef9e2022-09-28 22:24:28 -040022import Account from '../../../model/Account';
ervinanoh99655642022-09-01 15:11:31 -040023import { setAccountId, setAccountObject } from '../../redux/appSlice';
simon07b4eb02022-09-29 17:50:26 -040024import { useAppDispatch } from '../../redux/hooks';
25import authManager from '../AuthManager';
26import AccountPreferences from '../components/AccountPreferences';
27import Header from '../components/Header';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040028
simonfe1de722022-10-02 00:21:43 -040029type AccountSettingsProps = {
30 accountId?: string;
31 account?: Account;
32};
33
34const AccountSettings = (props: AccountSettingsProps) => {
simond47ef9e2022-09-28 22:24:28 -040035 console.log('ACCOUNT SETTINGS', props.account);
simonfe1de722022-10-02 00:21:43 -040036 const params = useParams();
37 const accountId = props.accountId || params.accountId;
38
39 if (accountId == null) {
40 throw new Error('Missing accountId');
simon80b7b3b2022-09-28 17:50:10 -040041 }
simonfe1de722022-10-02 00:21:43 -040042
idillon531b6f22022-09-16 14:02:00 -040043 const dispatch = useAppDispatch();
44
simonfe1de722022-10-02 00:21:43 -040045 const [account, setAccount] = useState<Account | null>(null);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040046
Adrien Béraud150b4782021-04-21 19:40:59 -040047 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040048 dispatch(setAccountId(accountId));
idillon531b6f22022-09-16 14:02:00 -040049
simond47ef9e2022-09-28 22:24:28 -040050 const controller = new AbortController();
51 authManager
52 .fetch(`/api/accounts/${accountId}`, { signal: controller.signal })
53 .then((res) => res.json())
54 .then((result) => {
simonfe1de722022-10-02 00:21:43 -040055 const account = Account.from(result);
simond47ef9e2022-09-28 22:24:28 -040056 account.setDevices(result.devices);
57 dispatch(setAccountObject(account));
simonfe1de722022-10-02 00:21:43 -040058 setAccount(account);
simond47ef9e2022-09-28 22:24:28 -040059 })
60 .catch((e) => console.log(e));
61 // return () => controller.abort() // crash on React18
simon80b7b3b2022-09-28 17:50:10 -040062 }, [accountId, dispatch]);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040063
Adrien Béraud150b4782021-04-21 19:40:59 -040064 return (
65 <Container maxWidth="sm">
66 <Header />
simonfe1de722022-10-02 00:21:43 -040067 {account != null ? (
68 <AccountPreferences account={account} onAccountChanged={(account: Account) => setAccount(account)} />
simond47ef9e2022-09-28 22:24:28 -040069 ) : (
70 <CircularProgress />
71 )}
Adrien Béraud150b4782021-04-21 19:40:59 -040072 </Container>
simond47ef9e2022-09-28 22:24:28 -040073 );
74};
Adrien Béraud6ecaa402021-04-06 17:37:25 -040075
simond47ef9e2022-09-28 22:24:28 -040076export default AccountSettings;