delete tutorial button at tutorial builder

This commit is contained in:
Delucse 2020-12-08 15:33:03 +01:00
parent e6813ba2d3
commit 3cf7f10d34
2 changed files with 85 additions and 20 deletions

View File

@ -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) => { export const resetTutorial = () => (dispatch) => {
dispatch({ dispatch({
type: GET_TUTORIALS, type: GET_TUTORIALS,

View File

@ -2,7 +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 { checkError, readJSON, jsonString, progress, tutorialId, resetTutorial as resetTutorialBuilder} from '../../../actions/tutorialBuilderActions'; 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 { clearMessages } from '../../../actions/messageActions';
import axios from 'axios'; import axios from 'axios';
@ -38,9 +38,18 @@ const styles = (theme) => ({
}, },
errorColor: { errorColor: {
color: theme.palette.error.dark 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 { class Builder extends Component {
constructor(props) { constructor(props) {
@ -63,9 +72,18 @@ class Builder extends Component {
} }
componentDidUpdate(props, state) { componentDidUpdate(props, state) {
if(this.props.message.id === 'GET_TUTORIALS_FAIL'){ if(props.message !== this.props.message){
alert(this.props.message.msg); if(this.props.message.id === 'GET_TUTORIALS_FAIL'){
this.props.clearMessages(); // 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) => { onChangeId = (value) => {
this.props.tutorialId(value); this.props.tutorialId(value);
this.props.progress(true); if(this.state.tutorial === 'change'){
var tutorial = this.props.tutorials.filter(tutorial => tutorial._id === value)[0]; this.props.progress(true);
this.props.readJSON(tutorial); var tutorial = this.props.tutorials.filter(tutorial => tutorial._id === value)[0];
this.setState({ snackbar: true, key: Date.now(), message: `Das ausgewählte Tutorial "${tutorial.title}" wurde erfolgreich übernommen.`, type: 'success' }); this.props.readJSON(tutorial);
this.setState({ snackbar: true, key: Date.now(), message: `Das ausgewählte Tutorial "${tutorial.title}" wurde erfolgreich übernommen.`, type: 'success' });
}
} }
resetFull = () => { resetFull = () => {
@ -222,6 +242,7 @@ class Builder extends Component {
} }
render() { render() {
var filteredTutorials = this.props.tutorials.filter(tutorial => tutorial.creator === this.props.user.email);
return ( return (
<div> <div>
<Breadcrumbs content={[{ link: '/tutorial', title: 'Tutorial' }, { link: '/tutorial/builder', title: 'Builder' }]} /> <Breadcrumbs content={[{ link: '/tutorial', title: 'Tutorial' }, { link: '/tutorial/builder', title: 'Builder' }]} />
@ -235,13 +256,24 @@ class Builder extends Component {
label="neues Tutorial erstellen" label="neues Tutorial erstellen"
labelPlacement="end" labelPlacement="end"
/> />
<FormControlLabel style={{color: 'black'}} {filteredTutorials.length > 0 ?
disabled={this.props.index === 0} <div>
value="change" <FormControlLabel style={{color: 'black'}}
control={<Radio color="primary" />} disabled={this.props.index === 0}
label="bestehendes Tutorial ändern" value="change"
labelPlacement="end" control={<Radio color="primary" />}
/> label="bestehendes Tutorial ändern"
labelPlacement="end"
/>
<FormControlLabel style={{color: 'black'}}
disabled={this.props.index === 0}
value="delete"
control={<Radio color="primary" />}
label="bestehendes Tutorial löschen"
labelPlacement="end"
/>
</div>
: null}
</RadioGroup> </RadioGroup>
<Divider variant='fullWidth' style={{ margin: '10px 0 15px 0' }} /> <Divider variant='fullWidth' style={{ margin: '10px 0 15px 0' }} />
@ -266,11 +298,11 @@ class Builder extends Component {
<Select <Select
color='primary' color='primary'
labelId="select-outlined-label" labelId="select-outlined-label"
defaultValue={this.props.id} value={this.props.id}
onChange={(e) => this.onChangeId(e.target.value)} onChange={(e) => this.onChangeId(e.target.value)}
label="Tutorial" label="Tutorial"
> >
{this.props.tutorials.map(tutorial => {filteredTutorials.map(tutorial =>
<MenuItem value={tutorial._id}>{tutorial.title}</MenuItem> <MenuItem value={tutorial._id}>{tutorial.title}</MenuItem>
)} )}
</Select> </Select>
@ -311,6 +343,14 @@ class Builder extends Component {
</div> </div>
: null} : null}
{this.state.tutorial === 'delete' && this.props.id !== '' ?
<Button
className={this.props.classes.errorButton}
variant='contained'
color='primary'
onClick={() => this.props.deleteTutorial()}>Tutorial löschen</Button>
: null}
<Dialog <Dialog
open={this.state.open} open={this.state.open}
maxWidth={this.state.string ? 'md' : 'sm'} maxWidth={this.state.string ? 'md' : 'sm'}
@ -355,6 +395,7 @@ Builder.propTypes = {
readJSON: PropTypes.func.isRequired, readJSON: PropTypes.func.isRequired,
jsonString: PropTypes.func.isRequired, jsonString: PropTypes.func.isRequired,
progress: PropTypes.func.isRequired, progress: PropTypes.func.isRequired,
deleteTutorial: PropTypes.func.isRequired,
resetTutorialBuilder: PropTypes.func.isRequired, resetTutorialBuilder: PropTypes.func.isRequired,
title: PropTypes.string.isRequired, title: PropTypes.string.isRequired,
steps: PropTypes.array.isRequired, steps: PropTypes.array.isRequired,
@ -364,7 +405,8 @@ Builder.propTypes = {
id: PropTypes.string.isRequired, id: PropTypes.string.isRequired,
isProgress: PropTypes.bool.isRequired, isProgress: PropTypes.bool.isRequired,
tutorials: PropTypes.array.isRequired, tutorials: PropTypes.array.isRequired,
message: PropTypes.object.isRequired message: PropTypes.object.isRequired,
user: PropTypes.object.isRequired
}; };
const mapStateToProps = state => ({ const mapStateToProps = state => ({
@ -376,7 +418,8 @@ const mapStateToProps = state => ({
json: state.builder.json, json: state.builder.json,
isProgress: state.builder.progress, isProgress: state.builder.progress,
tutorials: state.tutorial.tutorials, tutorials: state.tutorial.tutorials,
message: state.message message: state.message,
user: state.auth.user,
}); });
export default connect(mapStateToProps, { checkError, readJSON, jsonString, progress, tutorialId, resetTutorialBuilder, getTutorials, resetTutorial, clearMessages })(withStyles(styles, { withTheme: true })(withRouter(Builder))); export default connect(mapStateToProps, { checkError, readJSON, jsonString, progress, tutorialId, resetTutorialBuilder, getTutorials, resetTutorial, clearMessages, deleteTutorial })(withStyles(styles, { withTheme: true })(withRouter(Builder)));