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 (
this.onChangeSwitch(e.target.checked)} color="primary" /> } /> {this.state.checked ?
{this.onChangeRadio(e.target.value);}}> } label="Bild" labelPlacement="end" /> } label="Youtube-Video" labelPlacement="end" /> {this.state.radioValue === 'picture' ?
{!this.props.error ?
{`Beachte, dass das Foto zusätzlich in den Ordner public/media/tutorial unter dem Namen '${this.props.picture}' abgespeichert werden muss.`} {this.props.url
:
{this.state.error ? {`Die übergebene Datei entspricht nicht dem geforderten Bild-Format. Überprüfe, ob es sich um ein Bild handelt und versuche es nochmal.`} : {`Wähle ein Bild aus.`} }
} {/*upload picture*/}
{this.uploadPicture(e.target.files[0]);}} id={`picture ${this.props.index}`} type="file" />
: /*youtube-video*/
{this.props.youtube && !this.props.error ?
{`Stelle sicher, dass das unten angezeigte Youtube-Video funktioniert, andernfalls überprüfe die Youtube-ID.`}