|
@@ -1,60 +1,50 @@
|
|
-import React from "react";
|
|
|
|
-import { Layout, Loading } from "./components";
|
|
|
|
-import { withRouter } from "react-router-dom";
|
|
|
|
-import socket from "./socket";
|
|
|
|
|
|
+import React from 'react'
|
|
|
|
+import { Layout, Loading, Main } from './components'
|
|
|
|
+import { withRouter } from 'react-router-dom'
|
|
|
|
+import socket from './socket'
|
|
|
|
|
|
-const initialState = { loading: true };
|
|
|
|
|
|
+const initialState = { loading: true, blocks: [], timestamp: 0 }
|
|
|
|
|
|
class App extends React.Component {
|
|
class App extends React.Component {
|
|
initializeSocket() {
|
|
initializeSocket() {
|
|
- socket.on("connect", () => {
|
|
|
|
- if (!socket.id) console.log("no websocket connection");
|
|
|
|
- else console.log("my socketId:", socket.id);
|
|
|
|
- });
|
|
|
|
- socket.on("welcome", message => {
|
|
|
|
- this.setState({ loading: false, message });
|
|
|
|
- });
|
|
|
|
|
|
+ socket.on('connect', () => {
|
|
|
|
+ if (!socket.id) console.log('no websocket connection')
|
|
|
|
+ else console.log('my socketId:', socket.id)
|
|
|
|
+ })
|
|
|
|
+ socket.on('block', (block) => {
|
|
|
|
+ const blocks = this.state.blocks
|
|
|
|
+ .filter((b) => b.id !== block.id)
|
|
|
|
+ .concat(block)
|
|
|
|
+ .sort((a, b) => b.id - a.id)
|
|
|
|
+ this.setState({ loading: false, blocks, timestamp: block.timestamp })
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
|
|
- async fetchData() {
|
|
|
|
- // initial axios requests go here
|
|
|
|
- this.setState({ loading: false });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /** RENDER FUNCTIONS **/
|
|
|
|
- renderLoading() {
|
|
|
|
- return <Loading />;
|
|
|
|
- }
|
|
|
|
renderError() {
|
|
renderError() {
|
|
- if (this.state.showModal === "Error") return;
|
|
|
|
- this.setShowModal("Error");
|
|
|
|
|
|
+ if (this.state.showModal === 'Error') return
|
|
|
|
+ this.setShowModal('Error')
|
|
}
|
|
}
|
|
renderApp() {
|
|
renderApp() {
|
|
- const message = this.state.message || "Fresh and clean.";
|
|
|
|
- return (
|
|
|
|
- <div className="w-100 h-100 d-flex flex-grow-1 align-items-center justify-content-center">
|
|
|
|
- <h1>{message}</h1>
|
|
|
|
- </div>
|
|
|
|
- );
|
|
|
|
|
|
+ return <Main {...this.state} />
|
|
}
|
|
}
|
|
render() {
|
|
render() {
|
|
- if (this.state.loading) return this.renderLoading();
|
|
|
|
- if (this.state.error) this.renderError();
|
|
|
|
- if (this.state.component === "layout") return <Layout {...this.state} />;
|
|
|
|
- return this.renderApp();
|
|
|
|
|
|
+ if (this.state.loading) return <Loading />
|
|
|
|
+ if (this.state.error) this.renderError()
|
|
|
|
+ if (this.state.component === 'layout') return <Layout {...this.state} />
|
|
|
|
+ return this.renderApp()
|
|
}
|
|
}
|
|
|
|
|
|
componentDidMount() {
|
|
componentDidMount() {
|
|
- this.initializeSocket();
|
|
|
|
- //this.fetchData();
|
|
|
|
|
|
+ this.initializeSocket()
|
|
|
|
+ setInterval(() => this.setState({}), 100)
|
|
}
|
|
}
|
|
componentWillUnmount() {
|
|
componentWillUnmount() {
|
|
- console.log("unmounting...");
|
|
|
|
|
|
+ console.log('unmounting...')
|
|
}
|
|
}
|
|
constructor() {
|
|
constructor() {
|
|
- super();
|
|
|
|
- this.state = initialState;
|
|
|
|
|
|
+ super()
|
|
|
|
+ this.state = initialState
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-export default withRouter(App);
|
|
|
|
|
|
+export default withRouter(App)
|