Commit 0ed56f80 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Fixed some warnings

parent 38ae0956
...@@ -13,7 +13,9 @@ class LoginLogout extends React.Component { ...@@ -13,7 +13,9 @@ class LoginLogout extends React.Component {
api: PropTypes.object.isRequired, api: PropTypes.object.isRequired,
user: PropTypes.object, user: PropTypes.object,
login: PropTypes.func.isRequired, login: PropTypes.func.isRequired,
logout: PropTypes.func.isRequired logout: PropTypes.func.isRequired,
variant: PropTypes.string,
color: PropTypes.string
} }
static styles = theme => ({ static styles = theme => ({
...@@ -24,6 +26,7 @@ class LoginLogout extends React.Component { ...@@ -24,6 +26,7 @@ class LoginLogout extends React.Component {
marginRight: theme.spacing.unit * 2 marginRight: theme.spacing.unit * 2
} }
}, },
button: {}, // to allow overrides
errorText: { errorText: {
marginTop: theme.spacing.unit, marginTop: theme.spacing.unit,
marginBottom: theme.spacing.unit marginBottom: theme.spacing.unit
...@@ -44,19 +47,33 @@ class LoginLogout extends React.Component { ...@@ -44,19 +47,33 @@ class LoginLogout extends React.Component {
failure: false failure: false
} }
componentDidMount() {
this._ismounted = true
}
componentWillUnmount() {
this._ismounted = false
}
handleLoginDialogClosed(withLogin) { handleLoginDialogClosed(withLogin) {
this.setState({loginDialogOpen: false}) this.setState({loginDialogOpen: false})
if (withLogin) { if (withLogin) {
this.setState({loggingIn: true}) if (this._ismounted) {
this.setState({loggingIn: true})
}
this.props.login(this.state.userName, this.state.password, (success) => { this.props.login(this.state.userName, this.state.password, (success) => {
if (success) { if (this._ismounted) {
this.setState({loggingIn: false, loginDialogOpen: false, failure: false}) if (success) {
} else { this.setState({loggingIn: false, loginDialogOpen: false, failure: false})
this.setState({loggingIn: false, failure: true, loginDialogOpen: true}) } else {
this.setState({loggingIn: false, failure: true, loginDialogOpen: true})
}
} }
}) })
} else { } else {
this.setState({loggingIn: false, failure: false, userName: '', password: '', loginDialogOpen: false}) if (this._ismounted) {
this.setState({loggingIn: false, failure: false, userName: '', password: '', loginDialogOpen: false})
}
} }
} }
...@@ -71,7 +88,7 @@ class LoginLogout extends React.Component { ...@@ -71,7 +88,7 @@ class LoginLogout extends React.Component {
} }
render() { render() {
const { classes, user, ...otherProps } = this.props const { classes, user, variant, color } = this.props
const { loggingIn, failure } = this.state const { loggingIn, failure } = this.state
if (user) { if (user) {
return ( return (
...@@ -79,13 +96,20 @@ class LoginLogout extends React.Component { ...@@ -79,13 +96,20 @@ class LoginLogout extends React.Component {
<Typography color="inherit" variant="body1"> <Typography color="inherit" variant="body1">
Welcome, {user.first_name} {user.last_name} Welcome, {user.first_name} {user.last_name}
</Typography> </Typography>
<Button className={classes.button} {...otherProps} onClick={this.handleLogout}>Logout</Button> <Button
className={classes.button}
variant={variant} color={color}
onClick={this.handleLogout}
>Logout</Button>
</div> </div>
) )
} else { } else {
return ( return (
<div className={classes.root}> <div className={classes.root}>
<Button className={classes.button} {...otherProps} onClick={() => this.setState({loginDialogOpen: true})}>Login</Button> <Button
className={classes.button} variant={variant} color={color}
onClick={() => this.setState({loginDialogOpen: true})}
>Login</Button>
<Dialog <Dialog
open={this.state.loginDialogOpen} open={this.state.loginDialogOpen}
onClose={() => this.handleLoginDialogClosed(false)} onClose={() => this.handleLoginDialogClosed(false)}
......
...@@ -4,7 +4,7 @@ import { withErrors } from './errors' ...@@ -4,7 +4,7 @@ import { withErrors } from './errors'
import { UploadRequest } from '@navjobs/upload' import { UploadRequest } from '@navjobs/upload'
import Swagger from 'swagger-client' import Swagger from 'swagger-client'
import { apiBase } from '../config' import { apiBase } from '../config'
import { Typography, FormGroup, FormLabel, Button, withStyles } from '@material-ui/core' import { Typography, withStyles } from '@material-ui/core'
import LoginLogout from './LoginLogout' import LoginLogout from './LoginLogout'
const ApiContext = React.createContext() const ApiContext = React.createContext()
...@@ -290,7 +290,7 @@ export class ApiProvider extends React.Component { ...@@ -290,7 +290,7 @@ export class ApiProvider extends React.Component {
state = { state = {
api: new Api(), api: new Api(),
user: null, user: null,
login: (userName, password, callback) => { login: (userName, password, successCallback) => {
Api.createSwaggerClient(userName, password) Api.createSwaggerClient(userName, password)
.catch(this.state.api.handleApiError) .catch(this.state.api.handleApiError)
.then(client => { .then(client => {
...@@ -304,9 +304,9 @@ export class ApiProvider extends React.Component { ...@@ -304,9 +304,9 @@ export class ApiProvider extends React.Component {
if (response) { if (response) {
const user = response.body const user = response.body
this.setState({api: new Api(user), user: user}) this.setState({api: new Api(user), user: user})
callback(true) successCallback(true)
} else { } else {
callback(false) successCallback(false)
} }
}) })
}) })
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment