import React, { PropTypes } from 'react' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' import { Link } from 'react-router' import ReactGA from 'react-ga' import Modal from './Modal'; import Icon from '../../components/Icon'; import * as coreActions from '../../services/core/actions' import * as uiActions from '../../services/ui/actions' import * as mopidyActions from '../../services/mopidy/actions' import * as spotifyActions from '../../services/spotify/actions' import * as helpers from '../../helpers'; class EditPlaylist extends React.Component{ constructor(props){ super(props) this.state = { error: null, name: '', description: '', image: null, public: false, collaborative: false } } componentDidMount(){ this.props.uiActions.setWindowTitle("Edit playlist"); if (this.props.playlist){ this.setState({ name: this.props.playlist.name, description: this.props.playlist.description, public: (this.props.playlist.public == true), collaborative: (this.props.playlist.collaborative == true) }); } else { switch (helpers.uriSource(this.props.params.uri)){ case 'spotify': this.props.spotifyActions.getPlaylist(this.props.params.uri); this.props.spotifyActions.following(this.props.params.uri); break default: if (props.mopidy_connected){ this.props.mopidyActions.getPlaylist(this.props.params.uri); } break } } } componentWillReceiveProps(nextProps){ if (!this.props.playlist && nextProps.playlist){ this.setState({ name: nextProps.playlist.name, description: nextProps.playlist.description, public: (nextProps.playlist.public == true), collaborative: (nextProps.playlist.collaborative == true) }); } } savePlaylist(e){ e.preventDefault(); if (!this.state.name || this.state.name == ''){ this.setState({error: 'Name is required'}) return false } else { this.props.coreActions.savePlaylist( this.props.params.uri, this.state.name, this.state.description, this.state.public, this.state.collaborative, this.state.image ); window.history.back(); return false; } } setImage(e){ var self = this; // Create a file-reader to import the selected image as a base64 string var file_reader = new FileReader(); // Once the image is loaded, convert the result file_reader.addEventListener("load", function(e){ var image_base64 = e.target.result.replace('data:image/jpeg;base64,',''); self.setState({image: image_base64}); }); // This calls the filereader to load the file file_reader.readAsDataURL(e.target.files[0]); } renderFields(){ switch (helpers.uriSource(this.props.params.uri)){ case 'spotify': return (
Name
this.setState({ name: e.target.value })} value={ this.state.name } />
Description
this.setState({ description: e.target.value })} value={ this.state.description } />
Cover image
this.setImage(e)} />
JPEG only, 256kB max. Leave empty to keep cover image unchanged.
Options
) break default: return (
Name
this.setState({ name: e.target.value })} value={ this.state.name } />
) } } render(){ return (

Edit playlist

{this.state.error ?

{this.state.error}

: null}
this.savePlaylist(e)}> {this.renderFields()}
) } } const mapStateToProps = (state, ownProps) => { return { mopidy_connected: state.mopidy.connected, playlist: (state.core.playlists[ownProps.params.uri] !== undefined ? state.core.playlists[ownProps.params.uri] : null), playlists: state.core.playlists } } const mapDispatchToProps = (dispatch) => { return { coreActions: bindActionCreators(coreActions, dispatch), uiActions: bindActionCreators(uiActions, dispatch), mopidyActions: bindActionCreators(mopidyActions, dispatch), spotifyActions: bindActionCreators(spotifyActions, dispatch) } } export default connect(mapStateToProps, mapDispatchToProps)(EditPlaylist)