-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathLogoutLink.js
More file actions
52 lines (40 loc) · 1.33 KB
/
LogoutLink.js
File metadata and controls
52 lines (40 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React, { PropTypes } from 'react';
import utils from './../utils';
import context from './../context';
import UserActions from './../actions/UserActions';
export default class LogoutLink extends React.Component {
static contextTypes = {
router: PropTypes.object.isRequired
};
static propTypes = {
endpoint: PropTypes.string
};
state = {
disabled: false
};
_performRedirect(primaryRedirectTo) {
var router = context.getRouter();
var homeRoute = router.getHomeRoute();
var loginRoute = router.getLoginRoute();
var redirectTo = primaryRedirectTo || (homeRoute || {}).path || (loginRoute || {}).path || '/';
this.context.router.push(redirectTo);
}
onClick(e) {
e.preventDefault();
let primaryRedirectTo = this.props.redirectTo;
if (!this.state.disabled) {
this.setState({ disabled: true });
UserActions.logout({ endpoint: this.props.endpoint }, () => {
this._performRedirect(primaryRedirectTo);
});
}
}
render() {
var selectedProps = utils.excludeProps(['redirectTo', 'href', 'onClick', 'disabled', 'children', 'endpoint'], this.props);
return (
<a href='#' onClick={this.onClick.bind(this)} disabled={this.state.disabled} {...selectedProps}>
{ this.props.children ? this.props.children : 'Logout'}
</a>
);
}
}