Files
Iris/src/js/views/modals/EditRadio.js

266 lines
6.9 KiB
JavaScript
Raw Normal View History

2017-01-30 16:59:37 +13:00
2018-07-09 19:09:12 +12:00
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';
2018-07-09 19:09:12 +12:00
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'
2018-02-01 08:06:40 +13:00
import * as helpers from '../../helpers';
2017-01-30 16:59:37 +13:00
2018-07-09 19:09:12 +12:00
class EditRadio extends React.Component{
2017-01-30 16:59:37 +13:00
constructor(props){
super(props)
this.state = {
enabled: false,
seeds: [],
2017-04-18 04:44:39 +12:00
uri: '',
error_message: null
2017-01-30 16:59:37 +13:00
}
}
componentDidMount(){
2018-07-13 16:48:41 +12:00
this.props.uiActions.setWindowTitle("Edit radio");
2018-07-24 20:07:21 +12:00
if (this.props.radio && this.props.radio.enabled){
this.loadRadio(this.props.radio);
2018-07-24 20:07:21 +12:00
}
2017-01-30 16:59:37 +13:00
}
componentWillReceiveProps(nextProps){
if (!this.props.radio && nextProps.radio){
this.loadRadio(nextProps.radio);
}
}
loadRadio(radio){
var seeds = [...radio.seed_tracks, ...radio.seed_artists, ...radio.seed_genres];
this.setState({seeds: seeds, enabled: radio.enabled});
this.props.spotifyActions.resolveRadioSeeds(radio);
}
2017-04-18 04:44:39 +12:00
handleStart(e){
2017-11-17 08:32:12 +13:00
e.preventDefault();
var valid_seeds = true;
var seeds = this.mapSeeds();
for (var 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();
2017-11-17 08:32:12 +13:00
} else {
this.setState({error_message: "Invalid seed URI(s)"});
2017-11-17 08:32:12 +13:00
}
2017-04-18 04:44:39 +12:00
}
handleUpdate(e){
2017-11-17 08:32:12 +13:00
e.preventDefault();
var valid_seeds = true;
var seeds = this.mapSeeds();
for (var 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);
this.props.uiActions.closeModal();
} else {
this.setState({error_message: "Invalid seed URI(s)"});
2017-11-17 08:32:12 +13:00
}
2017-04-18 04:44:39 +12:00
}
handleStop(e){
e.preventDefault()
this.props.pusherActions.stopRadio()
this.props.uiActions.closeModal()
2017-01-30 16:59:37 +13:00
}
2018-07-24 20:07:21 +12:00
addSeed(e){
e.preventDefault();
2017-04-18 04:44:39 +12:00
if (this.state.uri == ''){
this.setState({error_message: 'Cannot be empty'});
return;
2017-04-18 04:44:39 +12:00
}
var seeds = Object.assign([],this.state.seeds);
var uris = this.state.uri.split(',');
2017-02-01 10:10:31 +13:00
for (var i = 0; i < uris.length; i++){
if (helpers.uriSource(uris[i]) !== 'spotify'){
this.setState({error_message: 'Non-Spotify URIs not supported'});
return;
}
2017-04-18 04:44:39 +12:00
if (seeds.indexOf(uris[i]) > -1){
this.setState({error_message: 'URI already added'});
2017-02-01 10:10:31 +13:00
} else {
seeds.push(uris[i]);
this.setState({error_message: null});
2017-11-17 08:32:12 +13:00
}
// Resolve
switch (helpers.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: seeds,
2017-02-01 10:10:31 +13:00
uri: ''
})
}
removeSeed(uri){
2017-02-01 10:10:31 +13:00
var seeds = []
for (var i = 0; i < this.state.seeds.length; i++){
if (this.state.seeds[i] != uri){
seeds.push(this.state.seeds[i])
}
}
2017-11-17 08:32:12 +13:00
this.setState({seeds: seeds});
2017-01-30 16:59:37 +13:00
}
2017-11-17 08:32:12 +13:00
mapSeeds(){
2017-01-30 16:59:37 +13:00
var seeds = []
2017-02-01 10:10:31 +13:00
2017-02-05 19:57:29 +13:00
if (this.state.seeds){
2017-02-01 10:10:31 +13:00
for (var i = 0; i < this.state.seeds.length; i++){
var uri = this.state.seeds[i]
if (uri){
2017-04-18 04:44:39 +12:00
if (helpers.uriType(uri) == 'artist'){
if (this.props.artists && this.props.artists.hasOwnProperty(uri)){
2017-11-17 08:32:12 +13:00
seeds.push(this.props.artists[uri]);
2017-02-01 10:10:31 +13:00
} else {
seeds.push({
unresolved: true,
uri: uri
})
}
2017-04-18 04:44:39 +12:00
} else if (helpers.uriType(uri) == 'track'){
if (this.props.tracks && this.props.tracks.hasOwnProperty(uri)){
2017-11-17 08:32:12 +13:00
seeds.push(this.props.tracks[uri]);
2017-02-01 10:10:31 +13:00
} else {
seeds.push({
unresolved: true,
uri: uri
})
}
}
}
2017-01-30 16:59:37 +13:00
}
}
2017-11-17 08:32:12 +13:00
return seeds;
}
renderSeeds(){
var seeds = this.mapSeeds();
if (seeds.length > 0){
return (
<div>
<div className="list">
{
seeds.map((seed,index) => {
return (
<div className="list-item" key={seed.uri}>
{seed.unresolved ? <span className="grey-text">{seed.uri}</span> : <span>{seed.name}</span> }
2017-11-17 08:32:12 +13:00
{!seed.unresolved ? <span className="grey-text">&nbsp;({seed.type})</span> : null}
2018-07-24 20:07:21 +12:00
<span className="button discrete remove-uri no-hover" onClick={e => this.removeSeed(seed.uri)}>
2018-05-29 21:05:03 +12:00
<Icon name="delete" />Remove
2018-07-24 20:07:21 +12:00
</span>
</div>
)
})
}
</div>
2017-02-05 19:57:29 +13:00
</div>
)
} else {
return (
2018-07-24 20:07:21 +12:00
<div className="no-results">No seeds</div>
)
}
2017-01-30 16:59:37 +13:00
}
render(){
return (
2018-07-13 16:48:41 +12:00
<Modal className="modal--edit-radio">
2017-09-28 14:39:22 +13:00
<h1>Radio</h1>
<h2 className="grey-text">Add and remove seeds to shape the sound of your radio. Radio uses Spotify's recommendations engine to suggest tracks similar to your seeds.</h2>
2017-04-18 04:44:39 +12:00
<form onSubmit={e => {(this.state.enabled ? this.handleUpdate(e) : this.handleStart(e))}}>
2017-04-18 04:44:39 +12:00
{this.renderSeeds()}
<div className="field text">
<div className="name">URI(s)</div>
<div className="input">
<input
type="text"
onChange={e => this.setState({uri: e.target.value, error_message: null})}
value={this.state.uri}
/>
<span className="button discrete add-uri no-hover" onClick={e => this.addSeed(e)}>
<Icon name="add" />Add
</span>
{this.state.error_message ? <span className="description error">{this.state.error_message}</span> : null}
</div>
</div>
<div className="actions centered-text">
{this.state.enabled ? <span className="button destructive large" onClick={e => this.handleStop(e)}>Stop</span> : null}
2018-02-01 08:06:40 +13:00
{this.state.enabled ? <button className="primary large" onClick={e => this.handleUpdate(e)}>Save</button> : <button className="primary large" onClick={e => this.handleStart(e)}>Start</button>}
2017-01-30 16:59:37 +13:00
</div>
</form>
2018-07-09 19:09:12 +12:00
</Modal>
2017-01-30 16:59:37 +13:00
)
}
2018-07-09 19:09:12 +12:00
}
const mapStateToProps = (state, ownProps) => {
return {
mopidy_connected: state.mopidy.connected,
radio: state.core.radio,
artists: state.core.artists,
tracks: state.core.tracks
2018-07-09 19:09:12 +12:00
}
}
const mapDispatchToProps = (dispatch) => {
return {
coreActions: bindActionCreators(coreActions, dispatch),
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
pusherActions: bindActionCreators(pusherActions, dispatch),
2018-07-09 19:09:12 +12:00
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(EditRadio)