Adrien BĂ©raud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 1 | import React from 'react'; |
| 2 | import Header from '../components/Header' |
| 3 | import authManager from '../AuthManager' |
| 4 | import AccountList from '../components/AccountList'; |
| 5 | import CircularProgress from '@material-ui/core/CircularProgress'; |
| 6 | import { withRouter } from 'react-router-dom'; |
| 7 | import Card from '@material-ui/core/Card'; |
| 8 | import CardHeader from '@material-ui/core/CardHeader'; |
| 9 | import Container from '@material-ui/core/Container'; |
| 10 | import Account from '../../../model/Account'; |
| 11 | |
| 12 | class 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 | |
| 64 | export default withRouter(AccountSelection); |