import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import FontAwesome from 'react-fontawesome'
import LazyLoadListener from '../components/LazyLoadListener'
import Header from '../components/Header'
import TrackList from '../components/TrackList'
import AlbumGrid from '../components/AlbumGrid'
import Thumbnail from '../components/Thumbnail'
import Parallax from '../components/Parallax'
import ArtistList from '../components/ArtistList'
import ArtistGrid from '../components/ArtistGrid'
import FollowButton from '../components/FollowButton'
import SidebarToggleButton from '../components/SidebarToggleButton'
import ContextMenuTrigger from '../components/ContextMenuTrigger'
import * as helpers from '../helpers'
import * as uiActions from '../services/ui/actions'
import * as mopidyActions from '../services/mopidy/actions'
import * as pusherActions from '../services/pusher/actions'
import * as lastfmActions from '../services/lastfm/actions'
import * as spotifyActions from '../services/spotify/actions'
class Artist extends React.Component{
constructor(props) {
super(props)
this.state = {
sub_view: 'overview'
}
}
componentDidMount(){
this.loadArtist();
}
componentWillReceiveProps( nextProps ){
if( nextProps.params.uri != this.props.params.uri ){
this.loadArtist( nextProps )
}else if( !this.props.mopidy_connected && nextProps.mopidy_connected ){
if( helpers.uriSource( this.props.params.uri ) != 'spotify' ){
this.loadArtist( nextProps )
}
}
}
componentWillUpdate( nextProps, nextState ){
if( nextState.sub_view != this.state.sub_view && nextState.sub_view == 'about' ){
if( this.props.artist && !this.props.artist.bio ){
this.props.lastfmActions.getArtist( this.props.params.uri, this.props.artist.name.replace('&','and') )
}
}
}
handleContextMenu(e){
var data = {
e: e,
context: 'artist',
items: [this.props.artist],
uris: [this.props.params.uri]
}
this.props.uiActions.showContextMenu(data)
}
loadArtist( props = this.props ){
switch( helpers.uriSource( props.params.uri ) ){
case 'spotify':
if (props.artist && props.artist.albums_uris && props.artist.related_artists_uris){
console.info('Loading spotify artist from index')
}else{
this.props.spotifyActions.getArtist( props.params.uri );
}
break
default:
if (props.mopidy_connected){
if (props.artist && props.artist.images){
console.info('Loading local artist from index')
} else {
this.props.mopidyActions.getArtist( props.params.uri );
}
}
break
}
// go back to overview
this.setState({ sub_view: 'overview' })
}
loadMore(){
this.props.spotifyActions.getURL( this.props.artist.albums_more, 'SPOTIFY_ARTIST_ALBUMS_LOADED', this.props.params.uri );
}
inLibrary(){
return (this.props.library_artists && this.props.library_artists.indexOf(this.props.params.uri) > -1)
}
renderSubViewMenu(){
return (
this.setState({ sub_view: 'overview' })}>
Overview
{this.props.artist.related_artists_uris ? this.setState({ sub_view: 'related_artists' })}>Related artists : null}
this.setState({ sub_view: 'about' })}>
About
)
}
renderBody(){
var scheme = helpers.uriSource( this.props.params.uri );
var related_artists = []
if (this.props.artist.related_artists_uris){
for (var i = 0; i < this.props.artist.related_artists_uris.length; i++){
var uri = this.props.artist.related_artists_uris[i]
if (this.props.artists.hasOwnProperty(uri)){
related_artists.push(this.props.artists[uri])
}
}
}
var albums = []
if (this.props.artist.albums_uris){
for (var i = 0; i < this.props.artist.albums_uris.length; i++){
var uri = this.props.artist.albums_uris[i]
if (this.props.albums.hasOwnProperty(uri)){
albums.push(this.props.albums[uri])
}
}
}
if( this.state.sub_view == 'related_artists' ){
return (
)
}else if( this.state.sub_view == 'about' ){
return (
{this.props.artist.followers ?
{this.props.artist.followers.total.toLocaleString() } followers
: null}
{this.props.artist.popularity ?
{this.props.artist.popularity }% popularity
: null}
{this.props.artist.listeners ?
{ this.props.artist.listeners.toLocaleString() } listeners
: null }
Biography
{ this.props.artist.bio ? {this.props.artist.bio.content}
Published: { this.props.artist.bio.published }
: null }
)
}
return (
0 ? "col w70" : "col w100"}>
Top tracks
{ this.props.artist.tracks ? : null }
{related_artists.length > 0 ?
: null}
Albums
)
}
render(){
var scheme = helpers.uriSource( this.props.params.uri );
if ( this.props.artist && this.props.artist.images ){
var image = helpers.sizedImages( this.props.artist.images ).huge
} else {
var image = null
}
if (this.props.artist){
var can_play_radio = (scheme == 'spotify')
var can_follow = (scheme == 'spotify')
return (
{this.props.artist ? this.props.artist.name : null}
{ can_play_radio ? this.props.pusherActions.startRadio([this.props.artist.uri])}>Start radio : null}
{ can_follow ? : null}
this.handleContextMenu(e)} />
{ this.renderSubViewMenu() }
{this.props.artist ? this.renderBody() : null}
);
} else {
return (
{ this.renderSubViewMenu() }
);
}
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
return {
artist: (state.ui.artists && typeof(state.ui.artists[ownProps.params.uri]) !== 'undefined' ? state.ui.artists[ownProps.params.uri] : false ),
artists: state.ui.artists,
library_artists: state.ui.library_artists,
albums: state.ui.albums,
spotify_authorized: state.spotify.authorized,
mopidy_connected: state.mopidy.connected
}
}
const mapDispatchToProps = (dispatch) => {
return {
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
pusherActions: bindActionCreators(pusherActions, dispatch),
lastfmActions: bindActionCreators(lastfmActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Artist)