import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { changeContent, deleteProperty, setError, deleteError } from '../../../actions/tutorialBuilderActions'; import Textfield from './Textfield'; import { withStyles } from '@material-ui/core/styles'; import Switch from '@material-ui/core/Switch'; import FormControlLabel from '@material-ui/core/FormControlLabel'; import FormHelperText from '@material-ui/core/FormHelperText'; import Radio from '@material-ui/core/Radio'; import RadioGroup from '@material-ui/core/RadioGroup'; import Button from '@material-ui/core/Button'; const styles = (theme) => ({ errorColor: { color: theme.palette.error.dark }, errorBorder: { border: `1px solid ${theme.palette.error.dark}` }, errorButton: { marginTop: '5px', height: '40px', backgroundColor: theme.palette.error.dark, '&:hover':{ backgroundColor: theme.palette.error.dark } } }); class Media extends Component { constructor(props){ super(props); this.state={ checked: props.value ? true : false, error: false, radioValue: !props.picture && !props.youtube ? 'picture' : props.picture ? 'picture' : 'youtube' }; } componentDidUpdate(props){ if(props.value !== this.props.value){ this.setState({ checked: this.props.value ? true : false }); } } onChangeSwitch = (value) => { var oldValue = this.state.checked; this.setState({checked: value}); if(oldValue !== value){ if(value){ this.props.setError(this.props.index, 'media'); } else { this.props.deleteError(this.props.index, 'media'); this.props.deleteProperty(this.props.index, 'media'); this.props.deleteProperty(this.props.index, 'url'); this.setState({ error: false}); } } } onChangeRadio = (value) => { this.props.setError(this.props.index, 'media'); var oldValue = this.state.radioValue; this.setState({radioValue: value, error: false}); // delete property 'oldValue', so that all old media files are reset this.props.deleteProperty(this.props.index, 'media', oldValue); if(oldValue === 'picture'){ this.props.deleteProperty(this.props.index, 'url'); } } uploadPicture = (pic) => { if(!(/^image\/.*/.test(pic.type))){ this.props.setError(this.props.index, 'media'); this.setState({ error: true }); this.props.deleteProperty(this.props.index, 'url'); } else { this.props.deleteError(this.props.index, 'media'); this.setState({ error: false }); this.props.changeContent(URL.createObjectURL(pic), this.props.index, 'url'); } this.props.changeContent(pic.name, this.props.index, 'media', 'picture'); } render() { return (