2017-01-30 16:59:37 +13:00
|
|
|
|
|
|
|
|
import React, { PropTypes } from 'react'
|
|
|
|
|
import FontAwesome from 'react-fontawesome'
|
|
|
|
|
|
|
|
|
|
import Icon from '../Icon'
|
|
|
|
|
import * as helpers from '../../helpers'
|
|
|
|
|
|
|
|
|
|
export default class EditRadioModal extends React.Component{
|
|
|
|
|
|
|
|
|
|
constructor(props){
|
|
|
|
|
super(props)
|
|
|
|
|
this.state = {
|
2017-01-31 08:44:12 +13:00
|
|
|
enabled: false,
|
|
|
|
|
seeds: [],
|
2017-02-01 10:10:31 +13:00
|
|
|
uri: ''
|
2017-01-30 16:59:37 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount(){
|
2017-01-31 08:44:12 +13:00
|
|
|
if (!this.props.radio || !this.props.radio.enabled) return null
|
|
|
|
|
var seeds = [...this.props.radio.seed_tracks, ...this.props.radio.seed_artists, ...this.props.radio.seed_genres]
|
|
|
|
|
this.setState({seeds: seeds, enabled: this.props.radio.enabled})
|
|
|
|
|
|
2017-02-01 10:10:31 +13:00
|
|
|
this.props.spotifyActions.resolveRadioSeeds(this.props.radio)
|
2017-01-30 16:59:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
save(){
|
2017-01-31 08:44:12 +13:00
|
|
|
this.props.pusherActions.startRadio(this.state.seeds)
|
2017-01-30 16:59:37 +13:00
|
|
|
this.props.uiActions.closeModal()
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 08:44:12 +13:00
|
|
|
stop(){
|
|
|
|
|
this.props.pusherActions.stopRadio()
|
|
|
|
|
this.props.uiActions.closeModal()
|
2017-01-30 16:59:37 +13:00
|
|
|
}
|
|
|
|
|
|
2017-01-31 08:44:12 +13:00
|
|
|
addSeed(){
|
2017-02-01 10:10:31 +13:00
|
|
|
if (this.state.uri == '') return null
|
2017-01-31 08:44:12 +13:00
|
|
|
|
|
|
|
|
var seeds = Object.assign([],this.state.seeds)
|
2017-02-01 10:10:31 +13:00
|
|
|
var uris = this.state.uri.split(',')
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < uris.length; i++){
|
|
|
|
|
if (seeds.indexOf(uris[i]) <= -1){
|
|
|
|
|
seeds.push(uris[i])
|
|
|
|
|
} else {
|
|
|
|
|
this.props.uiActions.createNotification(uris[i]+' already added','bad')
|
|
|
|
|
}
|
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-02-01 10:10:31 +13:00
|
|
|
this.setState({seeds: seeds})
|
2017-01-30 16:59:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderSeeds(){
|
|
|
|
|
var seeds = []
|
2017-02-01 10:10:31 +13:00
|
|
|
|
|
|
|
|
if (this.state.enabled && this.state.seeds){
|
|
|
|
|
for (var i = 0; i < this.state.seeds.length; i++){
|
|
|
|
|
var uri = this.state.seeds[i]
|
|
|
|
|
if (uri){
|
|
|
|
|
if (helpers.uriType(uri) == 'artist' && this.props.artists){
|
|
|
|
|
if (this.props.artists.hasOwnProperty(uri)){
|
|
|
|
|
seeds.push(this.props.artists[uri])
|
|
|
|
|
} else {
|
|
|
|
|
seeds.push({
|
|
|
|
|
type: 'artist',
|
|
|
|
|
unresolved: true,
|
|
|
|
|
uri: uri
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} else if (helpers.uriType(uri) == 'track' && this.props.tracks){
|
|
|
|
|
if (this.props.tracks.hasOwnProperty(uri)){
|
|
|
|
|
seeds.push(this.props.tracks[uri])
|
|
|
|
|
} else {
|
|
|
|
|
seeds.push({
|
|
|
|
|
type: 'track',
|
|
|
|
|
unresolved: true,
|
|
|
|
|
uri: uri
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-30 16:59:37 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="list">
|
|
|
|
|
{
|
2017-02-01 10:10:31 +13:00
|
|
|
seeds.map((seed,index) => {
|
2017-01-30 16:59:37 +13:00
|
|
|
return (
|
2017-02-01 10:10:31 +13:00
|
|
|
<div className="list-item" key={seed.uri}>
|
|
|
|
|
{seed.unresolved ? <span className="grey-text">{seed.uri}</span> : <span>{seed.name}</span> }
|
|
|
|
|
<span className="grey-text"> ({seed.type})</span>
|
|
|
|
|
<FontAwesome name="close" className="pull-right destructive" onClick={() => this.removeSeed(seed.uri)} />
|
2017-01-30 16:59:37 +13:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 08:44:12 +13:00
|
|
|
renderActions(){
|
|
|
|
|
if (this.state.enabled){
|
|
|
|
|
return (
|
|
|
|
|
<span>
|
|
|
|
|
<button className="primary wide" onClick={e => this.save()}>Save</button>
|
|
|
|
|
<button className="destructive wide" onClick={e => this.stop()}>Stop</button>
|
|
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
}else{
|
|
|
|
|
return (
|
|
|
|
|
<span>
|
|
|
|
|
<button className="primary wide" onClick={e => this.save()}>Start</button>
|
|
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-30 16:59:37 +13:00
|
|
|
render(){
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<h4>Edit radio</h4>
|
2017-01-31 08:44:12 +13:00
|
|
|
|
2017-02-01 10:10:31 +13:00
|
|
|
<h3>Current seeds</h3>
|
|
|
|
|
{this.renderSeeds()}
|
|
|
|
|
|
|
|
|
|
<form>
|
|
|
|
|
<h3>Add seeds</h3>
|
2017-01-30 16:59:37 +13:00
|
|
|
<div className="field">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
2017-02-01 10:10:31 +13:00
|
|
|
placeholder="Comma-separated URIs"
|
|
|
|
|
onChange={e => this.setState({uri: e.target.value})}
|
2017-01-30 16:59:37 +13:00
|
|
|
value={this.state.uri} />
|
2017-02-01 10:10:31 +13:00
|
|
|
<button className="discrete" onClick={e => this.addSeed()}><FontAwesome name="check" /></button>
|
2017-01-31 08:44:12 +13:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="actions centered-text">
|
|
|
|
|
{this.renderActions()}
|
2017-01-30 16:59:37 +13:00
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|