blob: 65728d8b46f4525336858cdb6ee7809cf7edfe25 [file] [log] [blame]
Adrien Béraud023f7cf2022-09-18 14:57:53 -04001import { Fragment, useEffect, useState } from 'react'
Adrien Béraudab519ff2022-05-03 15:34:48 -04002import { Avatar, Card, CardHeader, Container, List } from '@mui/material';
Adrien Béraud150b4782021-04-21 19:40:59 -04003import Header from '../components/Header'
Adrien Béraud150b4782021-04-21 19:40:59 -04004import authManager from '../AuthManager'
Adrien Béraud6ecaa402021-04-06 17:37:25 -04005import Account from '../../../model/Account';
Adrien Béraud150b4782021-04-21 19:40:59 -04006import LoadingPage from '../components/loading';
Adrien Béraud88a52442021-04-26 12:11:41 -04007import ListItemLink from '../components/ListItemLink';
8import ConversationAvatar from '../components/ConversationAvatar';
Adrien Béraudab519ff2022-05-03 15:34:48 -04009import { AddRounded } from '@mui/icons-material';
10import { useNavigate } from 'react-router';
Adrien Béraud6c934962021-06-07 10:13:26 -040011import { motion } from 'framer-motion';
12
13const variants = {
14 enter: { opacity: 1, y: 0 },
15 exit: { opacity: 0, y: "-50px" },
16}
Adrien Béraud6ecaa402021-04-06 17:37:25 -040017
Adrien Béraud150b4782021-04-21 19:40:59 -040018const AccountSelection = (props) => {
Adrien Béraudab519ff2022-05-03 15:34:48 -040019 const navigate = useNavigate()
idillon28996962022-09-06 17:49:41 -040020 const [loaded, setLoaded] = useState(false)
21 const [error, setError] = useState(false)
22 const [accounts, setAccounts] = useState([])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040023
Adrien Béraud150b4782021-04-21 19:40:59 -040024 useEffect(() => {
25 const controller = new AbortController()
26 authManager.fetch(`/api/accounts`, {signal: controller.signal})
27 .then(res => res.json())
28 .then(result => {
29 console.log(result)
Adrien Béraude5cad982021-06-07 10:05:50 -040030 if (result.length === 0) {
Adrien Béraudab519ff2022-05-03 15:34:48 -040031 navigate('/newAccount')
Adrien Béraude5cad982021-06-07 10:05:50 -040032 } else {
idillon28996962022-09-06 17:49:41 -040033 setLoaded(true)
34 setAccounts(result.map(account => Account.from(account)))
Adrien Béraude5cad982021-06-07 10:05:50 -040035 }
Adrien Béraud150b4782021-04-21 19:40:59 -040036 }, error => {
37 console.log(`get error ${error}`)
idillon28996962022-09-06 17:49:41 -040038 setLoaded(true)
39 setError(true)
Adrien Béraud88a52442021-04-26 12:11:41 -040040 }).catch(e => console.log(e))
idillon28996962022-09-06 17:49:41 -040041 // return () => controller.abort() // crash on React18
Adrien Béraud150b4782021-04-21 19:40:59 -040042 }, [])
Adrien Béraud6ecaa402021-04-06 17:37:25 -040043
idillon28996962022-09-06 17:49:41 -040044 if (!loaded)
Adrien Béraud150b4782021-04-21 19:40:59 -040045 return <LoadingPage />
46 return (
Adrien Béraud023f7cf2022-09-18 14:57:53 -040047 <Fragment>
Adrien Béraud150b4782021-04-21 19:40:59 -040048 <Header />
49 <Container maxWidth="sm" style={{paddingBottom:32}}>
Adrien Béraud6c934962021-06-07 10:13:26 -040050 <motion.div drag="x" initial="exit" animate="enter" exit="exit" variants={variants}>
Adrien Béraud150b4782021-04-21 19:40:59 -040051 <Card style={{marginTop:32, marginBottom:32}}>
52 <CardHeader title="Choose an account" />
Adrien Béraud88a52442021-04-26 12:11:41 -040053 <List>
idillon28996962022-09-06 17:49:41 -040054 {accounts.map(account => <ListItemLink key={account.getId()}
Adrien Béraud88a52442021-04-26 12:11:41 -040055 icon={<ConversationAvatar displayName={account.getDisplayNameNoFallback()} />}
56 to={`/account/${account.getId()}/settings`}
57 primary={account.getDisplayName()}
58 secondary={account.getDisplayUri()} />)}
59 <ListItemLink
60 icon={<Avatar><AddRounded /></Avatar>}
61 to='/newAccount'
62 primary="Create new account" />
63 </List>
Adrien Béraud150b4782021-04-21 19:40:59 -040064 </Card>
Adrien Béraud6c934962021-06-07 10:13:26 -040065 </motion.div>
Adrien Béraud150b4782021-04-21 19:40:59 -040066 </Container>
Adrien Béraud023f7cf2022-09-18 14:57:53 -040067 </Fragment>
68 );
Adrien Béraud6ecaa402021-04-06 17:37:25 -040069}
70
Adrien Béraud88a52442021-04-26 12:11:41 -040071export default AccountSelection