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
2017-07-17 07:16:33 +12:00
import Header from '../components/Header'
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'
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 {
2017-10-10 13:05:28 +13:00
constructor ( props ) {
2016-10-03 14:44:27 +13:00
super ( props ) ;
}
componentDidMount ( ) {
2017-10-01 09:38:01 +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 ] }
2017-10-10 13:05:28 +13:00
this . props . uiActions . showContextMenu ( e , data , 'album' , 'click' )
2017-01-27 09:55:19 +13:00
}
2017-10-10 13:05:28 +13:00
componentWillReceiveProps ( nextProps ) {
2016-11-09 13:18:03 +13:00
// if our URI has changed, fetch new album
2017-10-10 13:05:28 +13:00
if ( nextProps . params . uri != this . props . params . uri ) {
this . loadAlbum ( nextProps )
2016-11-09 13:18:03 +13:00
// if mopidy has just connected AND we're a local album, go get
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 . loadAlbum ( nextProps )
2016-11-01 13:19:51 +13:00
}
}
}
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 )
}
2017-10-10 13:05:28 +13:00
loadAlbum ( props = this . props ) {
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' )
2017-10-10 13:32:04 +13:00
} else {
2017-10-10 13:05:28 +13:00
this . props . spotifyActions . getAlbum ( props . params . uri ) ;
2017-01-15 11:40:08 +13:00
}
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 {
2017-10-10 13:05:28 +13:00
this . props . mopidyActions . getAlbum ( props . params . uri ) ;
2017-01-15 21:06:24 +13:00
}
}
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 ( ) {
2017-10-10 13:05:28 +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 ( ) {
2017-08-15 16:36:50 +12:00
var library = helpers . uriSource ( this . props . params . uri ) + '_library_albums'
return ( this . props [ library ] && this . props [ library ] . indexOf ( this . props . params . uri ) > - 1 )
2017-03-09 21:37:16 +13:00
}
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 (
2017-08-07 21:34:33 +12:00
< div className = "body-loader loading" >
2017-05-22 04:19:44 +12:00
< 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 (
2017-06-18 05:47:36 +12:00
< div className = "view album-view content-wrapper" >
2017-06-20 08:40:34 +12:00
< div className = "thumbnail-wrapper" >
2017-10-26 20:51:16 +13:00
< Thumbnail size = "large" canZoom images = { this . props . album . images } / >
2017-06-20 08:40:34 +12:00
< / d i v >
2017-05-22 04:19:44 +12:00
< div className = "title" >
2016-11-18 08:29:13 +13:00
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" >
2017-10-10 13:05:28 +13:00
{ ! this . props . slim _mode ? < li className = "has-tooltip" > < FontAwesome name = { helpers . sourceIcon ( this . props . params . uri ) } / > < span className = "tooltip" > { helpers . uriSource ( this . props . params . uri ) } { this . props . album . album _type ? this . props . album . album _type : 'album' } < / s p a n > < / l i > : n u l l }
2017-08-23 07:39:33 +12:00
{ ! this . props . slim _mode && artists . length > 0 ? < li > < ArtistSentence artists = { artists } / > < / l i > : n u l l }
2017-05-22 04:19:44 +12:00
{ 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 }
2017-10-21 21:58:53 +13:00
< 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 }
2017-08-16 18:03:07 +12:00
< LazyLoadListener loading = { this . props . album . tracks _more } loadMore = { ( ) => this . loadMore ( ) } / >
2017-05-22 04:19:44 +12:00
< / 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 ) => {
2017-10-26 20:36:27 +13:00
var uri = ownProps . params . uri ;
2016-11-09 13:18:03 +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-08-02 10:53:30 +12:00
artists : state . core . artists ,
2017-10-26 20:36:27 +13:00
album : ( state . core . albums && state . core . albums [ uri ] !== undefined ? state . core . albums [ uri ] : false ) ,
2017-08-02 10:53:30 +12:00
albums : state . core . albums ,
2017-08-15 16:36:50 +12:00
spotify _library _albums : state . spotify . library _albums ,
local _library _albums : state . mopidy . library _albums ,
2017-08-04 22:33:49 +12:00
spotify _authorized : state . spotify . authorization ,
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 )