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'
|
2018-10-24 21:34:41 +13:00
|
|
|
import Link from '../../components/Link'
|
2018-07-09 19:09:12 +12:00
|
|
|
import ReactGA from 'react-ga'
|
|
|
|
|
|
|
|
|
|
import Modal from './Modal';
|
|
|
|
|
import Icon from '../../components/Icon';
|
2018-07-25 21:33:14 +12:00
|
|
|
|
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'
|
2018-07-25 21:33:14 +12:00
|
|
|
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 = {
|
2017-01-31 08:44:12 +13:00
|
|
|
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){
|
2018-07-25 21:33:14 +12:00
|
|
|
this.loadRadio(this.props.radio);
|
2018-07-24 20:07:21 +12:00
|
|
|
}
|
2017-01-30 16:59:37 +13:00
|
|
|
}
|
|
|
|
|
|
2018-07-25 21:33:14 +12: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);
|
2018-07-21 11:43:40 +12:00
|
|
|
window.history.back();
|
2017-11-17 08:32:12 +13:00
|
|
|
} else {
|
2017-11-21 14:50:37 +13:00
|
|
|
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 {
|
2017-11-21 14:50:37 +13:00
|
|
|
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()
|
2017-01-31 08:44:12 +13:00
|
|
|
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 == ''){
|
2017-11-17 21:38:24 +13:00
|
|
|
this.setState({error_message: 'Cannot be empty'});
|
2017-11-21 14:50:37 +13:00
|
|
|
return;
|
2017-04-18 04:44:39 +12:00
|
|
|
}
|
2017-01-31 08:44:12 +13:00
|
|
|
|
2017-11-17 21:38:24 +13: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++){
|
2017-11-17 21:38:24 +13:00
|
|
|
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){
|
2017-11-17 21:38:24 +13:00
|
|
|
this.setState({error_message: 'URI already added'});
|
2017-02-01 10:10:31 +13:00
|
|
|
} else {
|
2017-11-17 21:38:24 +13:00
|
|
|
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;
|
|
|
|
|
}
|
2017-01-31 08:44:12 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// commit to state
|
|
|
|
|
this.setState({
|
|
|
|
|
seeds: seeds,
|
2017-02-01 10:10:31 +13:00
|
|
|
uri: ''
|
2017-01-31 08:44:12 +13:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-01-31 08:44:12 +13:00
|
|
|
}
|
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();
|
|
|
|
|
|
2017-04-20 16:50:35 +12:00
|
|
|
if (seeds.length > 0){
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<div className="list">
|
|
|
|
|
{
|
|
|
|
|
seeds.map((seed,index) => {
|
|
|
|
|
return (
|
2018-09-24 21:37:59 +12:00
|
|
|
<div className="list__item" key={seed.uri}>
|
2018-10-13 15:57:01 +13:00
|
|
|
{seed.unresolved ? <span className="mid_grey-text">{seed.uri}</span> : <span>{seed.name}</span> }
|
|
|
|
|
{!seed.unresolved ? <span className="mid_grey-text"> ({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>
|
2017-04-20 16:50:35 +12:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</div>
|
2017-02-05 19:57:29 +13:00
|
|
|
</div>
|
2017-04-20 16:50:35 +12:00
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
return (
|
2018-07-24 20:07:21 +12:00
|
|
|
<div className="no-results">No seeds</div>
|
2017-04-20 16:50:35 +12:00
|
|
|
)
|
|
|
|
|
}
|
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>
|
2018-10-13 15:57:01 +13:00
|
|
|
<h2 className="mid_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
|
|
|
|
2018-07-25 21:33:14 +12:00
|
|
|
<form onSubmit={e => {(this.state.enabled ? this.handleUpdate(e) : this.handleStart(e))}}>
|
|
|
|
|
|
2017-04-18 04:44:39 +12:00
|
|
|
{this.renderSeeds()}
|
|
|
|
|
|
2017-07-14 08:47:02 +12:00
|
|
|
<div className="field text">
|
2018-06-06 16:54:09 +12:00
|
|
|
<div className="name">URI(s)</div>
|
|
|
|
|
<div className="input">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
onChange={e => this.setState({uri: e.target.value, error_message: null})}
|
2018-06-06 21:11:27 +12:00
|
|
|
value={this.state.uri}
|
|
|
|
|
/>
|
2018-07-25 21:33:14 +12:00
|
|
|
<span className="button discrete add-uri no-hover" onClick={e => this.addSeed(e)}>
|
2018-06-06 16:54:09 +12:00
|
|
|
<Icon name="add" />Add
|
2018-07-25 21:33:14 +12:00
|
|
|
</span>
|
2018-06-06 16:54:09 +12:00
|
|
|
{this.state.error_message ? <span className="description error">{this.state.error_message}</span> : null}
|
|
|
|
|
</div>
|
2017-01-31 08:44:12 +13:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="actions centered-text">
|
2018-12-05 21:39:06 +13:00
|
|
|
{this.state.enabled ? <span className="button button--destructive button--large" onClick={e => this.handleStop(e)}>Stop</span> : null}
|
2018-07-25 21:33:14 +12:00
|
|
|
|
2018-12-05 21:39:06 +13:00
|
|
|
{this.state.enabled ? <button className="button button--primary button--large" onClick={e => this.handleUpdate(e)}>Save</button> : <button className="button button--primary button--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,
|
2018-07-25 21:33:14 +12:00
|
|
|
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),
|
2018-07-25 21:33:14 +12:00
|
|
|
pusherActions: bindActionCreators(pusherActions, dispatch),
|
2018-07-09 19:09:12 +12:00
|
|
|
spotifyActions: bindActionCreators(spotifyActions, dispatch)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(EditRadio)
|