blob: b09fb65eb0f21ef6d5dd3de4fd418080b31628b6 [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 */
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040018import { Container } from '@mui/material';
19import { Account, HttpStatusCode } from 'jami-web-common';
20import { useEffect } from 'react';
21import { useNavigate } from 'react-router-dom';
simon07b4eb02022-09-29 17:50:26 -040022
simon07b4eb02022-09-29 17:50:26 -040023import AccountPreferences from '../components/AccountPreferences';
24import Header from '../components/Header';
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040025import ProcessingRequest from '../components/ProcessingRequest';
26import { setAccount } from '../redux/appSlice';
27import { useAppDispatch, useAppSelector } from '../redux/hooks';
28import { getAccessToken, setAccessToken } from '../utils/auth';
29import { apiUrl } from '../utils/constants';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040030
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040031export default function AccountSettings() {
idillon531b6f22022-09-16 14:02:00 -040032 const dispatch = useAppDispatch();
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040033 const navigate = useNavigate();
idillon531b6f22022-09-16 14:02:00 -040034
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040035 const { account } = useAppSelector((state) => state.userInfo);
36 const accessToken = getAccessToken();
Adrien Béraud6ecaa402021-04-06 17:37:25 -040037
Adrien Béraud150b4782021-04-21 19:40:59 -040038 useEffect(() => {
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040039 if (accessToken) {
40 const getAccount = async () => {
41 const url = new URL('/account', apiUrl);
42 let response: Response;
idillon531b6f22022-09-16 14:02:00 -040043
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040044 try {
45 response = await fetch(url, {
46 method: 'GET',
47 mode: 'cors',
48 headers: {
49 Authorization: `Bearer ${accessToken}`,
50 },
51 referrerPolicy: 'no-referrer',
52 });
53 } catch (err) {
54 setAccessToken('');
55 dispatch(setAccount(undefined));
56 navigate('/', { replace: true });
57 return;
58 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040059
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040060 if (response.status === HttpStatusCode.Ok) {
61 const serializedAccount = await response.json();
62 const account = Account.from(serializedAccount);
63 dispatch(setAccount(account));
64 } else if (response.status === HttpStatusCode.Unauthorized) {
65 setAccessToken('');
66 dispatch(setAccount(undefined));
67 navigate('/', { replace: true });
68 }
69 };
70
71 getAccount();
72 }
73 }, [accessToken, dispatch, navigate]);
74
75 // TODO: Improve component and sub-components UI.
Adrien Béraud150b4782021-04-21 19:40:59 -040076 return (
77 <Container maxWidth="sm">
78 <Header />
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040079 {account ? <AccountPreferences account={account} /> : <ProcessingRequest open={true} />}
Adrien Béraud150b4782021-04-21 19:40:59 -040080 </Container>
simond47ef9e2022-09-28 22:24:28 -040081 );
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040082}