Files
Iris/src/js/views/Artist.js

298 lines
10 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { Link } from 'react-router'
2016-11-07 21:21:15 +13:00
import FontAwesome from 'react-fontawesome'
2016-11-01 08:57:28 +13:00
import LazyLoadListener from '../components/LazyLoadListener'
2016-10-24 21:26:01 +13:00
import Header from '../components/Header'
import TrackList from '../components/TrackList'
import AlbumGrid from '../components/AlbumGrid'
2016-10-25 17:01:37 +13:00
import Thumbnail from '../components/Thumbnail'
import Parallax from '../components/Parallax'
2016-11-23 13:17:07 +13:00
import ArtistGrid from '../components/ArtistGrid'
import RelatedArtists from '../components/RelatedArtists'
2016-11-18 13:21:30 +13:00
import FollowButton from '../components/FollowButton'
import ContextMenuTrigger from '../components/ContextMenuTrigger'
2016-11-18 13:21:30 +13:00
import * as helpers from '../helpers'
2016-12-20 16:58:20 +13:00
import * as uiActions from '../services/ui/actions'
2016-11-01 16:41:04 +13:00
import * as mopidyActions from '../services/mopidy/actions'
2016-12-20 16:58:20 +13:00
import * as pusherActions from '../services/pusher/actions'
2016-11-07 21:21:15 +13:00
import * as lastfmActions from '../services/lastfm/actions'
import * as spotifyActions from '../services/spotify/actions'
class Artist extends React.Component{
2017-10-10 13:05:28 +13:00
constructor(props){
2016-11-23 13:17:07 +13:00
super(props)
}
componentDidMount(){
2017-10-26 19:26:38 +13:00
this.loadArtist();
}
2017-10-10 13:05:28 +13:00
componentWillReceiveProps(nextProps){
if (nextProps.params.uri != this.props.params.uri){
this.loadArtist(nextProps);
2017-10-10 13:05:28 +13:00
}else if (!this.props.mopidy_connected && nextProps.mopidy_connected){
if (helpers.uriSource(this.props.params.uri ) != 'spotify'){
this.loadArtist(nextProps);
2016-11-01 16:41:04 +13:00
}
}
}
2017-02-27 08:44:02 +13:00
handleContextMenu(e){
var data = {
e: e,
context: 'artist',
items: [this.props.artist],
2017-02-27 08:44:02 +13:00
uris: [this.props.params.uri]
}
this.props.uiActions.showContextMenu(data);
2017-02-27 08:44:02 +13:00
}
2017-10-10 13:05:28 +13:00
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, true);
}
this.props.spotifyActions.following(props.params.uri);
break
default:
2017-01-15 21:06:24 +13:00
if (props.mopidy_connected){
if (props.artist && props.artist.images){
console.info('Loading local artist from index');
2017-01-15 21:06:24 +13:00
} else {
this.props.mopidyActions.getArtist(props.params.uri);
2017-01-15 21:06:24 +13:00
}
}
break
}
}
2016-11-01 08:57:28 +13:00
loadMore(){
2017-10-10 13:05:28 +13:00
this.props.spotifyActions.getURL(this.props.artist.albums_more, 'SPOTIFY_ARTIST_ALBUMS_LOADED', this.props.params.uri );
2016-11-01 08:57:28 +13:00
}
inLibrary(){
var library = helpers.uriSource(this.props.params.uri)+'_library_artists'
return (this.props[library] && this.props[library].indexOf(this.props.params.uri) > -1)
}
2016-11-23 13:17:07 +13:00
renderSubViewMenu(){
2016-11-01 16:41:04 +13:00
return (
2016-11-23 13:17:07 +13:00
<div className="sub-views">
2017-07-25 16:33:31 +12:00
<Link className="option" activeClassName="active" to={global.baseURL+'artist/'+this.props.params.uri}><h4>Overview</h4></Link>
{this.props.artist.related_artists_uris ? <Link className="option" activeClassName="active" to={global.baseURL+'artist/'+this.props.params.uri+'/related-artists'}><h4>Related artists</h4></Link> : null}
2017-07-25 16:33:31 +12:00
<Link className="option" activeClassName="active" to={global.baseURL+'artist/'+this.props.params.uri+'/about'}><h4>About</h4></Link>
2016-11-23 13:17:07 +13:00
</div>
)
}
2016-11-23 13:17:07 +13:00
renderBody(){
2017-05-22 04:19:44 +12:00
if (helpers.isLoading(this.props.load_queue,['spotify_artists/'+helpers.getFromUri('artistid',this.props.params.uri), 'lastfm_method=artist.getInfo'])){
return (
2017-08-07 21:34:33 +12:00
<div className="body-loader loading">
2017-05-22 04:19:44 +12:00
<div className="loader"></div>
</div>
)
}
2017-10-10 13:05:28 +13:00
var scheme = helpers.uriSource(this.props.params.uri );
2017-02-07 10:00:29 +13:00
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])
}
}
}
switch (this.props.params.sub_view){
case 'related-artists':
return (
<div className="body related-artists">
<section className="grid-wrapper no-top-padding">
<ArtistGrid artists={related_artists} />
</section>
</div>
)
case 'about':
return (
<div className="body about">
<div className="col w40 tiles artist-stats">
2017-07-24 17:57:44 +12:00
{this.props.artist.images ? <div className="tile thumbnail-wrapper"><Thumbnail size="huge" canZoom images={this.props.artist.images} /></div> : null}
{this.props.artist.images_additional ? <div className="tile thumbnail-wrapper"><Thumbnail size="huge" canZoom images={this.props.artist.images_additional} /></div> : null}
{this.props.artist.followers ? <div className="tile"><span className="content"><FontAwesome name="users" />{this.props.artist.followers.total.toLocaleString() } followers</span></div> : null}
{this.props.artist.popularity ? <div className="tile"><span className="content"><FontAwesome name="fire" />{this.props.artist.popularity }% popularity</span></div> : null}
{this.props.artist.listeners ? <div className="tile"><span className="content"><FontAwesome name="headphones" />{ this.props.artist.listeners.toLocaleString() } listeners</span></div> : null }
</div>
<div className="col w60 biography">
<section>
{ this.props.artist.bio ? <div className="biography-text"><p>{this.props.artist.bio.content}</p><br />
<div className="grey-text">Published: { this.props.artist.bio.published }</div>
<div className="grey-text">Origin: <a href={ this.props.artist.bio.links.link.href } target="_blank">{ this.props.artist.bio.links.link.href }</a></div></div> : null }
</section>
</div>
2017-02-10 22:22:33 +13:00
</div>
)
2016-11-01 16:41:04 +13:00
default:
2017-11-09 16:54:52 +13:00
var tracks = [];
if (this.props.artist.tracks_uris && this.props.tracks){
for (var i = 0; i < this.props.artist.tracks_uris.length; i++){
var uri = this.props.artist.tracks_uris[i]
if (this.props.tracks.hasOwnProperty(uri)){
tracks.push(this.props.tracks[uri])
}
}
}
return (
<div className="body overview">
<div className={related_artists.length > 0 ? "col w70" : "col w100"}>
2017-06-18 05:47:36 +12:00
<h4>Top tracks</h4>
<div className="list-wrapper">
2017-11-09 16:54:52 +13:00
<TrackList className="artist-track-list" uri={this.props.params.uri} tracks={tracks} />
</div>
</div>
2016-11-01 16:41:04 +13:00
<div className="col w5"></div>
2016-11-01 16:41:04 +13:00
2017-06-19 07:38:23 +12:00
{related_artists.length > 0 ? <div className="col w25 related-artists"><h4>Related artists</h4><div className="list-wrapper"><RelatedArtists artists={related_artists.slice(0,6)} /></div><Link to={global.baseURL+'artist/'+this.props.params.uri+'/related-artists'} className="button">All related artists</Link></div> : null}
2016-11-01 16:41:04 +13:00
<div className="cf"></div>
2017-06-18 05:47:36 +12:00
<h4>Albums</h4>
<section className="grid-wrapper no-top-padding">
<AlbumGrid albums={albums} />
2017-08-16 18:03:07 +12:00
<LazyLoadListener loading={this.props.artist.albums_more} loadMore={ () => this.loadMore() }/>
</section>
</div>
)
}
2016-11-23 13:17:07 +13:00
}
render(){
2017-05-22 04:19:44 +12:00
2017-10-10 13:05:28 +13:00
var scheme = helpers.uriSource(this.props.params.uri )
2016-11-23 13:17:07 +13:00
2017-10-10 13:05:28 +13:00
if (this.props.artist && this.props.artist.images){
var image = helpers.sizedImages(this.props.artist.images ).huge
} else {
var image = null
}
2016-11-23 18:19:51 +13:00
if (this.props.artist){
var is_spotify = (scheme == 'spotify')
if (is_spotify){
2017-09-10 20:15:24 +12:00
var uris_to_play = (this.props.artist.tracks ? helpers.arrayOf('uri', this.props.artist.tracks) : [])
} else {
var uris_to_play = this.props.artist.albums_uris
}
2017-02-27 07:31:58 +13:00
return (
<div className="view artist-view">
<div className="intro">
2016-11-23 13:17:07 +13:00
2017-07-24 06:03:09 +12:00
<Parallax image={image} />
2016-11-23 13:17:07 +13:00
<div className="liner">
<h1>{this.props.artist ? this.props.artist.name : null}</h1>
2017-02-27 07:31:58 +13:00
<div className="actions">
<button className="primary" onClick={e => this.props.mopidyActions.playURIs(uris_to_play, this.props.artist.uri)}>Play</button>
{is_spotify ? <FollowButton className="white" uri={this.props.params.uri} removeText="Remove from library" addText="Add to library" is_following={this.inLibrary()} /> : null}
2017-10-21 21:58:53 +13:00
<ContextMenuTrigger className="white" onTrigger={e => this.handleContextMenu(e)} />
2017-02-27 07:31:58 +13:00
</div>
{ this.renderSubViewMenu() }
</div>
2016-11-23 13:17:07 +13:00
</div>
2017-06-18 05:47:36 +12:00
<div className="content-wrapper">
{this.renderBody()}
</div>
</div>
);
} else {
return (
<div className="view artist-view">
<div className="intro">
<Parallax />
<div className="liner">
<h1>
<span className="placeholder"></span>
</h1>
<div className="actions">
<button className="placeholder">&nbsp;</button>
<button className="placeholder">&nbsp;</button>
</div>
{ this.renderSubViewMenu() }
</div>
</div>
2017-06-18 05:47:36 +12:00
<div className="content-wrapper">
{this.renderBody()}
</div>
</div>
);
}
2016-11-01 16:41:04 +13:00
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
var uri = ownProps.params.uri;
2016-11-09 21:13:40 +13:00
return {
2017-08-23 07:39:33 +12:00
slim_mode: state.ui.slim_mode,
2017-05-22 04:19:44 +12:00
load_queue: state.ui.load_queue,
2017-11-09 16:54:52 +13:00
artist: (state.core.artists[uri] !== undefined ? state.core.artists[uri] : false),
tracks: state.core.tracks,
artists: state.core.artists,
spotify_library_artists: state.spotify.library_artists,
local_library_artists: state.mopidy.library_artists,
albums: (state.core.albums ? state.core.albums : []),
spotify_authorized: state.spotify.authorization,
mopidy_connected: state.mopidy.connected
2016-11-09 21:13:40 +13:00
}
}
const mapDispatchToProps = (dispatch) => {
return {
2016-12-20 16:58:20 +13:00
uiActions: bindActionCreators(uiActions, dispatch),
2016-11-01 16:41:04 +13:00
mopidyActions: bindActionCreators(mopidyActions, dispatch),
2016-12-20 16:58:20 +13:00
pusherActions: bindActionCreators(pusherActions, dispatch),
2016-11-07 21:21:15 +13:00
lastfmActions: bindActionCreators(lastfmActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Artist)