Skip to content

Commit 0fe184f

Browse files
author
Sven
committed
refactor: login to functional component
1 parent cc12748 commit 0fe184f

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed
Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
1-
import React, { Component } from 'react'
1+
import React from 'react'
22
import PropTypes from 'prop-types'
33
import { Redirect } from 'react-router-dom'
44
import { Button, Container } from 'reactstrap'
55

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+
}
2811

29-
return (
12+
export default function Login({ location, isAuthenticated, authenticateWithCb }) {
13+
const { from } = location.state || { from: { pathname: '/' } }
14+
return isAuthenticated ? <Redirect to={from} />
15+
: (
3016
<Container className="text-center page-layout__viewport">
3117
<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>
3319
</Container>
3420
)
35-
}
3621
}

src/store/fakeAuth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ function authenticate() {
77
}
88
}
99

10-
export function authenticateWithCb(cb) {
10+
export function authenticateWithCb() {
1111
return (dispatch) => {
1212
dispatch(authenticate())
13-
setTimeout(cb, 100)
13+
localStorage.setItem('LoginState', 'true')
1414
}
1515
}
1616

0 commit comments

Comments
 (0)