Files
Iris/src/js/components/Modal/EditRadioModal.js

181 lines
4.7 KiB
JavaScript
Raw Normal View History

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 = {
enabled: false,
seeds: [],
2017-04-18 04:44:39 +12:00
uri: '',
error_message: null
2017-01-30 16:59:37 +13:00
}
}
componentDidMount(){
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
}
2017-04-18 04:44:39 +12:00
handleStart(e){
e.preventDefault()
this.props.pusherActions.startRadio(this.state.seeds)
this.props.uiActions.closeModal()
}
handleUpdate(e){
e.preventDefault()
this.props.pusherActions.updateRadio(this.state.seeds)
this.props.uiActions.closeModal()
}
handleStop(e){
e.preventDefault()
this.props.pusherActions.stopRadio()
this.props.uiActions.closeModal()
2017-01-30 16:59:37 +13:00
}
addSeed(){
2017-04-18 04:44:39 +12:00
if (this.state.uri == ''){
this.setState({error_message: 'Cannot be empty'});
return null;
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-02-01 10:10:31 +13:00
}
}
// 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-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
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-02-01 10:10:31 +13:00
seeds.push(this.props.artists[uri])
} else {
seeds.push({
type: 'artist',
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-02-01 10:10:31 +13:00
seeds.push(this.props.tracks[uri])
} else {
seeds.push({
type: 'track',
unresolved: true,
uri: uri
})
}
}
}
2017-01-30 16:59:37 +13:00
}
}
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> }
<span className="grey-text">&nbsp;({seed.type})</span>
<button className="discrete remove-uri no-hover" onClick={e => this.removeSeed(seed.uri)}>
<FontAwesome name="close" />&nbsp;Remove
</button>
</div>
)
})
}
</div>
2017-02-05 19:57:29 +13:00
</div>
)
} else {
return (
<div>
<div className="list">
<div className="list-item no-click">
<span className="grey-text">No seeds</span>
</div>
</div>
</div>
)
}
2017-01-30 16:59:37 +13:00
}
render(){
return (
<div>
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>
{this.renderSeeds()}
<div className="field text">
<span className="label">URI(s)</span>
2017-04-18 04:44:39 +12:00
<input
type="text"
onChange={e => this.setState({uri: e.target.value, error_message: null})}
value={this.state.uri} />
2017-09-28 14:39:22 +13:00
<span className="button discrete add-uri no-hover" onClick={e => this.addSeed()}>
2017-04-18 04:44:39 +12:00
<FontAwesome name="plus" />&nbsp; Add
2017-09-28 14:39:22 +13:00
</span>
2017-09-29 08:57:11 +13:00
{this.state.error_message ? <span className="description error">{this.state.error_message}</span> : null}
</div>
</form>
<form>
<div className="actions centered-text">
2017-04-18 04:44:39 +12:00
{this.state.enabled ? <button className="destructive wide" onClick={e => this.handleStop(e)}>Stop</button> : null}
{this.state.enabled ? <button className="primary wide" onClick={e => this.handleUpdate(e)}>Save</button> : <button className="primary wide" onClick={e => this.handleStart(e)}>Start</button>}
2017-01-30 16:59:37 +13:00
</div>
</form>
</div>
)
}
}