193 lines
5.8 KiB
React
Raw Normal View History

2016-04-19 21:45:49 -04:00
import React from 'react';
2016-04-29 23:59:13 -04:00
import {browserHistory} from 'react-router';
2016-04-19 21:45:49 -04:00
import Header from './components/Header.jsx';
import Sidebar from './components/Sidebar.jsx';
import Footer from './components/Footer.jsx';
import HiddenSidebar from './components/HiddenSidebar.jsx';
import Socket from '../socket.js';
2016-04-19 21:45:49 -04:00
class App extends React.Component {
constructor(props) {
super(props);
this.checkLogin = this.checkLogin.bind(this);
2016-05-09 19:33:31 -04:00
this.flashMessage = this.flashMessage.bind(this);
this.facServStatus = this.facServStatus.bind(this);
this.getSaves = this.getSaves.bind(this);
this.getStatus = this.getStatus.bind(this);
this.connectWebSocket = this.connectWebSocket.bind(this);
2018-02-11 21:03:54 +01:00
this.getFactorioVersion = this.getFactorioVersion.bind(this);
this.state = {
serverRunning: "stopped",
serverStatus: {},
2018-02-11 21:03:54 +01:00
factorioVersion: {},
saves: [],
loggedIn: false,
username: "",
2016-05-09 19:33:31 -04:00
messages: [],
showMessage: false,
2016-04-29 23:59:13 -04:00
}
}
componentDidMount() {
this.checkLogin();
2016-05-09 19:33:31 -04:00
// Wait 1 second before redirecting to login page
setTimeout(() => {
if (!this.state.loggedIn) {
browserHistory.push("/login");
}
}, 1000);
this.connectWebSocket();
2018-02-11 21:03:54 +01:00
this.getFactorioVersion(); //Init serverStatus, so i know, which factorio-version is installed
}
2017-12-22 18:36:26 -05:00
connectWebSocket() {
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
let ws = new WebSocket(ws_scheme + "://" + window.location.host + "/ws");
let socket = this.socket = new Socket(ws);
}
2016-05-09 19:33:31 -04:00
flashMessage(message) {
var m = this.state.messages;
m.push(message);
this.setState({messages: m, showMessage: true});
}
checkLogin() {
$.ajax({
url: "/api/user/status",
dataType: "json",
success: (data) => {
if (data.success === true) {
this.setState({
loggedIn: true,
username: data.data.Username
});
}
}
})
}
facServStatus() {
$.ajax({
url: "/api/server/status",
dataType: "json",
success: (data) => {
2018-02-11 21:03:54 +01:00
this.setState({
serverRunning: data.data.status
})
}
})
}
getSaves() {
$.ajax({
url: "/api/saves/list",
dataType: "json",
success: (data) => {
if (data.success === true) {
this.setState({saves: data.data})
} else {
this.setState({saves: []})
}
},
error: (xhr, status, err) => {
2016-05-09 19:33:31 -04:00
console.log('api/saves/list', status, err.toString());
}
})
if (!this.state.saves) {
this.setState({saves:[]})
}
2016-04-19 21:45:49 -04:00
}
getStatus() {
$.ajax({
url: "/api/server/status",
dataType: "json",
success: (data) => {
2018-02-11 21:03:54 +01:00
this.setState({
serverStatus: data.data
})
},
error: (xhr, status, err) => {
console.log('api/server/status', status, err.toString());
}
})
}
getFactorioVersion() {
$.ajax({
2018-02-11 21:03:54 +01:00
url: "/api/server/facVersion",
// dataType: "json",
2018-02-11 21:03:54 +01:00
success: (data) => {
this.setState({
factorioVersion: data.data.base_mod_version
});
},
error: (xhr, status, err) => {
console.log('api/server/status', status, err.toString());
}
})
}
2016-04-19 21:45:49 -04:00
render() {
2017-12-22 18:36:26 -05:00
// render main application,
// if logged in show application
2016-05-09 19:33:31 -04:00
// if not logged in show Not logged in message
var resp;
if (this.state.loggedIn) {
2017-12-22 18:36:26 -05:00
var resp =
<div>
2017-12-22 18:36:26 -05:00
<Header
username={this.state.username}
loggedIn={this.state.loggedIn}
2016-05-09 19:33:31 -04:00
messages={this.state.messages}
/>
2016-04-19 21:45:49 -04:00
2017-12-22 18:36:26 -05:00
<Sidebar
serverStatus={this.facServStatus}
serverRunning={this.state.serverRunning}
/>
2017-12-22 18:36:26 -05:00
2016-05-09 19:33:31 -04:00
// Render react-router components and pass in props
{React.cloneElement(
this.props.children,
{
message: "",
messages: this.state.messages,
flashMessage: this.flashMessage,
facServStatus: this.facServStatus,
serverStatus: this.state.serverStatus,
factorioVersion: this.state.factorioVersion,
getStatus: this.getStatus,
saves: this.state.saves,
getSaves: this.getSaves,
username: this.state.username,
socket: this.socket
}
)}
2016-04-19 21:45:49 -04:00
<Footer />
2016-04-19 21:45:49 -04:00
2017-12-22 18:36:26 -05:00
<HiddenSidebar
serverStatus={this.state.serverStatus}
username={this.state.username}
loggedIn={this.state.loggedIn}
checkLogin={this.checkLogin}
/>
</div>
} else {
var resp = <div><p>Not Logged in</p></div>;
}
2016-04-19 21:45:49 -04:00
return(
<div className="wrapper">
{resp}
2016-04-19 21:45:49 -04:00
</div>
)
}
}
export default App