2016-10-03 14:44:27 +13:00
import React , { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
2016-11-02 16:51:00 +13:00
import FontAwesome from 'react-fontawesome'
2016-10-03 14:44:27 +13:00
2016-10-17 10:39:11 +13:00
import TrackList from '../components/TrackList'
2016-10-31 09:25:15 +13:00
import Thumbnail from '../components/Thumbnail'
import Parallax from '../components/Parallax'
import ArtistSentence from '../components/ArtistSentence'
2016-10-31 16:49:42 +13:00
import ArtistGrid from '../components/ArtistGrid'
2016-11-18 13:21:30 +13:00
import FollowButton from '../components/FollowButton'
2016-10-31 16:49:42 +13:00
import Dater from '../components/Dater'
2016-11-07 21:21:15 +13:00
import LazyLoadListener from '../components/LazyLoadListener'
2016-11-24 08:11:41 +13:00
import SidebarToggleButton from '../components/SidebarToggleButton'
2017-02-28 16:13:27 +13:00
import ContextMenuTrigger from '../components/ContextMenuTrigger'
2016-10-23 23:48:06 +13:00
2016-11-19 13:06:08 +13:00
import * as helpers from '../helpers'
2017-01-27 09:55:19 +13:00
import * as uiActions from '../services/ui/actions'
2016-10-19 14:09:34 +13:00
import * as mopidyActions from '../services/mopidy/actions'
2016-11-07 21:21:15 +13:00
import * as spotifyActions from '../services/spotify/actions'
2016-10-03 14:44:27 +13:00
class Album extends React . Component {
constructor ( props ) {
super ( props ) ;
}
componentDidMount ( ) {
2017-01-15 11:40:08 +13:00
this . loadAlbum ( )
2016-10-03 14:44:27 +13:00
}
2017-01-27 09:55:19 +13:00
handleContextMenu ( e ) {
e . preventDefault ( )
var data = { uris : [ this . props . params . uri ] }
this . props . uiActions . showContextMenu ( e , data , 'album' , 'click' )
}
2016-10-03 14:44:27 +13:00
componentWillReceiveProps ( nextProps ) {
2016-11-09 13:18:03 +13:00
// if our URI has changed, fetch new album
2016-10-16 12:37:37 +13:00
if ( nextProps . params . uri != this . props . params . uri ) {
2016-11-01 13:19:51 +13:00
this . loadAlbum ( nextProps )
2016-11-09 13:18:03 +13:00
// if mopidy has just connected AND we're a local album, go get
} else if ( ! this . props . mopidy _connected && nextProps . mopidy _connected ) {
2017-02-26 18:33:20 +13:00
if ( helpers . uriSource ( this . props . params . uri ) != 'spotify' ) {
2016-11-01 13:19:51 +13:00
this . loadAlbum ( nextProps )
}
}
}
2017-02-27 08:44:02 +13:00
handleContextMenu ( e ) {
var data = {
e : e ,
context : 'album' ,
2017-02-28 16:13:27 +13:00
items : [ this . props . album ] ,
2017-02-27 08:44:02 +13:00
uris : [ this . props . params . uri ]
}
this . props . uiActions . showContextMenu ( data )
}
2016-11-01 13:19:51 +13:00
loadAlbum ( props = this . props ) {
2016-11-09 13:18:03 +13:00
switch ( helpers . uriSource ( props . params . uri ) ) {
2016-11-07 21:21:15 +13:00
2016-11-09 13:18:03 +13:00
case 'spotify' :
2017-01-15 11:40:08 +13:00
if ( props . album && props . album . tracks && props . album . artists _uris ) {
console . info ( 'Loading album from index' )
} else {
this . props . spotifyActions . getAlbum ( props . params . uri ) ;
}
2016-11-09 13:18:03 +13:00
break ;
2016-11-07 21:21:15 +13:00
2017-02-23 22:06:43 +13:00
default :
2017-01-15 21:06:24 +13:00
if ( props . mopidy _connected ) {
if ( props . album && props . album . tracks ) {
console . info ( 'Loading album from index' )
} else {
this . props . mopidyActions . getAlbum ( props . params . uri ) ;
}
}
2016-11-09 13:18:03 +13:00
break ;
2016-10-03 14:44:27 +13:00
}
}
2016-11-07 21:21:15 +13:00
loadMore ( ) {
2016-11-09 20:34:37 +13:00
this . props . spotifyActions . getURL ( this . props . album . tracks _more , 'SPOTIFY_ALBUM_LOADED_MORE' ) ;
2016-11-07 21:21:15 +13:00
}
2016-11-18 08:29:13 +13:00
play ( ) {
2017-02-14 20:24:35 +13:00
this . props . mopidyActions . playURIs ( [ this . props . params . uri ] , this . props . params . uri )
2016-11-18 08:29:13 +13:00
}
2017-03-09 21:37:16 +13:00
inLibrary ( ) {
return ( this . props . library _albums && this . props . library _albums . indexOf ( this . props . params . uri ) > - 1 )
}
2016-11-07 21:21:15 +13:00
render ( ) {
2017-05-22 04:19:44 +12:00
if ( helpers . isLoading ( this . props . load _queue , [ 'spotify_albums/' + helpers . getFromUri ( 'albumid' , this . props . params . uri ) ] ) ) {
return (
< div className = "body-loader" >
< div className = "loader" > < / d i v >
< / d i v >
)
}
if ( ! this . props . album ) {
return null
}
var artists = [ ]
if ( this . props . album . artists _uris && this . props . artists ) {
for ( var i = 0 ; i < this . props . album . artists _uris . length ; i ++ ) {
var uri = this . props . album . artists _uris [ i ]
if ( this . props . artists . hasOwnProperty ( uri ) ) {
artists . push ( this . props . artists [ uri ] )
2017-01-14 10:03:45 +13:00
}
}
2017-05-22 04:19:44 +12:00
}
2017-02-26 18:33:20 +13:00
2017-05-22 04:19:44 +12:00
return (
< div className = "view album-view" >
< SidebarToggleButton / >
2017-02-07 20:07:04 +13:00
2017-05-22 04:19:44 +12:00
< Thumbnail size = "large" canZoom images = { this . props . album . images } / >
< div className = "title" >
< div className = "source grey-text" >
< FontAwesome name = { helpers . sourceIcon ( this . props . params . uri ) } / > & nbsp ;
{ helpers . uriSource ( this . props . params . uri ) } & nbsp ;
{ this . props . album . album _type ? this . props . album . album _type : 'album' }
2016-11-18 08:29:13 +13:00
< / d i v >
2017-05-22 04:19:44 +12:00
< h1 > { this . props . album . name } < / h 1 >
2017-02-07 20:07:04 +13:00
2017-05-22 04:19:44 +12:00
< ul className = "details" >
{ artists . length > 0 ? < li > < ArtistSentence artists = { artists } / > < / l i > : n u l l }
{ this . props . album . release _date ? < li > < Dater type = "date" data = { this . props . album . release _date } / > < / l i > : n u l l }
< li >
{ this . props . album . tracks _total ? this . props . album . tracks _total : '0' } tracks , & nbsp ;
{ this . props . album . tracks ? < Dater type = "total-time" data = { this . props . album . tracks } / > : '0 mins' }
< / l i >
< / u l >
2016-11-01 13:19:51 +13:00
< / d i v >
2017-02-23 22:06:43 +13:00
2017-05-22 04:19:44 +12:00
< div className = "actions" >
< button className = "primary" onClick = { e => this . play ( ) } > Play < / b u t t o n >
{ helpers . uriSource ( this . props . params . uri ) == 'spotify' ? < FollowButton className = "secondary" uri = { this . props . params . uri } addText = "Add to library" removeText = "Remove from library" is _following = { this . inLibrary ( ) } / > : null }
< ContextMenuTrigger onTrigger = { e => this . handleContextMenu ( e ) } / >
2016-10-03 14:44:27 +13:00
< / d i v >
2017-02-07 20:07:04 +13:00
2017-05-22 04:19:44 +12:00
< section className = "list-wrapper" >
{ this . props . album . tracks ? < TrackList className = "album-track-list" tracks = { this . props . album . tracks } uri = { this . props . params . uri } / > : null }
< LazyLoadListener enabled = { this . props . album . tracks _more } loadMore = { ( ) => this . loadMore ( ) } / >
< / s e c t i o n >
< / d i v >
)
2016-10-03 14:44:27 +13:00
}
}
/ * *
* Export our component
*
* We also integrate our global store , using connect ( )
* * /
const mapStateToProps = ( state , ownProps ) => {
2016-11-09 13:18:03 +13:00
return {
2017-05-22 04:19:44 +12:00
load _queue : state . ui . load _queue ,
2017-01-14 10:03:45 +13:00
artists : state . ui . artists ,
2017-01-15 11:40:08 +13:00
album : ( state . ui . albums && typeof ( state . ui . albums [ ownProps . params . uri ] ) !== 'undefined' ? state . ui . albums [ ownProps . params . uri ] : false ) ,
2017-01-14 10:03:45 +13:00
albums : state . ui . albums ,
2017-03-09 21:37:16 +13:00
library _albums : state . ui . library _albums ,
2016-11-18 10:02:05 +13:00
spotify _authorized : state . spotify . authorized ,
2016-11-09 13:18:03 +13:00
mopidy _connected : state . mopidy . connected
} ;
2016-10-03 14:44:27 +13:00
}
const mapDispatchToProps = ( dispatch ) => {
return {
2017-01-27 09:55:19 +13:00
uiActions : bindActionCreators ( uiActions , dispatch ) ,
2016-10-19 14:09:34 +13:00
mopidyActions : bindActionCreators ( mopidyActions , dispatch ) ,
2016-10-16 13:28:21 +13:00
spotifyActions : bindActionCreators ( spotifyActions , dispatch )
2016-10-03 14:44:27 +13:00
}
}
export default connect ( mapStateToProps , mapDispatchToProps ) ( Album )