From 53ee280accad1b9d15c9d804dd0c3ce95f2ee8c6 Mon Sep 17 00:00:00 2001 From: Mario Pesch Date: Fri, 6 Jan 2023 13:09:59 +0100 Subject: [PATCH] add board to share projects --- src/actions/arduinoActions.js | 150 ++++++++++++++++++ .../{boardAction.js => boardActions.js} | 0 src/actions/projectActions.js | 6 +- src/actions/types.js | 2 + src/components/CodeEditor/Sidebar.js | 27 ++-- src/components/Content.js | 11 +- src/components/DeviceSelection.js | 2 +- src/components/Home.js | 15 +- src/components/Settings/DeviceSelector.js | 2 +- src/components/Workspace/SaveProject.js | 8 +- src/reducers/arduinoReducer.js | 20 +++ src/reducers/index.js | 4 +- 12 files changed, 222 insertions(+), 25 deletions(-) create mode 100644 src/actions/arduinoActions.js rename src/actions/{boardAction.js => boardActions.js} (100%) create mode 100644 src/reducers/arduinoReducer.js diff --git a/src/actions/arduinoActions.js b/src/actions/arduinoActions.js new file mode 100644 index 0000000..c0936b8 --- /dev/null +++ b/src/actions/arduinoActions.js @@ -0,0 +1,150 @@ +import { GET_ARDUINO_EXAMPLES } from './types'; + +import axios from 'axios'; +import { returnErrors, returnSuccess } from './messageActions'; + + +export const getArduinoExamples = () => (dispatch) => { + const config = { + success: res => { + var examples = res.data.arduinoExamples; + console.log(examples); + dispatch({ + type: GET_ARDUINO_EXAMPLES, + payload: examples + }); + dispatch(returnSuccess(res.data.message, res.status)); + }, + error: err => { + if (err.response) { + dispatch(returnErrors(err.response.data.message, err.response.status, 'GET_PROJECTS_FAIL')); + } + } + }; + axios.get(`${process.env.REACT_APP_BLOCKLY_API}/arduino`, config) + .then(res => { + res.config.success(res); + }) + .catch(err => { + err.config.error(err); + }); +}; + +// export const updateProject = (type, id) => (dispatch, getState) => { +// var workspace = getState().workspace; +// var body = { +// xml: workspace.code.xml, +// title: workspace.name, +// board: getState().board.board, +// }; +// var project = getState().project; +// if (type === 'gallery') { +// body.description = project.description; +// } +// const config = { +// success: res => { +// var project = res.data[type]; +// var projects = getState().project.projects; +// var index = projects.findIndex(res => res._id === project._id); +// projects[index] = project; +// dispatch({ +// type: GET_PROJECTS, +// payload: projects +// }); +// if (type === 'project') { +// dispatch(returnSuccess(res.data.message, res.status, 'PROJECT_UPDATE_SUCCESS')); +// } else { +// dispatch(returnSuccess(res.data.message, res.status, 'GALLERY_UPDATE_SUCCESS')); +// } +// }, +// error: err => { +// if (err.response) { +// if (type === 'project') { +// dispatch(returnErrors(err.response.data.message, err.response.status, 'PROJECT_UPDATE_FAIL')); +// } else { +// dispatch(returnErrors(err.response.data.message, err.response.status, 'GALLERY_UPDATE_FAIL')); +// } +// } +// } +// }; +// axios.put(`${process.env.REACT_APP_BLOCKLY_API}/${type}/${id}`, body, config) +// .then(res => { +// res.config.success(res); +// }) +// .catch(err => { +// err.config.error(err); +// }); +// }; + +// export const deleteProject = (type, id) => (dispatch, getState) => { +// const config = { +// success: res => { +// var projects = getState().project.projects; +// var index = projects.findIndex(res => res._id === id); +// projects.splice(index, 1) +// dispatch({ +// type: GET_PROJECTS, +// payload: projects +// }); +// if (type === 'project') { +// dispatch(returnSuccess(res.data.message, res.status, 'PROJECT_DELETE_SUCCESS')); +// } else { +// dispatch(returnSuccess(res.data.message, res.status, 'GALLERY_DELETE_SUCCESS')); +// } +// }, +// error: err => { +// dispatch(returnErrors(err.response.data.message, err.response.status, 'PROJECT_DELETE_FAIL')); +// } +// }; +// axios.delete(`${process.env.REACT_APP_BLOCKLY_API}/${type}/${id}`, config) +// .then(res => { +// res.config.success(res); +// }) +// .catch(err => { +// if (err.response && err.response.status !== 401) { +// err.config.error(err); +// } +// }); +// }; + + +// export const shareProject = (title, type, id) => (dispatch, getState) => { +// var body = { +// title: title, +// board: getState().board.board +// }; +// if (type === 'project') { +// body.projectId = id; +// } else { +// body.xml = getState().workspace.code.xml; +// } +// axios.post(`${process.env.REACT_APP_BLOCKLY_API}/share`, body) +// .then(res => { +// var shareContent = res.data.content; +// if (body.projectId) { +// var projects = getState().project.projects; +// var index = projects.findIndex(res => res._id === id); +// projects[index].shared = shareContent.expiresAt; +// dispatch({ +// type: GET_PROJECTS, +// payload: projects +// }); +// } +// dispatch(returnSuccess(res.data.message, shareContent._id, 'SHARE_SUCCESS')); +// }) +// .catch(err => { +// if (err.response) { +// dispatch(returnErrors(err.response.data.message, err.response.status, 'SHARE_FAIL')); +// } +// }); +// }; + + +// export const resetProject = () => (dispatch) => { +// dispatch({ +// type: GET_PROJECTS, +// payload: [] +// }); +// dispatch(setType('')); +// dispatch(setDescription('')); +// }; diff --git a/src/actions/boardAction.js b/src/actions/boardActions.js similarity index 100% rename from src/actions/boardAction.js rename to src/actions/boardActions.js diff --git a/src/actions/projectActions.js b/src/actions/projectActions.js index 0cd8f0e..54c7789 100644 --- a/src/actions/projectActions.js +++ b/src/actions/projectActions.js @@ -90,7 +90,8 @@ export const updateProject = (type, id) => (dispatch, getState) => { var workspace = getState().workspace; var body = { xml: workspace.code.xml, - title: workspace.name + title: workspace.name, + board: getState().board.board, }; var project = getState().project; if (type === 'gallery') { @@ -165,7 +166,8 @@ export const deleteProject = (type, id) => (dispatch, getState) => { export const shareProject = (title, type, id) => (dispatch, getState) => { var body = { - title: title + title: title, + board: getState().board.board }; if (type === 'project') { body.projectId = id; diff --git a/src/actions/types.js b/src/actions/types.js index c67ad2a..62936e7 100644 --- a/src/actions/types.js +++ b/src/actions/types.js @@ -64,5 +64,7 @@ export const GET_PROJECTS = "GET_PROJECTS"; export const PROJECT_TYPE = "PROJECT_TYPE"; export const PROJECT_DESCRIPTION = "PROJECT_DESCRIPTION"; +export const GET_ARDUINO_EXAMPLES = "GET_ARDUINO_EXAMPLES"; + //board export const BOARD = "BOARD"; diff --git a/src/components/CodeEditor/Sidebar.js b/src/components/CodeEditor/Sidebar.js index 5e2b976..25a21c9 100644 --- a/src/components/CodeEditor/Sidebar.js +++ b/src/components/CodeEditor/Sidebar.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect } from "react"; import Blockly from "blockly"; import { useSelector } from "react-redux"; import Accordion from "@material-ui/core/Accordion"; @@ -10,17 +10,20 @@ import { useMonaco } from "@monaco-editor/react"; import { Button } from "@material-ui/core"; import SerialMonitor from "./SerialMonitor.js"; import axios from "axios"; +import { useDispatch } from "react-redux"; +import { getArduinoExamples } from "../../actions/arduinoActions"; +//import getArduinoExamples from "../../reducers/arduinoReducer" const Sidebar = () => { - //const [examples, setExamples] = React.useState([]); + + + const examples = useSelector((state) => state.arduino.examples); const user = useSelector((state) => state.auth.user); - // useEffect(() => { - // axios - // .get("https://coelho.opensensemap.org/items/blocklysamples") - // .then((res) => { - // setExamples(res.data.data); - // }); - // }, []); + const dispatch = useDispatch() + + useEffect(() => { + dispatch(getArduinoExamples()); + }, [dispatch]); const monaco = useMonaco(); const loadCode = (code) => { monaco.editor.getModels()[0].setValue(code); @@ -52,7 +55,7 @@ const Sidebar = () => { ) : null} - {/* + { key={i} onClick={() => loadCode(object.code)} > - {object.name} + {object.title} ); })} - */} + {user ? ( ({ link: { diff --git a/src/components/Home.js b/src/components/Home.js index 13a4661..357d306 100644 --- a/src/components/Home.js +++ b/src/components/Home.js @@ -2,10 +2,10 @@ import React, { Component } from "react"; import PropTypes from "prop-types"; import { connect } from "react-redux"; import { clearStats, workspaceName } from "../actions/workspaceActions"; - +import { setBoard } from "../actions/boardActions"; import * as Blockly from "blockly/core"; import { createNameId } from "mnemonic-id"; - +import * as BoardHelper from "./Blockly/helpers/board"; import WorkspaceStats from "./Workspace/WorkspaceStats"; import WorkspaceFunc from "./Workspace/WorkspaceFunc"; import BlocklyWindow from "./Blockly/BlocklyWindow"; @@ -66,6 +66,10 @@ class Home extends Component { this.setState({ stats: window.localStorage.getItem("stats") }); if (!this.props.project) { this.props.workspaceName(createNameId()); + } else { + console.log(this.props.project); + this.props.setBoard(this.props.project.board); + BoardHelper.setBoard(this.props.project.board); } if (this.props.message && this.props.message.id === "GET_SHARE_FAIL") { this.setState({ @@ -187,7 +191,8 @@ class Home extends Component { ) : null} - + {!this.props.board ? : null} + {/* */} {/* */} {this.props.platform ? ( ({ message: state.message, statistics: state.general.statistics, platform: state.general.platform, + board: state.board.board }); -export default connect(mapStateToProps, { clearStats, workspaceName })( +export default connect(mapStateToProps, { clearStats, workspaceName, setBoard })( withStyles(styles, { withTheme: true })(Home) ); diff --git a/src/components/Settings/DeviceSelector.js b/src/components/Settings/DeviceSelector.js index b506527..560919f 100644 --- a/src/components/Settings/DeviceSelector.js +++ b/src/components/Settings/DeviceSelector.js @@ -1,7 +1,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; -import { setBoard } from '../../actions/boardAction'; +import { setBoard } from '../../actions/boardActions'; import * as Blockly from 'blockly/core'; diff --git a/src/components/Workspace/SaveProject.js b/src/components/Workspace/SaveProject.js index 3f01ff8..5fe8759 100644 --- a/src/components/Workspace/SaveProject.js +++ b/src/components/Workspace/SaveProject.js @@ -91,8 +91,11 @@ class SaveProject extends Component { saveProject = () => { var body = { xml: this.props.xml, - title: this.props.name + title: this.props.name, + board: this.props.board, + type: "blockly" }; + console.log(body); if (this.state.projectType === 'gallery') { body.description = this.state.description; } @@ -202,7 +205,8 @@ const mapStateToProps = state => ({ description: state.project.description, xml: state.workspace.code.xml, message: state.message, - user: state.auth.user + user: state.auth.user, + board: state.board.board, }); export default connect(mapStateToProps, { updateProject, setDescription })(withStyles(styles, { withTheme: true })(withRouter(SaveProject))); diff --git a/src/reducers/arduinoReducer.js b/src/reducers/arduinoReducer.js new file mode 100644 index 0000000..3a4e69a --- /dev/null +++ b/src/reducers/arduinoReducer.js @@ -0,0 +1,20 @@ +import { GET_ARDUINO_EXAMPLES } from '../actions/types'; + +const initialState = { + examples: [], + type: '', + description: '', + progress: false +}; + +export default function foo(state = initialState, action) { + switch (action.type) { + case GET_ARDUINO_EXAMPLES: + return { + ...state, + examples: action.payload + }; + default: + return state; + } +} diff --git a/src/reducers/index.js b/src/reducers/index.js index 241e4df..dcb9299 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -8,6 +8,7 @@ import messageReducer from './messageReducer'; import authReducer from './authReducer'; import boardReducer from './boardReducer'; import sensorwikiReducer from './sensorwikiReducer'; +import arduinoReducer from './arduinoReducer'; export default combineReducers({ auth: authReducer, @@ -18,5 +19,6 @@ export default combineReducers({ project: projectReducer, general: generalReducer, message: messageReducer, - sensorwiki: sensorwikiReducer + sensorwiki: sensorwikiReducer, + arduino: arduinoReducer });