blob: 2a4c99b08ad1d6c4f558feca2ac0643b4cb3510e [file] [log] [blame]
Adrien BĂ©raud6ecaa402021-04-06 17:37:25 -04001import React from 'react';
2import Header from '../components/Header'
3import authManager from '../AuthManager'
4import AccountList from '../components/AccountList';
5import CircularProgress from '@material-ui/core/CircularProgress';
6import { withRouter } from 'react-router-dom';
7import Card from '@material-ui/core/Card';
8import CardHeader from '@material-ui/core/CardHeader';
9import Container from '@material-ui/core/Container';
10import Account from '../../../model/Account';
11
12class AccountSelection extends React.Component {
13
14 constructor(props) {
15 super(props)
16 this.controller = new AbortController()
17 this.state = {
18 loaded: false,
19 error: false,
20 accounts: []
21 }
22 }
23
24 componentDidMount() {
25 if (!this.state.loaded) {
26 authManager.fetch('/api/accounts', {signal: this.controller.signal})
27 .then(res => res.json())
28 .then(result => {
29 console.log(result)
30 this.setState({
31 loaded: true,
32 accounts: result.map(account => Account.from(account)),
33 })
34 }, error => {
35 console.log(`get error ${error}`)
36 this.setState({
37 loaded: true,
38 error: true
39 })
40 })
41 }
42 }
43 componentWillUnmount() {
44 this.controller.abort()
45 }
46
47 render() {
48 if (!this.state.loaded)
49 return <Container><CircularProgress /></Container>
50 return (
51 <div className="app" style={{marginBottom:32}} >
52 <Header />
53 <Container>
54 <Card style={{marginTop:32, marginBottom:32}}>
55 <CardHeader title="Choose an account" />
56 <AccountList accounts={this.state.accounts} onClick={account => this.props.history.push(`/account/${account.getId()}`)} />
57 </Card>
58 </Container>
59 </div>
60 )
61 }
62}
63
64export default withRouter(AccountSelection);