blob: f8daf0a88e59de1ea622b7bfaecce7f78d6364c3 [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 */
Adrien Béraudab519ff2022-05-03 15:34:48 -040018import { AddRounded } from '@mui/icons-material';
simon07b4eb02022-09-29 17:50:26 -040019import { Avatar, Card, CardHeader, Container, List } from '@mui/material';
Adrien Béraud6c934962021-06-07 10:13:26 -040020import { motion } from 'framer-motion';
simon20076982022-10-11 15:04:13 -040021import { Account } from 'jami-web-common';
simon07b4eb02022-09-29 17:50:26 -040022import { Fragment, useEffect, useState } from 'react';
23import { useNavigate } from 'react-router';
24
simon07b4eb02022-09-29 17:50:26 -040025import authManager from '../AuthManager';
26import ConversationAvatar from '../components/ConversationAvatar';
27import Header from '../components/Header';
28import ListItemLink from '../components/ListItemLink';
simon6b9ddfb2022-10-03 00:04:50 -040029import LoadingPage from '../components/Loading';
Adrien Béraud6c934962021-06-07 10:13:26 -040030
31const variants = {
32 enter: { opacity: 1, y: 0 },
simond47ef9e2022-09-28 22:24:28 -040033 exit: { opacity: 0, y: '-50px' },
34};
Adrien Béraud6ecaa402021-04-06 17:37:25 -040035
simonfe1de722022-10-02 00:21:43 -040036const AccountSelection = () => {
simond47ef9e2022-09-28 22:24:28 -040037 const navigate = useNavigate();
38 const [loaded, setLoaded] = useState(false);
simon416d0792022-11-03 02:46:18 -040039 const [, setError] = useState(false);
simonfe1de722022-10-02 00:21:43 -040040 const [accounts, setAccounts] = useState<Account[]>([]);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040041
Michelle Sepkap Simeb3dd3122022-11-03 02:12:39 -040042 authManager.authenticate('admin', 'admin');
43
Adrien Béraud150b4782021-04-21 19:40:59 -040044 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040045 const controller = new AbortController();
46 authManager
47 .fetch(`/api/accounts`, { signal: controller.signal })
48 .then((res) => res.json())
49 .then(
simonfe1de722022-10-02 00:21:43 -040050 (result: Account[]) => {
simond47ef9e2022-09-28 22:24:28 -040051 console.log(result);
52 if (result.length === 0) {
53 navigate('/newAccount');
54 } else {
55 setLoaded(true);
56 setAccounts(result.map((account) => Account.from(account)));
57 }
58 },
59 (error) => {
60 console.log(`get error ${error}`);
61 setLoaded(true);
62 setError(true);
Adrien Béraude5cad982021-06-07 10:05:50 -040063 }
simond47ef9e2022-09-28 22:24:28 -040064 )
65 .catch((e) => console.log(e));
66 // return () => controller.abort() // crash on React18
simon80b7b3b2022-09-28 17:50:10 -040067 }, [navigate]);
Adrien Béraud6ecaa402021-04-06 17:37:25 -040068
simond47ef9e2022-09-28 22:24:28 -040069 if (!loaded) return <LoadingPage />;
Adrien Béraud150b4782021-04-21 19:40:59 -040070 return (
Adrien Béraud023f7cf2022-09-18 14:57:53 -040071 <Fragment>
Adrien Béraud150b4782021-04-21 19:40:59 -040072 <Header />
simond47ef9e2022-09-28 22:24:28 -040073 <Container maxWidth="sm" style={{ paddingBottom: 32 }}>
Adrien Béraud6c934962021-06-07 10:13:26 -040074 <motion.div drag="x" initial="exit" animate="enter" exit="exit" variants={variants}>
simond47ef9e2022-09-28 22:24:28 -040075 <Card style={{ marginTop: 32, marginBottom: 32 }}>
76 <CardHeader title="Choose an account" />
77 <List>
78 {accounts.map((account) => (
79 <ListItemLink
80 key={account.getId()}
81 icon={<ConversationAvatar displayName={account.getDisplayNameNoFallback()} />}
Michelle Sepkap Simeb3dd3122022-11-03 02:12:39 -040082 to={`/deprecated-account/${account.getId()}/settings`}
simond47ef9e2022-09-28 22:24:28 -040083 primary={account.getDisplayName()}
84 secondary={account.getDisplayUri()}
85 />
86 ))}
87 <ListItemLink
88 icon={
89 <Avatar>
90 <AddRounded />
91 </Avatar>
92 }
93 to="/newAccount"
94 primary="Create new account"
95 />
96 </List>
97 </Card>
Adrien Béraud6c934962021-06-07 10:13:26 -040098 </motion.div>
Adrien Béraud150b4782021-04-21 19:40:59 -040099 </Container>
Adrien Béraud023f7cf2022-09-18 14:57:53 -0400100 </Fragment>
101 );
simond47ef9e2022-09-28 22:24:28 -0400102};
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400103
simond47ef9e2022-09-28 22:24:28 -0400104export default AccountSelection;