Compare commits

..
2 Commits
Author SHA1 Message Date
ceda_ei fcc81cf7e5 Add WebSocket provider and handler for changePlugin 2020-12-21 15:16:05 +05:30
ceda_ei 459d334732 Add sample config and update gitignore 2020-12-21 15:04:30 +05:30
5 changed files with 53 additions and 8 deletions
+1
View File
@@ -23,3 +23,4 @@ yarn-debug.log*
yarn-error.log*
.eslintcache
src/config.js
+6 -3
View File
@@ -1,12 +1,15 @@
import React from "react";
import { Grommet } from "grommet";
import Core from "./Core";
import WebSocketProvider from "./WebSocket";
function App() {
return (
<Grommet plain>
<Core />
</Grommet>
<WebSocketProvider>
<Grommet plain>
<Core />
</Grommet>
</WebSocketProvider>
);
}
+1 -5
View File
@@ -7,7 +7,6 @@ import * as plugins from "./plugins";
function Core() {
const coreState = useSelector(selectCore);
const dispatch = useDispatch();
console.log(plugins.default);
const plugin = plugins.default[coreState.plugin];
const props = {
data: coreState.data,
@@ -27,10 +26,7 @@ function Core() {
}
</>
}
{plugin ?
React.cloneElement(plugin(), props)
:null
}
{plugin ? plugin(props) :null}
</>;
}
+42
View File
@@ -0,0 +1,42 @@
import React, { createContext } from "react";
import PropTypes from "prop-types";
import io from "socket.io-client";
import { WS_BASE } from "./config";
import { useDispatch } from "react-redux";
import { setData, setPlugin } from "./coreSlice";
const WebSocketContext = createContext(null);
export { WebSocketContext };
function WebSocketProvider({ children }) {
let socket;
const dispatch = useDispatch();
if (!socket) {
socket = io.connect(WS_BASE);
socket.on("switchPlugin", (payload) => {
dispatch(setPlugin(payload.plugin));
dispatch(setData(payload.data));
if (payload.time) {
setTimeout(() => {
dispatch(setData({}));
dispatch(setPlugin(false));
}, payload.time);
}
});
}
return (
<WebSocketContext.Provider value={socket}>
{children}
</WebSocketContext.Provider>
);
}
WebSocketProvider.propTypes = {
children: PropTypes.node
};
export default WebSocketProvider;
+3
View File
@@ -0,0 +1,3 @@
const WS_BASE = "http://localhost:5050/";
export { WS_BASE };