simon | 26e79f7 | 2022-10-05 22:16:08 -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 | */ |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 18 | import { Container } from '@mui/material'; |
| 19 | import { Account, HttpStatusCode } from 'jami-web-common'; |
| 20 | import { useEffect } from 'react'; |
| 21 | import { useNavigate } from 'react-router-dom'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 22 | |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | import AccountPreferences from '../components/AccountPreferences'; |
| 24 | import Header from '../components/Header'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 25 | import ProcessingRequest from '../components/ProcessingRequest'; |
| 26 | import { setAccount } from '../redux/appSlice'; |
| 27 | import { useAppDispatch, useAppSelector } from '../redux/hooks'; |
| 28 | import { getAccessToken, setAccessToken } from '../utils/auth'; |
| 29 | import { apiUrl } from '../utils/constants'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 30 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 31 | export default function AccountSettings() { |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 32 | const dispatch = useAppDispatch(); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 33 | const navigate = useNavigate(); |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 34 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 35 | const { account } = useAppSelector((state) => state.userInfo); |
| 36 | const accessToken = getAccessToken(); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 37 | |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 38 | useEffect(() => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 39 | if (accessToken) { |
| 40 | const getAccount = async () => { |
| 41 | const url = new URL('/account', apiUrl); |
| 42 | let response: Response; |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 43 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 44 | 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éraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 59 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 60 | 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éraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 76 | return ( |
| 77 | <Container maxWidth="sm"> |
| 78 | <Header /> |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 79 | {account ? <AccountPreferences account={account} /> : <ProcessingRequest open={true} />} |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 80 | </Container> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 81 | ); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 82 | } |