2016-10-03 14:44:27 +13:00
import React , { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
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'
2018-04-19 13:34:16 +12:00
import FollowButton from '../components/Fields/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'
2018-05-21 06:01:53 +12:00
import Icon from '../components/Icon'
2016-10-23 23:48:06 +13:00
2016-11-19 13:06:08 +13:00
import * as helpers from '../helpers'
2018-08-13 19:58:36 +12:00
import * as coreActions from '../services/core/actions'
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 ( ) {
2018-06-29 14:26:06 +12:00
this . setWindowTitle ( ) ;
2018-08-13 19:58:36 +12:00
this . props . coreActions . loadAlbum ( this . props . params . uri ) ;
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 ) {
2018-08-13 19:58:36 +12:00
this . props . coreActions . loadAlbum ( nextProps . params . uri ) ;
2016-11-09 13:18:03 +13:00
// if mopidy has just connected AND we're a local album, go get
2018-08-13 19:58:36 +12:00
} else if ( ! this . props . mopidy _connected && nextProps . mopidy _connected ) {
if ( helpers . uriSource ( nextProps . params . uri ) != 'spotify' ) {
this . props . coreActions . loadAlbum ( nextProps . params . uri ) ;
2016-11-01 13:19:51 +13:00
}
}
2018-06-29 14:26:06 +12:00
if ( ! this . props . album && nextProps . album ) {
this . setWindowTitle ( nextProps . album ) ;
}
}
2018-08-13 19:58:36 +12:00
setWindowTitle ( album = this . props . album ) {
2018-06-29 14:26:06 +12:00
if ( album ) {
var artists = "" ;
2018-08-29 21:13:54 +12:00
if ( album . artists _uris && this . props . artists ) {
for ( var i = 0 ; i < album . artists _uris . length ; i ++ ) {
var uri = album . artists _uris [ i ]
if ( this . props . artists . hasOwnProperty ( uri ) ) {
if ( artists != "" ) {
artists += ", " ;
}
artists += this . props . artists [ uri ] . name ;
}
2018-06-29 14:26:06 +12:00
}
}
this . props . uiActions . setWindowTitle ( album . name + " by " + artists + " (album)" ) ;
} else {
this . props . uiActions . setWindowTitle ( "Album" ) ;
}
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 ]
}
2018-06-29 14:26:06 +12:00
this . props . uiActions . showContextMenu ( data ) ;
2017-02-27 08:44:02 +13:00
}
2016-11-07 21:21:15 +13:00
loadMore ( ) {
2017-11-12 09:05:34 +13:00
this . props . spotifyActions . getMore (
2017-11-09 21:04:45 +13:00
this . props . album . tracks _more ,
{
parent _type : 'album' ,
parent _key : this . props . album . uri ,
records _type : 'track'
}
) ;
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 ( ) {
2018-08-13 19:58:36 +12:00
if ( ! this . props . album ) {
2018-04-10 08:01:36 +12:00
if ( helpers . isLoading ( this . props . load _queue , [ 'spotify_albums/' + helpers . getFromUri ( 'albumid' , this . props . params . uri ) ] ) ) {
return (
< div className = "body-loader loading" >
< div className = "loader" > < / d i v >
< / d i v >
)
} else {
return null ;
}
2017-05-22 04:19:44 +12:00
}
2018-08-31 16:04:55 +12:00
var album = helpers . collate ( this . props . album , { tracks : this . props . tracks , artists : this . props . artists } ) ;
2017-11-08 21:13:32 +13:00
2018-09-18 16:59:10 +12:00
if ( ! album . tracks _uris || ( album . tracks _uris && ! album . tracks ) || ( album . tracks _uris . length !== album . tracks . length ) ) {
2018-04-14 13:48:03 +12:00
var is _loading _tracks = true ;
} else {
var is _loading _tracks = false ;
}
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" >
2018-08-29 21:13:54 +12:00
< Thumbnail size = "large" canZoom images = { 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
2018-08-29 21:13:54 +12:00
< h1 > { album . name } < / h 1 >
2017-02-07 20:07:04 +13:00
2017-05-22 04:19:44 +12:00
< ul className = "details" >
2018-09-21 11:58:54 +12:00
{ ! this . props . slim _mode ? < li className = "tooltip" > < Icon type = "fontawesome" name = { helpers . sourceIcon ( album . uri ) } / > < span className = "tooltip__content" > { helpers . uriSource ( this . props . params . uri ) } { album . type ? album . type : 'album' } < / s p a n > < / l i > : n u l l }
2018-08-31 16:04:55 +12:00
{ ! this . props . slim _mode && album . artists && album . artists . length > 0 ? < li > < ArtistSentence artists = { album . artists } / > < / l i > : n u l l }
2018-08-29 21:13:54 +12:00
{ album . release _date ? < li > < Dater type = "date" data = { album . release _date } / > < / l i > : n u l l }
2017-05-22 04:19:44 +12:00
< li >
2018-08-29 21:13:54 +12:00
{ album . tracks ? < span > { album . tracks . length } tracks , < Dater type = "total-time" data = { album . tracks } / > < / s p a n > : ' 0 t r a c k s , 0 m i n s ' }
2017-05-22 04:19:44 +12:00
< / 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" >
2018-09-05 21:29:46 +12:00
< TrackList className = "album-track-list" tracks = { album . tracks } uri = { album . uri } / >
2018-09-03 08:24:49 +12:00
< LazyLoadListener
loadKey = { album . tracks _more }
showLoader = { is _loading _tracks }
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
}
}
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-11-08 21:13:32 +13:00
tracks : state . core . tracks ,
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 {
2018-08-13 19:58:36 +12:00
coreActions : bindActionCreators ( coreActions , dispatch ) ,
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
}
}
2018-08-13 19:58:36 +12:00
export default connect ( mapStateToProps , mapDispatchToProps ) ( Album )