diff --git a/src/actions/projectActions.js b/src/actions/projectActions.js index 31d3af3..eb7bdd1 100644 --- a/src/actions/projectActions.js +++ b/src/actions/projectActions.js @@ -60,9 +60,33 @@ export const getProjects = (type) => (dispatch) => { }); }; +export const updateProject = () => (dispatch, getState) => { + var workspace = getState().workspace; + var body = { + xml: workspace.code.xml, + title: workspace.name + } + var id = getState().project.projects[0]._id; + axios.put(`${process.env.REACT_APP_BLOCKLY_API}/project/${id}`, body) + .then(res => { + var project = res.data.project; + dispatch({ + type: GET_PROJECT, + payload: project + }); + dispatch(returnSuccess(res.data.message, res.status, 'PROJECT_UPDATE_SUCCESS')); + }) + .catch(err => { + if(err.response){ + dispatch(returnErrors(err.response.data.message, err.response.status, 'PROJECT_UPDATE_FAIL')); + } + }); +} + export const resetProject = () => (dispatch) => { dispatch({ type: GET_PROJECTS, payload: [] }); + dispatch(setType('')); }; diff --git a/src/components/WorkspaceFunc.js b/src/components/WorkspaceFunc.js index 86b3a55..bc0879e 100644 --- a/src/components/WorkspaceFunc.js +++ b/src/components/WorkspaceFunc.js @@ -2,6 +2,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { clearStats, onChangeCode, workspaceName } from '../actions/workspaceActions'; +import { updateProject } from '../actions/projectActions'; import * as Blockly from 'blockly/core'; @@ -87,6 +88,14 @@ class WorkspaceFunc extends Component { if (props.name !== this.props.name) { this.setState({ name: this.props.name }); } + if(this.props.message !== props.message){ + if(this.props.message.id === 'PROJECT_UPDATE_SUCCESS'){ + this.setState({ snackbar: true, key: Date.now(), message: `Das Projekt wurde erfolgreich aktualisiert.`, type: 'success' }); + } + else if (this.props.message.id === 'PROJECT_UPDATE_FAIL'){ + this.setState({ snackbar: true, key: Date.now(), message: `Fehler beim Aktualisieren des Projektes. Versuche es noch einmal.`, type: 'error' }); + } + } } toggleDialog = () => { @@ -265,10 +274,10 @@ class WorkspaceFunc extends Component { : null} {this.props.assessment ? : } - + this.saveProject()} + onClick={this.props.projectType === 'project' ? () => this.props.updateProject() : () => this.saveProject()} > @@ -376,18 +385,23 @@ class WorkspaceFunc extends Component { } WorkspaceFunc.propTypes = { + clearStats: PropTypes.func.isRequired, + onChangeCode: PropTypes.func.isRequired, + workspaceName: PropTypes.func.isRequired, + updateProject: PropTypes.func.isRequired, arduino: PropTypes.string.isRequired, xml: PropTypes.string.isRequired, name: PropTypes.string, - clearStats: PropTypes.func.isRequired, - onChangeCode: PropTypes.func.isRequired, - workspaceName: PropTypes.func.isRequired + projectType: PropTypes.string.isRequired, + message: PropTypes.object.isRequired }; const mapStateToProps = state => ({ arduino: state.workspace.code.arduino, xml: state.workspace.code.xml, - name: state.workspace.name + name: state.workspace.name, + projectType: state.project.type, + message: state.message }); -export default connect(mapStateToProps, { clearStats, onChangeCode, workspaceName })(withStyles(styles, { withTheme: true })(withWidth()(withRouter(WorkspaceFunc)))); +export default connect(mapStateToProps, { clearStats, onChangeCode, workspaceName, updateProject })(withStyles(styles, { withTheme: true })(withWidth()(withRouter(WorkspaceFunc))));