Enforcing valid URIs

This commit is contained in:
James Barnsley
2017-11-17 08:32:12 +13:00
parent 3d5d744c5c
commit 2b959f7803
2 changed files with 67 additions and 23 deletions

View File

@ -26,15 +26,43 @@ export default class EditRadioModal extends React.Component{
}
handleStart(e){
e.preventDefault()
this.props.pusherActions.startRadio(this.state.seeds)
this.props.uiActions.closeModal()
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);
this.props.uiActions.closeModal();
} else {
this.setState({error_message: "Remove all invalid seed URIs"});
}
}
handleUpdate(e){
e.preventDefault()
this.props.pusherActions.updateRadio(this.state.seeds)
this.props.uiActions.closeModal()
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: "Remove all invalid seed URIs"});
}
}
handleStop(e){
@ -45,20 +73,31 @@ export default class EditRadioModal extends React.Component{
addSeed(){
if (this.state.uri == ''){
this.setState({error_message: 'Cannot be empty'})
return null
this.setState({error_message: 'Cannot be empty'});
return null;
}
var seeds = Object.assign([],this.state.seeds)
var seeds = Object.assign([], this.state.seeds)
var uris = this.state.uri.split(',')
for (var i = 0; i < uris.length; i++){
if (seeds.indexOf(uris[i]) > -1){
this.setState({error_message: 'URI already added'})
this.setState({error_message: 'URI already added'});
} else {
seeds.push(uris[i])
this.setState({error_message: null})
}
seeds.push(uris[i]);
this.setState({error_message: null});
}
// 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
@ -75,10 +114,10 @@ export default class EditRadioModal extends React.Component{
seeds.push(this.state.seeds[i])
}
}
this.setState({seeds: seeds})
this.setState({seeds: seeds});
}
renderSeeds(){
mapSeeds(){
var seeds = []
if (this.state.seeds){
@ -87,20 +126,18 @@ export default class EditRadioModal extends React.Component{
if (uri){
if (helpers.uriType(uri) == 'artist'){
if (this.props.artists && this.props.artists.hasOwnProperty(uri)){
seeds.push(this.props.artists[uri])
seeds.push(this.props.artists[uri]);
} else {
seeds.push({
type: 'artist',
unresolved: true,
uri: uri
})
}
} else if (helpers.uriType(uri) == 'track'){
if (this.props.tracks && this.props.tracks.hasOwnProperty(uri)){
seeds.push(this.props.tracks[uri])
seeds.push(this.props.tracks[uri]);
} else {
seeds.push({
type: 'track',
unresolved: true,
uri: uri
})
@ -110,6 +147,12 @@ export default class EditRadioModal extends React.Component{
}
}
return seeds;
}
renderSeeds(){
var seeds = this.mapSeeds();
if (seeds.length > 0){
return (
<div>
@ -119,7 +162,7 @@ export default class EditRadioModal extends React.Component{
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>
{!seed.unresolved ? <span className="grey-text">&nbsp;({seed.type})</span> : null}
<button className="discrete remove-uri no-hover" onClick={e => this.removeSeed(seed.uri)}>
<FontAwesome name="close" />&nbsp;Remove
</button>

View File

@ -373,12 +373,13 @@ const PusherMiddleware = (function(){
request(store, 'change_radio', data)
.then(
response => {
store.dispatch(uiActions.processFinished('PUSHER_RADIO_PROCESS'));
if (response.status == 0){
store.dispatch(uiActions.createNotification(response.message, 'bad'))
store.dispatch(uiActions.createNotification(response.message, 'bad'));
}
store.dispatch(uiActions.processFinished('PUSHER_RADIO_PROCESS'))
},
error => {
error => {
store.dispatch(uiActions.processFinished('PUSHER_RADIO_PROCESS'));
store.dispatch(coreActions.handleException(
'Could not change radio',
error