ensl_gathers/lib/react/main.jsx

243 lines
6 KiB
React
Raw Normal View History

2015-10-01 10:22:23 +00:00
"use strict";
var App = React.createClass({
2015-10-29 19:50:38 +00:00
getInitialState() {
let updateTitle = true;
2016-01-02 21:32:26 +00:00
let showEventsPanel = true;
2015-10-29 19:50:38 +00:00
2016-01-02 21:32:26 +00:00
if (storageAvailable('localStorage')) {
if (localStorage.getItem("updateTitle") !== null) {
updateTitle = JSON.parse(localStorage.getItem("updateTitle"));
}
if (localStorage.getItem("showEventsPanel") !== null) {
showEventsPanel = JSON.parse(localStorage.getItem("showEventsPanel"));
}
2015-10-29 19:50:38 +00:00
}
return {
2015-12-29 16:26:11 +00:00
updateTitle: updateTitle,
2016-01-02 21:32:26 +00:00
showEventsPanel: showEventsPanel,
2015-12-29 16:26:11 +00:00
events: []
2015-10-29 19:50:38 +00:00
};
},
2015-10-01 10:22:23 +00:00
getDefaultProps() {
return {
gather: {
gatherers: []
},
users: [],
2015-10-02 14:53:11 +00:00
messages: [],
maps: [],
2015-11-15 16:26:32 +00:00
user: null,
2015-10-02 14:53:11 +00:00
servers: [],
archive: [],
2015-11-15 16:26:32 +00:00
socket: null,
2015-10-02 14:53:11 +00:00
soundController: null
2015-10-29 19:50:38 +00:00
};
2015-10-01 10:22:23 +00:00
},
2015-10-03 22:39:51 +00:00
updateTitle() {
let gather = this.props.gather;
2015-10-29 19:50:38 +00:00
if (gather && this.state.updateTitle) {
2015-10-03 22:39:51 +00:00
document.title = `NSL Gathers (${gather.gatherers.length}/12)`;
2015-10-29 19:50:38 +00:00
return;
}
document.title = "NSL Gathers";
},
2016-01-02 21:32:26 +00:00
toggleEventsPanel(event) {
let newState = event.target.checked;
this.setState({ showEventsPanel: newState });
if (storageAvailable('localStorage')) {
localStorage.setItem("showEventsPanel", newState)
}
},
2015-10-29 19:50:38 +00:00
toggleUpdateTitle(event) {
let newState = event.target.checked;
2016-01-02 21:32:26 +00:00
this.setState({ updateTitle: newState });
2015-10-29 19:50:38 +00:00
if (storageAvailable('localStorage')) {
localStorage.setItem("updateTitle", newState)
2015-10-03 22:39:51 +00:00
}
2015-10-29 19:50:38 +00:00
this.updateTitle();
2015-10-03 22:39:51 +00:00
},
2015-10-02 17:52:04 +00:00
thisGatherer() {
let gather = this.props.gather;
let user = this.props.user;
if (gather && user && gather.gatherers.length) {
return gather.gatherers
.filter(gatherer => gatherer.id === user.id)
.pop() || null;
}
return null;
},
2015-10-01 10:22:23 +00:00
componentDidMount() {
let self = this;
2015-10-02 14:53:11 +00:00
let socket = this.props.socket;
2015-10-02 17:52:04 +00:00
let soundController = this.props.soundController;
2015-10-29 19:50:38 +00:00
this.updateTitle();
2015-12-24 20:16:46 +00:00
socket.on('stateChange', data => {
let state = data.state;
if (state.from === 'gathering'
&& state.to === 'election'
2015-10-02 17:52:04 +00:00
&& this.thisGatherer()) {
soundController.playGatherMusic();
}
if (state.from === 'election'
&& state.to === 'gathering') {
soundController.stop();
}
2015-10-02 17:52:04 +00:00
});
2015-10-01 10:22:23 +00:00
2015-12-29 16:26:11 +00:00
socket.on('event:append', data => {
let events = self.state.events;
events.unshift(data);
self.setState({
events: events.slice(0, 20)
});
});
2015-10-01 10:22:23 +00:00
socket.on('users:update',
2015-10-02 14:53:11 +00:00
data => self.setProps({
users: data.users,
user: data.currentUser
})
);
2015-10-01 10:22:23 +00:00
socket.on("message:append", data => {
self.setProps({
2015-10-02 14:53:11 +00:00
messages: self.props.messages.concat(data.messages)
2015-10-01 10:22:23 +00:00
.sort((a, b) => {
return new Date(a.createdAt) - new Date(b.createdAt);
})
});
});
socket.on("message:refresh", data => {
self.setProps({
2015-10-02 14:53:11 +00:00
messages: data.messages
});
});
socket.on("gather:refresh", (data) => {
self.setProps({
gather: data.gather,
maps: data.maps,
servers: data.servers,
previousGather: data.previousGather
});
2015-10-03 22:39:51 +00:00
this.updateTitle();
2015-10-02 14:53:11 +00:00
});
socket.on("gather:archive:refresh", data => {
self.setProps({
archive: data.archive,
maps: data.maps,
servers: data.servers
2015-10-01 10:22:23 +00:00
});
});
socket.emit("users:refresh");
socket.emit("message:refresh");
},
render() {
2016-01-02 21:32:26 +00:00
let eventsPanel;
if (this.state.showEventsPanel) {
eventsPanel = <Events events={this.state.events} />;
}
2015-10-01 10:22:23 +00:00
return <div id="wrapper">
<nav className="navbar navbar-default navbar-static-top"
role="navigation"
style={{marginBottom: "0"}}>
<div className="navbar-header">
2015-10-04 12:25:00 +00:00
<a className="navbar-brand" href="/">NSL Gathers <small><i>Alpha</i></small></a>
2015-10-01 10:22:23 +00:00
</div>
<ul className="nav navbar-top-links navbar-right" id="currentuser">
<CurrentUser user={this.props.user} />
</ul>
<ul className="nav navbar-top-links navbar-right" id="soundcontroller">
2015-10-02 14:53:11 +00:00
<SoundPanel soundController={this.props.soundController} />
2015-10-01 10:22:23 +00:00
</ul>
2015-10-20 16:36:16 +00:00
<TeamSpeakButton />
2015-10-01 10:22:23 +00:00
<ul className="nav navbar-top-links navbar-right">
<li className="dropdown">
<a className="dropdown-toggle" data-toggle="dropdown" href="#">
Info &nbsp;<i className="fa fa-caret-down"></i>
</a>
<ul className="dropdown-menu">
<li>
<a href="https://github.com/cblanc/sws_gathers" target="_blank">
2015-10-22 14:25:45 +00:00
<i className="fa fa-github">&nbsp;</i>&nbsp;Github
2015-10-01 10:22:23 +00:00
</a>
</li>
<li>
<a href="http://steamcommunity.com/id/nslgathers" target="_blank">
2015-10-22 14:25:45 +00:00
<i className="fa fa-external-link">&nbsp;</i>&nbsp;Steam Bot
2015-10-01 10:22:23 +00:00
</a>
</li>
<li>
<a href="http://www.ensl.org/articles/464" target="_blank">
2015-10-22 14:25:45 +00:00
<i className="fa fa-external-link">&nbsp;</i>&nbsp;Gather Rules
2015-10-01 10:22:23 +00:00
</a>
</li>
2016-01-01 14:48:28 +00:00
<li>
<a href="/messages" target="_blank">
<i className="fa fa-external-link">&nbsp;</i>&nbsp;Message Archive
</a>
</li>
2015-10-01 10:22:23 +00:00
</ul>
</li>
</ul>
</nav>
<AdminPanel />
2015-10-29 19:50:38 +00:00
<SettingsPanel
2016-01-02 21:32:26 +00:00
toggleEventsPanel={this.toggleEventsPanel}
showEventsPanel={this.state.showEventsPanel}
2015-10-29 19:50:38 +00:00
toggleUpdateTitle={this.toggleUpdateTitle}
updateTitle={this.state.updateTitle} />
2015-10-20 16:36:16 +00:00
<TeamSpeakModal />
2015-10-01 10:22:23 +00:00
<ProfileModal user={this.props.user} />
<div style={{minHeight: "750px"}}>
<div className="container-fluid">
<div className="row">
<div className="col-md-2 hidden-xs">
<ul className="nav" id="side-menu">
2015-11-15 16:26:32 +00:00
<UserMenu users={this.props.users} user={this.props.user} />
2015-10-01 10:22:23 +00:00
</ul>
</div>
<div className="col-md-4" id="chatroom">
<Chatroom
messages={this.props.messages}
user={this.props.user} />
</div>
<div className="col-md-6" id="gathers">
2015-10-02 14:53:11 +00:00
<Gather
gather={this.props.gather}
2015-10-02 17:52:04 +00:00
thisGatherer={this.thisGatherer()}
2015-10-02 14:53:11 +00:00
user={this.props.user}
soundController={this.props.soundController}
2015-10-02 14:53:11 +00:00
maps={this.props.maps}
servers={this.props.servers}
previousGather={this.props.previousGather}/>
2015-10-20 22:54:28 +00:00
<hr />
2015-10-02 14:53:11 +00:00
<ArchivedGathers archive={this.props.archive}
maps={this.props.maps}
servers={this.props.servers} />
2016-01-02 21:32:26 +00:00
<hr />
{eventsPanel}
2015-10-01 10:22:23 +00:00
</div>
</div>
</div>
</div>
</div>
}
});