2016-10-26 16:55:47 +13:00
|
|
|
|
|
|
|
|
import React, { PropTypes } from 'react'
|
|
|
|
|
import { connect } from 'react-redux'
|
|
|
|
|
import { bindActionCreators } from 'redux'
|
2017-05-12 05:22:57 +12:00
|
|
|
import FontAwesome from 'react-fontawesome'
|
2016-10-26 16:55:47 +13:00
|
|
|
|
2017-05-26 09:04:58 +12:00
|
|
|
import Header from '../../components/Header'
|
2017-01-11 13:52:55 +13:00
|
|
|
import ArtistSentence from '../../components/ArtistSentence'
|
2017-05-16 08:54:45 +12:00
|
|
|
import ArtistGrid from '../../components/ArtistGrid'
|
|
|
|
|
import AlbumGrid from '../../components/AlbumGrid'
|
2017-05-12 05:22:57 +12:00
|
|
|
import TrackList from '../../components/TrackList'
|
|
|
|
|
import Parallax from '../../components/Parallax'
|
2017-05-15 08:57:38 +12:00
|
|
|
import AddSeedField from '../../components/AddSeedField'
|
2017-10-30 16:59:19 +13:00
|
|
|
import URILink from '../../components/URILink'
|
|
|
|
|
|
2016-11-19 13:06:08 +13:00
|
|
|
import * as helpers from '../../helpers'
|
2016-10-26 16:55:47 +13:00
|
|
|
import * as spotifyActions from '../../services/spotify/actions'
|
|
|
|
|
|
|
|
|
|
class Discover extends React.Component{
|
|
|
|
|
|
2017-10-10 13:05:28 +13:00
|
|
|
constructor(props){
|
2017-05-12 05:22:57 +12:00
|
|
|
super(props)
|
|
|
|
|
|
|
|
|
|
this._autocomplete_timer = false
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2017-05-14 07:16:11 +12:00
|
|
|
seeds: [
|
2017-06-02 09:31:05 +12:00
|
|
|
'spotify:genre:chill'
|
2017-05-14 07:16:11 +12:00
|
|
|
],
|
|
|
|
|
add_seed: '',
|
2017-05-16 12:05:20 +12:00
|
|
|
adding_seed: false
|
2017-05-12 05:22:57 +12:00
|
|
|
}
|
2016-10-26 16:55:47 +13:00
|
|
|
}
|
|
|
|
|
|
2017-01-11 13:52:55 +13:00
|
|
|
componentDidMount(){
|
2017-06-02 09:31:05 +12:00
|
|
|
|
|
|
|
|
// We have seeds provided in the URL
|
|
|
|
|
if (this.props.params.seeds){
|
2017-06-02 10:38:12 +12:00
|
|
|
this.handleURLSeeds(this.props.params.seeds)
|
2017-06-02 09:31:05 +12:00
|
|
|
|
|
|
|
|
// BAU
|
2017-05-14 07:16:11 +12:00
|
|
|
} else {
|
2017-08-22 08:09:35 +12:00
|
|
|
this.props.spotifyActions.getFavorites()
|
2017-05-12 05:22:57 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps, newState){
|
2017-05-16 08:54:45 +12:00
|
|
|
|
2017-06-02 10:38:12 +12:00
|
|
|
// New seeds via URL
|
|
|
|
|
if (newProps.params.seeds != this.props.params.seeds){
|
|
|
|
|
this.handleURLSeeds(newProps.params.seeds)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 08:54:45 +12:00
|
|
|
// When we've loaded favorite_artists.
|
|
|
|
|
// This indirectly listens for when the action has
|
|
|
|
|
// loaded new data.
|
|
|
|
|
if (this.props.favorite_artists.length <= 0 && newProps.favorite_artists.length > 0){
|
2017-05-12 05:22:57 +12:00
|
|
|
var initial_seeds = newProps.favorite_artists.sort(() => .5 - Math.random())
|
|
|
|
|
initial_seeds = initial_seeds.slice(0,2)
|
|
|
|
|
|
|
|
|
|
this.setState({seeds: initial_seeds})
|
|
|
|
|
this.getRecommendations(initial_seeds)
|
|
|
|
|
}
|
2017-01-11 13:52:55 +13:00
|
|
|
}
|
|
|
|
|
|
2017-06-02 10:38:12 +12:00
|
|
|
handleURLSeeds(seeds_string = this.props.params.seeds){
|
2017-06-15 08:41:55 +12:00
|
|
|
|
|
|
|
|
// Rejoin if we've had to uri-encode these as strings
|
|
|
|
|
// We'd need to do this if our URL has been encoded so the whole URL can become
|
|
|
|
|
// it's own URI (eg iris:discover:spotify_artist_1234) where we can't use ":"
|
|
|
|
|
var seeds = seeds_string.split('_').join(':').split(',')
|
2017-06-02 10:38:12 +12:00
|
|
|
|
|
|
|
|
for (var i = 0; i < seeds.length; i++){
|
|
|
|
|
switch (helpers.uriType(seeds[i])){
|
|
|
|
|
|
|
|
|
|
case 'artist':
|
|
|
|
|
this.props.spotifyActions.getArtist(seeds[i])
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case 'track':
|
|
|
|
|
this.props.spotifyActions.getTrack(seeds[i])
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setState({seeds: seeds})
|
|
|
|
|
this.getRecommendations(seeds)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-12 05:22:57 +12:00
|
|
|
getRecommendations(seeds = this.state.seeds){
|
2017-05-16 12:05:20 +12:00
|
|
|
if (seeds.length > 0){
|
2017-06-02 09:31:05 +12:00
|
|
|
this.props.spotifyActions.getRecommendations(seeds, 50)
|
2017-05-16 08:54:45 +12:00
|
|
|
}
|
2017-05-12 05:22:57 +12:00
|
|
|
}
|
2017-01-11 13:52:55 +13:00
|
|
|
|
2017-05-12 05:22:57 +12:00
|
|
|
removeSeed(index){
|
|
|
|
|
var seeds = this.state.seeds
|
|
|
|
|
seeds.splice(index,1)
|
|
|
|
|
this.setState({seeds: seeds})
|
|
|
|
|
this.getRecommendations(seeds)
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 09:31:05 +12:00
|
|
|
handleSelect(e,uri){
|
2017-05-12 05:22:57 +12:00
|
|
|
var seeds = this.state.seeds
|
2017-06-02 09:31:05 +12:00
|
|
|
seeds.push(uri)
|
2017-05-12 05:22:57 +12:00
|
|
|
this.setState({seeds: seeds})
|
|
|
|
|
this.getRecommendations(seeds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderSeeds(){
|
2017-06-02 09:31:05 +12:00
|
|
|
var seeds_objects = []
|
|
|
|
|
|
|
|
|
|
if (this.state.seeds.length > 0){
|
|
|
|
|
for (var i = 0; i < this.state.seeds.length; i++){
|
|
|
|
|
var uri = this.state.seeds[i]
|
|
|
|
|
|
|
|
|
|
switch (helpers.uriType(uri)){
|
|
|
|
|
|
|
|
|
|
case 'track':
|
|
|
|
|
if (typeof(this.props.tracks[uri]) !== 'undefined'){
|
|
|
|
|
seeds_objects.push(this.props.tracks[uri])
|
|
|
|
|
} else {
|
|
|
|
|
seeds_objects.push({
|
|
|
|
|
name: 'Loading...',
|
|
|
|
|
uri: uri
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case 'artist':
|
|
|
|
|
if (typeof(this.props.artists[uri]) !== 'undefined'){
|
|
|
|
|
seeds_objects.push(this.props.artists[uri])
|
|
|
|
|
} else {
|
|
|
|
|
seeds_objects.push({
|
|
|
|
|
name: 'Loading...',
|
|
|
|
|
uri: uri
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case 'genre':
|
|
|
|
|
var name = helpers.getFromUri('genreid',uri)
|
|
|
|
|
seeds_objects.push({
|
|
|
|
|
name: (name.charAt(0).toUpperCase() + name.slice(1)).replace('-',' '),
|
|
|
|
|
uri: uri
|
|
|
|
|
})
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 13:52:55 +13:00
|
|
|
return (
|
2017-05-12 05:22:57 +12:00
|
|
|
<div className="seeds">
|
2017-01-11 13:52:55 +13:00
|
|
|
{
|
2017-06-02 09:31:05 +12:00
|
|
|
seeds_objects.map((seed,index) => {
|
2017-05-12 05:22:57 +12:00
|
|
|
var type = helpers.uriType(seed.uri)
|
|
|
|
|
return (
|
|
|
|
|
<span className="seed" key={seed.uri}>
|
|
|
|
|
{seed.name}
|
2017-10-30 16:59:19 +13:00
|
|
|
<URILink className="type" type={type} uri={seed.uri}>({type})</URILink>
|
2017-09-21 08:24:33 +12:00
|
|
|
<FontAwesome name="close" className="remove" onClick={() => this.removeSeed(index)} />
|
2017-05-12 05:22:57 +12:00
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
})
|
2017-01-11 13:52:55 +13:00
|
|
|
}
|
2017-06-02 09:31:05 +12:00
|
|
|
<AddSeedField onSelect={(e,uri) => this.handleSelect(e,uri)} />
|
2017-05-12 05:22:57 +12:00
|
|
|
</div>
|
2017-01-11 13:52:55 +13:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-22 04:19:44 +12:00
|
|
|
renderResults(){
|
2017-05-22 08:17:40 +12:00
|
|
|
if (helpers.isLoading(this.props.load_queue,['spotify_recommendations'])){
|
2017-05-16 08:54:45 +12:00
|
|
|
return (
|
2017-08-07 21:34:33 +12:00
|
|
|
<div className="body-loader loading">
|
2017-05-16 08:54:45 +12:00
|
|
|
<div className="loader"></div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.props.recommendations || typeof(this.props.recommendations.albums_uris) === 'undefined' || typeof(this.props.recommendations.artists_uris) === 'undefined'){
|
2017-11-13 16:52:53 +13:00
|
|
|
return null;
|
2017-05-16 08:54:45 +12:00
|
|
|
}
|
|
|
|
|
|
2017-11-13 16:52:53 +13:00
|
|
|
var albums = [];
|
2017-05-16 08:54:45 +12:00
|
|
|
if (this.props.recommendations.albums_uris && this.props.albums){
|
|
|
|
|
for (var i = 0; i < this.props.recommendations.albums_uris.length; i++){
|
2017-11-13 16:52:53 +13:00
|
|
|
var uri = this.props.recommendations.albums_uris[i];
|
2017-05-16 08:54:45 +12:00
|
|
|
if (this.props.albums.hasOwnProperty(uri)){
|
2017-11-13 16:52:53 +13:00
|
|
|
albums.push(this.props.albums[uri]);
|
2017-05-16 08:54:45 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-13 16:52:53 +13:00
|
|
|
var artists = [];
|
2017-05-16 08:54:45 +12:00
|
|
|
if (this.props.recommendations.artists_uris && this.props.artists){
|
|
|
|
|
for (var i = 0; i < this.props.recommendations.artists_uris.length; i++){
|
2017-11-13 16:52:53 +13:00
|
|
|
var uri = this.props.recommendations.artists_uris[i];
|
2017-05-16 08:54:45 +12:00
|
|
|
if (this.props.artists.hasOwnProperty(uri)){
|
2017-11-13 16:52:53 +13:00
|
|
|
artists.push(this.props.artists[uri]);
|
2017-05-16 08:54:45 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-15 08:41:55 +12:00
|
|
|
|
2017-11-13 16:52:53 +13:00
|
|
|
var tracks = [];
|
|
|
|
|
if (this.props.recommendations.tracks_uris && this.props.tracks){
|
|
|
|
|
for (var i = 0; i < this.props.recommendations.tracks_uris.length; i++){
|
|
|
|
|
var uri = this.props.recommendations.tracks_uris[i];
|
|
|
|
|
if (this.props.tracks.hasOwnProperty(uri)){
|
|
|
|
|
tracks.push(this.props.tracks[uri]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var uri = 'iris:discover';
|
2017-06-15 08:41:55 +12:00
|
|
|
if (this.props.params.seeds){
|
2017-11-13 16:52:53 +13:00
|
|
|
uri += ':'+this.props.params.seeds.split(':').join('_');
|
2017-06-15 08:41:55 +12:00
|
|
|
}
|
2017-05-16 08:54:45 +12:00
|
|
|
|
|
|
|
|
return (
|
2017-06-19 07:38:23 +12:00
|
|
|
<div className="content-wrapper recommendations-results">
|
2017-07-05 07:49:53 +12:00
|
|
|
<section>
|
2017-05-16 08:54:45 +12:00
|
|
|
<h4>Artists</h4>
|
2017-09-03 16:10:32 +12:00
|
|
|
<ArtistGrid single_row artists={artists} />
|
2017-05-16 08:54:45 +12:00
|
|
|
</section>
|
2017-07-05 07:49:53 +12:00
|
|
|
<section>
|
2017-05-16 08:54:45 +12:00
|
|
|
<h4>Albums</h4>
|
2017-09-03 16:10:32 +12:00
|
|
|
<AlbumGrid single_row albums={albums} />
|
2017-05-16 08:54:45 +12:00
|
|
|
</section>
|
2017-07-05 07:49:53 +12:00
|
|
|
<section>
|
2017-06-19 07:38:23 +12:00
|
|
|
<h4>Tracks</h4>
|
2017-11-13 16:52:53 +13:00
|
|
|
<TrackList className="discover-track-list" uri={uri} tracks={tracks} />
|
2017-05-16 08:54:45 +12:00
|
|
|
</section>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 16:55:47 +13:00
|
|
|
render(){
|
2016-11-19 13:06:08 +13:00
|
|
|
return (
|
|
|
|
|
<div className="view discover-view">
|
2017-05-12 05:22:57 +12:00
|
|
|
<div className="intro">
|
2017-06-29 08:08:19 +12:00
|
|
|
|
2017-05-12 05:22:57 +12:00
|
|
|
<Parallax image="/iris/assets/backgrounds/discover.jpg" />
|
2017-06-29 08:08:19 +12:00
|
|
|
|
2017-05-12 05:22:57 +12:00
|
|
|
<div className="liner">
|
2017-06-26 08:32:45 +12:00
|
|
|
<h1>Explore new music</h1>
|
|
|
|
|
<h2 className="grey-text">
|
2017-08-22 08:09:35 +12:00
|
|
|
Add seeds below to build your sound
|
|
|
|
|
{!this.props.params.seeds ? ". Let's start with two of your favorite artists." : null}
|
2017-06-26 08:32:45 +12:00
|
|
|
</h2>
|
2017-05-12 05:22:57 +12:00
|
|
|
{this.renderSeeds()}
|
|
|
|
|
</div>
|
2017-06-29 08:08:19 +12:00
|
|
|
|
2017-05-12 05:22:57 +12:00
|
|
|
</div>
|
2017-06-29 08:08:19 +12:00
|
|
|
|
2017-05-16 08:54:45 +12:00
|
|
|
{this.renderResults()}
|
2017-06-29 08:08:19 +12:00
|
|
|
|
2016-11-19 13:06:08 +13:00
|
|
|
</div>
|
|
|
|
|
)
|
2016-10-26 16:55:47 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Export our component
|
|
|
|
|
*
|
|
|
|
|
* We also integrate our global store, using connect()
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2016-11-19 13:06:08 +13:00
|
|
|
return {
|
2017-11-13 16:52:53 +13:00
|
|
|
albums: state.core.albums,
|
|
|
|
|
artists: state.core.artists,
|
|
|
|
|
tracks: state.core.tracks,
|
2017-08-02 21:11:22 +12:00
|
|
|
genres: (state.core.genres ? state.core.genres : []),
|
2017-08-04 22:33:49 +12:00
|
|
|
authorized: state.spotify.authorization,
|
2017-05-14 07:16:11 +12:00
|
|
|
load_queue: state.ui.load_queue,
|
2017-05-12 05:22:57 +12:00
|
|
|
quick_search_results: (state.spotify.quick_search_results ? state.spotify.quick_search_results : {artists: [], tracks: []}),
|
2017-05-16 08:54:45 +12:00
|
|
|
recommendations: (state.spotify.recommendations ? state.spotify.recommendations : {}),
|
2017-05-12 05:22:57 +12:00
|
|
|
favorite_artists: (state.spotify.favorite_artists ? state.spotify.favorite_artists : []),
|
|
|
|
|
favorite_tracks: (state.spotify.favorite_tracks ? state.spotify.favorite_tracks : [])
|
2016-11-19 13:06:08 +13:00
|
|
|
}
|
2016-10-26 16:55:47 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
|
return {
|
|
|
|
|
spotifyActions: bindActionCreators(spotifyActions, dispatch)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(Discover)
|