|
1 | | -import React, { Component } from 'react' |
| 1 | +import React from 'react' |
2 | 2 | import PropTypes from 'prop-types' |
3 | 3 | import { Redirect } from 'react-router-dom' |
4 | 4 | import { Button, Container } from 'reactstrap' |
5 | 5 |
|
6 | | -export default class Login extends Component { |
7 | | - static propTypes = { |
8 | | - authenticateWithCb: PropTypes.func.isRequired, |
9 | | - location: PropTypes.object.isRequired, |
10 | | - isAuthenticated: PropTypes.bool.isRequired, |
11 | | - } |
12 | | - |
13 | | - login = () => { |
14 | | - this.props.authenticateWithCb(() => { |
15 | | - localStorage.setItem('LoginState', 'true') |
16 | | - }) |
17 | | - } |
18 | | - |
19 | | - render() { |
20 | | - const { from } = this.props.location.state || { from: { pathname: '/' } } |
21 | | - const { isAuthenticated } = this.props |
22 | | - |
23 | | - if (isAuthenticated) { |
24 | | - return ( |
25 | | - <Redirect to={from} /> |
26 | | - ) |
27 | | - } |
| 6 | +Login.propTypes = { |
| 7 | + authenticateWithCb: PropTypes.func.isRequired, |
| 8 | + location: PropTypes.object.isRequired, |
| 9 | + isAuthenticated: PropTypes.bool.isRequired, |
| 10 | +} |
28 | 11 |
|
29 | | - return ( |
| 12 | +export default function Login({ location, isAuthenticated, authenticateWithCb }) { |
| 13 | + const { from } = location.state || { from: { pathname: '/' } } |
| 14 | + return isAuthenticated ? <Redirect to={from} /> |
| 15 | + : ( |
30 | 16 | <Container className="text-center page-layout__viewport"> |
31 | 17 | <p>You must log in to view the page at {from.pathname}</p> |
32 | | - <Button color="primary" onClick={this.login}>Log in</Button> |
| 18 | + <Button color="primary" onClick={authenticateWithCb}>Log in</Button> |
33 | 19 | </Container> |
34 | 20 | ) |
35 | | - } |
36 | 21 | } |
0 commit comments