import React from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; 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 pusherActions from '../../services/pusher/actions'; import { uriSource, uriType, } from '../../util/helpers'; import { i18n, I18n } from '../../locale'; class EditRadio extends React.Component { constructor(props) { super(props); this.state = { enabled: false, seeds: [], uri: '', error_message: null, }; } componentDidMount() { this.props.uiActions.setWindowTitle(i18n('modal.edit_radio.title')); if (this.props.radio && this.props.radio.enabled) { this.loadRadio(this.props.radio); } } componentDidUpdate = ({ radio: prev_radio }) => { const { radio } = this.props; if (!prev_radio && radio) this.loadRadio(radio); } loadRadio(radio) { const seeds = [...radio.seed_tracks, ...radio.seed_artists, ...radio.seed_genres]; this.setState({ seeds, enabled: radio.enabled }); } handleStart(e) { e.preventDefault(); let valid_seeds = true; const seeds = this.mapSeeds(); for (let i = 0; i < seeds.length; i++) { if (seeds[i].unresolved !== undefined) { valid_seeds = false; continue; } } if (valid_seeds) { this.props.pusherActions.startRadio(this.state.seeds); window.history.back(); } else { this.setState({ error_message: i18n('modal.edit_radio.invalid_seed_uri') }); } } handleUpdate(e) { e.preventDefault(); let valid_seeds = true; const seeds = this.mapSeeds(); for (let i = 0; i < seeds.length; i++) { if (seeds[i].unresolved !== undefined) { valid_seeds = false; continue; } } if (valid_seeds) { this.props.pusherActions.updateRadio(this.state.seeds); window.history.back(); } else { this.setState({ error_message: i18n('modal.edit_radio.invalid_seed_uri') }); } } handleStop(e) { e.preventDefault(); this.props.pusherActions.stopRadio(); this.props.uiActions.closeModal(); } addSeed(e) { e.preventDefault(); if (this.state.uri == '') { this.setState({ error_message: i18n('errors.required') }); return; } this.setState({ error_message: null }); const seeds = Object.assign([], this.state.seeds); let uris = this.state.uri.split(','); if (uris.length >= 5) { uris = uris.slice(0, 5); this.setState({ error_message: i18n('modal.edit_radio.too_many_seeds') }); } for (let i = 0; i < uris.length; i++) { if (uriSource(uris[i]) !== 'spotify') { this.setState({ error_message: i18n('modal.edit_radio.only_spotify_uris') }); return; } if (seeds.indexOf(uris[i]) > -1) { this.setState({ error_message: i18n('modal.edit_radio.too_many_seeds') }); } else { seeds.push(uris[i]); } // Resolve switch (uriType(uris[i])) { case 'track': this.props.spotifyActions.getTrack(uris[i]); break; case 'artist': this.props.spotifyActions.getArtist(uris[i]); break; } } // commit to state this.setState({ seeds, uri: '', }); } removeSeed(uri) { const seeds = []; for (let i = 0; i < this.state.seeds.length; i++) { if (this.state.seeds[i] != uri) { seeds.push(this.state.seeds[i]); } } this.setState({ seeds }); } mapSeeds() { const seeds = []; if (this.state.seeds) { for (let i = 0; i < this.state.seeds.length && i < 5; i++) { const uri = this.state.seeds[i]; if (uri) { if (uriType(uri) == 'artist') { if (this.props.artists && this.props.artists.hasOwnProperty(uri)) { seeds.push(this.props.artists[uri]); } else { seeds.push({ unresolved: true, uri, }); } } else if (uriType(uri) == 'track') { if (this.props.tracks && this.props.tracks.hasOwnProperty(uri)) { seeds.push(this.props.tracks[uri]); } else { seeds.push({ unresolved: true, uri, }); } } } } } return seeds; } renderSeeds() { const seeds = this.mapSeeds(); if (seeds.length > 0) { return (
{ seeds.map((seed, index) => (
{seed.unresolved ? {seed.uri} : {seed.name} } {!seed.unresolved ? (  ( {uriType(seed.uri)} ) ) : null} this.removeSeed(seed.uri)}>
)) }
); } return (
); } render() { return (

{ (this.state.enabled ? this.handleUpdate(e) : this.handleStart(e)); }}> {this.renderSeeds()}
this.setState({ uri: e.target.value, error_message: null })} value={this.state.uri} /> this.addSeed(e)}> {this.state.error_message ? {this.state.error_message} : null}
{this.state.enabled ? ( this.handleStop(e)}> ) : null} {this.state.enabled ? ( ) : ( )}
); } } const mapStateToProps = (state, ownProps) => ({ mopidy_connected: state.mopidy.connected, radio: state.core.radio, artists: state.core.artists, tracks: state.core.tracks, }); const mapDispatchToProps = (dispatch) => ({ coreActions: bindActionCreators(coreActions, dispatch), uiActions: bindActionCreators(uiActions, dispatch), mopidyActions: bindActionCreators(mopidyActions, dispatch), pusherActions: bindActionCreators(pusherActions, dispatch), spotifyActions: bindActionCreators(spotifyActions, dispatch), }); export default connect(mapStateToProps, mapDispatchToProps)(EditRadio);