import React from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import Modal from './Modal'; 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 { uriSource } from '../../util/helpers'; class EditPlaylist extends React.Component { constructor(props) { super(props); this.state = { loaded: false, error: null, name: '', description: '', image: null, public: false, collaborative: false, }; } componentDidMount() { this.props.uiActions.setWindowTitle('Edit playlist'); if (this.props.playlist) { this.setState({ loaded: true, name: this.props.playlist.name, description: this.props.playlist.description, public: (this.props.playlist.public == true), collaborative: (this.props.playlist.collaborative == true), }); } else { switch (uriSource(this.props.uri)) { case 'spotify': this.props.spotifyActions.getPlaylist(this.props.uri); this.props.spotifyActions.following(this.props.uri); break; default: if (this.props.mopidy_connected) { this.props.mopidyActions.getPlaylist(this.props.uri); } break; } } } componentDidUpdate = ({ mopidy_connected: prev_mopidy_connected, }) => { const { uri, playlist, mopidy_connected, mopidyActions: { getPlaylist, }, } = this.props; if (!prev_mopidy_connected && mopidy_connected && !playlist) { getPlaylist(uri); } } static getDerivedStateFromProps({ playlist }, state) { if (playlist && !state.loaded) { return { loaded: true, name: playlist.name, description: playlist.description, public: (playlist.public === true), collaborative: (playlist.collaborative === true), }; } return null; } savePlaylist(e) { e.preventDefault(); if (!this.state.name || this.state.name == '') { this.setState({ error: 'Name is required' }); return false; } this.props.coreActions.savePlaylist( this.props.uri, this.state.name, this.state.description, this.state.public, this.state.collaborative, this.state.image, ); window.history.back(); return false; } setImage(e) { const self = this; // Create a file-reader to import the selected image as a base64 string const file_reader = new FileReader(); // Once the image is loaded, convert the result file_reader.addEventListener('load', (e) => { const 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 (uriSource(this.props.uri)) { case 'spotify': return (