From 3cf7f10d34d44a50d206b70d7d8e37f15eb34624 Mon Sep 17 00:00:00 2001 From: Delucse <46593742+Delucse@users.noreply.github.com> Date: Tue, 8 Dec 2020 15:33:03 +0100 Subject: [PATCH] delete tutorial button at tutorial builder --- src/actions/tutorialActions.js | 22 ++++++ src/components/Tutorial/Builder/Builder.js | 83 ++++++++++++++++------ 2 files changed, 85 insertions(+), 20 deletions(-) diff --git a/src/actions/tutorialActions.js b/src/actions/tutorialActions.js index ec0e67b..bffabfa 100644 --- a/src/actions/tutorialActions.js +++ b/src/actions/tutorialActions.js @@ -56,6 +56,28 @@ export const getTutorials = () => (dispatch, getState) => { }); }; +export const deleteTutorial = (id) => (dispatch, getState) => { + var tutorial = getState().tutorial; + var id = getState().builder.id; + axios.delete(`${process.env.REACT_APP_BLOCKLY_API}/tutorial/${id}`) + .then(res => { + var tutorials = tutorial.tutorials; + var index = tutorials.findIndex(res => res._id === id); + tutorials.splice(index, 1) + dispatch({ + type: GET_TUTORIALS, + payload: tutorials + }); + dispatch(returnSuccess(res.data.message, res.status, 'TUTORIAL_DELETE_SUCCESS')); + }) + .catch(err => { + if(err.response){ + dispatch(returnErrors(err.response.data.message, err.response.status, 'TUTORIAL_DELETE_FAIL')); + } + }); +}; + + export const resetTutorial = () => (dispatch) => { dispatch({ type: GET_TUTORIALS, diff --git a/src/components/Tutorial/Builder/Builder.js b/src/components/Tutorial/Builder/Builder.js index 753edd0..9656d00 100644 --- a/src/components/Tutorial/Builder/Builder.js +++ b/src/components/Tutorial/Builder/Builder.js @@ -2,7 +2,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { checkError, readJSON, jsonString, progress, tutorialId, resetTutorial as resetTutorialBuilder} from '../../../actions/tutorialBuilderActions'; -import { getTutorials, resetTutorial} from '../../../actions/tutorialActions'; +import { getTutorials, resetTutorial, deleteTutorial } from '../../../actions/tutorialActions'; import { clearMessages } from '../../../actions/messageActions'; import axios from 'axios'; @@ -38,9 +38,18 @@ const styles = (theme) => ({ }, errorColor: { color: theme.palette.error.dark + }, + errorButton: { + marginTop: '5px', + height: '40px', + backgroundColor: theme.palette.error.dark, + '&:hover':{ + backgroundColor: theme.palette.error.dark + } } }); + class Builder extends Component { constructor(props) { @@ -63,9 +72,18 @@ class Builder extends Component { } componentDidUpdate(props, state) { - if(this.props.message.id === 'GET_TUTORIALS_FAIL'){ - alert(this.props.message.msg); - this.props.clearMessages(); + if(props.message !== this.props.message){ + if(this.props.message.id === 'GET_TUTORIALS_FAIL'){ + // alert(this.props.message.msg); + this.props.clearMessages(); + } + else if(this.props.message.id === 'TUTORIAL_DELETE_SUCCESS'){ + this.onChange('new'); + this.setState({ snackbar: true, key: Date.now(), message: `Das Tutorial wurde erfolgreich gelöscht.`, type: 'success' }); + } + else if(this.props.message.id === 'TUTORIAL_DELETE_FAIL'){ + this.setState({ snackbar: true, key: Date.now(), message: `Fehler beim Löschen des Tutorials. Versuche es noch einmal.`, type: 'error' }); + } } } @@ -130,10 +148,12 @@ class Builder extends Component { onChangeId = (value) => { this.props.tutorialId(value); - this.props.progress(true); - var tutorial = this.props.tutorials.filter(tutorial => tutorial._id === value)[0]; - this.props.readJSON(tutorial); - this.setState({ snackbar: true, key: Date.now(), message: `Das ausgewählte Tutorial "${tutorial.title}" wurde erfolgreich übernommen.`, type: 'success' }); + if(this.state.tutorial === 'change'){ + this.props.progress(true); + var tutorial = this.props.tutorials.filter(tutorial => tutorial._id === value)[0]; + this.props.readJSON(tutorial); + this.setState({ snackbar: true, key: Date.now(), message: `Das ausgewählte Tutorial "${tutorial.title}" wurde erfolgreich übernommen.`, type: 'success' }); + } } resetFull = () => { @@ -222,6 +242,7 @@ class Builder extends Component { } render() { + var filteredTutorials = this.props.tutorials.filter(tutorial => tutorial.creator === this.props.user.email); return (