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-12 05:22:57 +12:00
|
|
|
import SidebarToggleButton from '../../components/SidebarToggleButton'
|
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'
|
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{
|
|
|
|
|
|
|
|
|
|
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: [
|
|
|
|
|
{
|
|
|
|
|
uri: 'spotify:genre:chill',
|
|
|
|
|
name: 'Chill'
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
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-05-12 05:22:57 +12:00
|
|
|
if (this.props.authorized){
|
|
|
|
|
this.props.spotifyActions.getFavorites()
|
2017-05-14 07:16:11 +12:00
|
|
|
} else {
|
|
|
|
|
this.getRecommendations()
|
2017-05-12 05:22:57 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps, newState){
|
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-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-05-16 08:54:45 +12:00
|
|
|
var uris = helpers.asURIs(seeds)
|
|
|
|
|
this.props.spotifyActions.getRecommendations(uris, 50)
|
|
|
|
|
}
|
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-05-14 07:16:11 +12:00
|
|
|
handleSelect(e,item){
|
2017-05-12 05:22:57 +12:00
|
|
|
var seeds = this.state.seeds
|
|
|
|
|
seeds.push(item)
|
|
|
|
|
this.setState({seeds: seeds})
|
|
|
|
|
this.getRecommendations(seeds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderSeeds(){
|
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-05-12 05:22:57 +12:00
|
|
|
this.state.seeds.map((seed,index) => {
|
|
|
|
|
var type = helpers.uriType(seed.uri)
|
|
|
|
|
if (!type){
|
|
|
|
|
type = 'genre'
|
2017-01-11 13:52:55 +13:00
|
|
|
}
|
2017-05-12 05:22:57 +12:00
|
|
|
return (
|
|
|
|
|
<span className="seed" key={seed.uri}>
|
|
|
|
|
{seed.name}
|
|
|
|
|
<span className="type">({type})</span>
|
|
|
|
|
<FontAwesome name="close" className="remove" onClick={() => this.removeSeed(index)} />
|
|
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
})
|
2017-01-11 13:52:55 +13:00
|
|
|
}
|
2017-05-15 08:57:38 +12:00
|
|
|
<AddSeedField onSelect={(e,item) => this.handleSelect(e,item)} />
|
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 (
|
|
|
|
|
<div className="body-loader">
|
|
|
|
|
<div className="loader"></div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.props.recommendations || typeof(this.props.recommendations.albums_uris) === 'undefined' || typeof(this.props.recommendations.artists_uris) === 'undefined'){
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var albums = []
|
|
|
|
|
if (this.props.recommendations.albums_uris && this.props.albums){
|
|
|
|
|
for (var i = 0; i < this.props.recommendations.albums_uris.length; i++){
|
|
|
|
|
var uri = this.props.recommendations.albums_uris[i]
|
|
|
|
|
if (this.props.albums.hasOwnProperty(uri)){
|
|
|
|
|
albums.push(this.props.albums[uri])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var artists = []
|
|
|
|
|
if (this.props.recommendations.artists_uris && this.props.artists){
|
|
|
|
|
for (var i = 0; i < this.props.recommendations.artists_uris.length; i++){
|
|
|
|
|
var uri = this.props.recommendations.artists_uris[i]
|
|
|
|
|
if (this.props.artists.hasOwnProperty(uri)){
|
|
|
|
|
artists.push(this.props.artists[uri])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="recommendations-results">
|
|
|
|
|
<section className="grid-wrapper">
|
|
|
|
|
<h4>Artists</h4>
|
|
|
|
|
<ArtistGrid artists={artists} />
|
|
|
|
|
</section>
|
|
|
|
|
<section className="grid-wrapper">
|
|
|
|
|
<h4>Albums</h4>
|
|
|
|
|
<AlbumGrid albums={albums} />
|
|
|
|
|
</section>
|
|
|
|
|
<section className="list-wrapper">
|
|
|
|
|
<h4 className="left-padding">Tracks</h4>
|
|
|
|
|
{this.props.recommendations.tracks ? <TrackList className="discover-track-list" uri="iris:discover" tracks={this.props.recommendations.tracks} /> : null}
|
|
|
|
|
</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
|
|
|
<SidebarToggleButton />
|
|
|
|
|
<div className="intro">
|
|
|
|
|
<Parallax image="/iris/assets/backgrounds/discover.jpg" />
|
|
|
|
|
<div className="liner">
|
|
|
|
|
<h1>Discover new music</h1>
|
2017-05-14 07:16:11 +12:00
|
|
|
<h3>
|
|
|
|
|
Add seeds below to build your sound. Let's start with
|
|
|
|
|
{this.props.authorized ? " two of your favorite artists" : " the chill genre"}.
|
|
|
|
|
</h3>
|
2017-05-12 05:22:57 +12:00
|
|
|
{this.renderSeeds()}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2017-05-16 08:54:45 +12:00
|
|
|
{this.renderResults()}
|
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-05-16 08:54:45 +12:00
|
|
|
albums: state.ui.albums,
|
|
|
|
|
artists: state.ui.artists,
|
2017-01-11 13:52:55 +13:00
|
|
|
authorized: state.spotify.authorized,
|
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)
|