update project

This commit is contained in:
Delucse 2020-12-02 15:21:28 +01:00
parent a30747c608
commit c5fd5c6239
2 changed files with 45 additions and 7 deletions

View File

@ -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) => { export const resetProject = () => (dispatch) => {
dispatch({ dispatch({
type: GET_PROJECTS, type: GET_PROJECTS,
payload: [] payload: []
}); });
dispatch(setType(''));
}; };

View File

@ -2,6 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { clearStats, onChangeCode, workspaceName } from '../actions/workspaceActions'; import { clearStats, onChangeCode, workspaceName } from '../actions/workspaceActions';
import { updateProject } from '../actions/projectActions';
import * as Blockly from 'blockly/core'; import * as Blockly from 'blockly/core';
@ -87,6 +88,14 @@ class WorkspaceFunc extends Component {
if (props.name !== this.props.name) { if (props.name !== this.props.name) {
this.setState({ 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 = () => { toggleDialog = () => {
@ -265,10 +274,10 @@ class WorkspaceFunc extends Component {
</Tooltip> </Tooltip>
: null} : null}
{this.props.assessment ? <SolutionCheck /> : <Compile iconButton />} {this.props.assessment ? <SolutionCheck /> : <Compile iconButton />}
<Tooltip title='Projekt speichern' arrow style={{ marginRight: '5px' }}> <Tooltip title={this.props.projectType === 'project'? 'Projekt aktualisieren':'Projekt speichern'} arrow style={{ marginRight: '5px' }}>
<IconButton <IconButton
className={this.props.classes.button} className={this.props.classes.button}
onClick={() => this.saveProject()} onClick={this.props.projectType === 'project' ? () => this.props.updateProject() : () => this.saveProject()}
> >
<FontAwesomeIcon icon={faSave} size="xs" /> <FontAwesomeIcon icon={faSave} size="xs" />
</IconButton> </IconButton>
@ -376,18 +385,23 @@ class WorkspaceFunc extends Component {
} }
WorkspaceFunc.propTypes = { WorkspaceFunc.propTypes = {
clearStats: PropTypes.func.isRequired,
onChangeCode: PropTypes.func.isRequired,
workspaceName: PropTypes.func.isRequired,
updateProject: PropTypes.func.isRequired,
arduino: PropTypes.string.isRequired, arduino: PropTypes.string.isRequired,
xml: PropTypes.string.isRequired, xml: PropTypes.string.isRequired,
name: PropTypes.string, name: PropTypes.string,
clearStats: PropTypes.func.isRequired, projectType: PropTypes.string.isRequired,
onChangeCode: PropTypes.func.isRequired, message: PropTypes.object.isRequired
workspaceName: PropTypes.func.isRequired
}; };
const mapStateToProps = state => ({ const mapStateToProps = state => ({
arduino: state.workspace.code.arduino, arduino: state.workspace.code.arduino,
xml: state.workspace.code.xml, 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))));