-
Notifications
You must be signed in to change notification settings - Fork 8
/
Application.jsx
132 lines (115 loc) · 4.34 KB
/
Application.jsx
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React from 'react';
import PropTypes from 'prop-types';
import $ from 'jquery';
import { connect } from 'react-redux';
import { CSSTransitionGroup } from 'react-transition-group';
import ErrorBoundary from './Components/Site/ErrorBoundary';
import NavBar from './Components/Site/NavBar';
import Router from './Router';
import {tryParseJSON} from './util';
import AlertPanel from './Components/Site/AlertPanel';
import * as actions from './actions';
class Application extends React.Component {
constructor(props) {
super(props);
this.router = new Router();
this.state = {
};
}
componentWillMount() {
if(!localStorage) {
this.setState({ incompatibleBrowser: true });
} else {
try {
let token = localStorage.getItem('token');
let refreshToken = localStorage.getItem('refreshToken');
if(refreshToken) {
const parsedToken = tryParseJSON(refreshToken);
if(parsedToken) {
this.props.setAuthTokens(token, parsedToken);
this.props.authenticate();
}
}
} catch(error) {
this.setState({ cannotLoad: true });
}
}
this.props.loadCards();
this.props.loadPacks();
this.props.loadRestrictedList();
$(document).ajaxError((event, xhr) => {
if(xhr.status === 403) {
this.props.navigate('/unauth');
}
});
this.props.connectLobby();
}
componentDidUpdate() {
if(!this.props.currentGame) {
this.props.setContextMenu([]);
}
}
render() {
let gameBoardVisible = this.props.currentGame && this.props.currentGame.started;
let component = this.router.resolvePath({
pathname: this.props.path,
user: this.props.user,
currentGame: this.props.currentGame
});
if(this.state.incompatibleBrowser) {
component = <AlertPanel type='error' message='Your browser does not provide the required functionality for this site to work. Please upgrade your browser. The site works best with a recet version of Chrome, Safari or Firefox' />;
} else if(this.state.cannotLoad) {
component = <AlertPanel type='error' message='This site requires the ability to store cookies and local site data to function. Please enable these features to use the site.' />;
}
let backgroundClass = 'bg';
if(gameBoardVisible && this.props.user) {
switch(this.props.user.settings.background) {
case 'BG1':
backgroundClass = 'bg-board';
break;
case 'BG2':
backgroundClass = 'bg-board2';
break;
default:
backgroundClass = '';
break;
}
}
return (<div className={ backgroundClass }>
<NavBar title='Doomtown Online' />
<div className='wrapper'>
<div className='container content'>
<ErrorBoundary navigate={ this.props.navigate } errorPath={ this.props.path } message={ 'We\'re sorry - something\'s gone wrong.' }>
<CSSTransitionGroup transitionName='pages' transitionEnterTimeout={ 600 } transitionLeaveTimeout={ 600 }>
{ component }
</CSSTransitionGroup>
</ErrorBoundary>
</div>
</div>
</div>);
}
}
Application.displayName = 'Application';
Application.propTypes = {
authenticate: PropTypes.func,
connectLobby: PropTypes.func,
currentGame: PropTypes.object,
loadCards: PropTypes.func,
loadPacks: PropTypes.func,
loadRestrictedList: PropTypes.func,
navigate: PropTypes.func,
path: PropTypes.string,
setAuthTokens: PropTypes.func,
setContextMenu: PropTypes.func,
token: PropTypes.string,
user: PropTypes.object
};
function mapStateToProps(state) {
return {
currentGame: state.lobby.currentGame,
path: state.navigation.path,
token: state.account.token,
user: state.account.user
};
}
export default connect(mapStateToProps, actions)(Application);