Files
Iris/src/js/views/Album.js

205 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-10-03 14:44:27 +13:00
2019-01-15 16:57:50 +13:00
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
2016-10-03 14:44:27 +13:00
import ErrorMessage from '../components/ErrorMessage';
import Header from '../components/Header'
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'
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'
import ContextMenuTrigger from '../components/ContextMenuTrigger'
2018-05-21 06:01:53 +12:00
import Icon from '../components/Icon'
import * as helpers from '../helpers'
2018-08-13 19:58:36 +12:00
import * as coreActions from '../services/core/actions'
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(){
this.setWindowTitle();
2019-01-15 08:05:09 +13:00
this.props.coreActions.loadAlbum(this.props.uri);
2016-10-03 14:44:27 +13:00
}
handleContextMenu(e){
e.preventDefault()
2019-01-15 08:05:09 +13:00
var data = { uris: [this.props.uri] }
2017-10-10 13:05:28 +13:00
this.props.uiActions.showContextMenu(e, data, 'album', 'click' )
}
2017-10-10 13:05:28 +13:00
componentWillReceiveProps(nextProps){
// if our URI has changed, fetch new album
2019-01-15 08:05:09 +13:00
if (nextProps.uri != this.props.uri){
this.props.coreActions.loadAlbum(nextProps.uri);
// 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){
2019-01-15 08:05:09 +13:00
if (helpers.uriSource(nextProps.uri) != 'spotify'){
this.props.coreActions.loadAlbum(nextProps.uri);
2016-11-01 13:19:51 +13:00
}
}
if (!this.props.album && nextProps.album){
this.setWindowTitle(nextProps.album);
}
}
2018-08-13 19:58:36 +12:00
setWindowTitle(album = this.props.album){
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;
}
}
}
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',
items: [this.props.album],
2019-01-15 08:05:09 +13:00
uris: [this.props.uri]
2017-02-27 08:44:02 +13:00
}
this.props.uiActions.showContextMenu(data);
2017-02-27 08:44:02 +13:00
}
2016-11-07 21:21:15 +13:00
loadMore(){
this.props.spotifyActions.getMore(
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
}
play(){
2019-01-15 08:05:09 +13:00
this.props.mopidyActions.playURIs([this.props.uri], this.props.uri)
}
inLibrary(){
2019-01-15 08:05:09 +13:00
var library = helpers.uriSource(this.props.uri)+'_library_albums'
return (this.props[library] && this.props[library].indexOf(this.props.uri) > -1)
}
2016-11-07 21:21:15 +13:00
render(){
2018-08-13 19:58:36 +12:00
if (!this.props.album){
2019-01-15 08:05:09 +13:00
if (helpers.isLoading(this.props.load_queue,['spotify_albums/'+helpers.getFromUri('albumid',this.props.uri)])){
return (
<div className="body-loader loading">
<div className="loader"></div>
</div>
)
} else {
return (
<ErrorMessage type="not-found" title="Not found">
<p>Could not find album with URI "{this.props.uri}"</p>
</ErrorMessage>
);
}
2017-05-22 04:19:44 +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 (
<div className="view album-view content-wrapper preserve-3d">
<Parallax image={album.images ? album.images.huge : null} blur />
<div className="thumbnail-wrapper">
2018-08-29 21:13:54 +12:00
<Thumbnail size="large" canZoom images={album.images} />
</div>
2017-05-22 04:19:44 +12:00
<div className="title">
2018-08-29 21:13:54 +12:00
<h1>{album.name}</h1>
2017-02-07 20:07:04 +13:00
2017-05-22 04:19:44 +12:00
<ul className="details">
{!this.props.slim_mode ? <li><Icon type="fontawesome" name={helpers.sourceIcon(album.uri )} /></li> : null}
{album.artists && album.artists.length > 0 ? <li><ArtistSentence artists={album.artists} /></li> : null}
2018-08-29 21:13:54 +12:00
{album.release_date ? <li><Dater type="date" data={album.release_date} /></li> : null}
{!this.props.slim_mode ? <li>
2018-08-29 21:13:54 +12:00
{album.tracks ? <span>{album.tracks.length} tracks, <Dater type="total-time" data={album.tracks} /></span> : '0 tracks, 0 mins' }
</li> : null }
2017-05-22 04:19:44 +12:00
</ul>
2016-11-01 13:19:51 +13:00
</div>
2017-05-22 04:19:44 +12:00
<div className="actions">
<button className="button button--primary" onClick={e => this.play()}>Play</button>
2019-01-15 08:05:09 +13:00
{ helpers.uriSource(this.props.uri) == 'spotify' ? <FollowButton className="secondary" uri={this.props.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
</div>
2017-02-07 20:07:04 +13:00
2017-05-22 04:19:44 +12:00
<section className="list-wrapper">
<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
</section>
</div>
)
2016-10-03 14:44:27 +13:00
}
}
const mapStateToProps = (state, ownProps) => {
2019-01-15 16:57:50 +13:00
var uri = decodeURIComponent(ownProps.match.params.uri);
return {
2019-01-15 08:05:09 +13:00
uri: uri,
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,
artists: state.core.artists,
album: (state.core.albums && state.core.albums[uri] !== undefined ? state.core.albums[uri] : false ),
albums: state.core.albums,
spotify_library_albums: state.spotify.library_albums,
local_library_albums: state.mopidy.library_albums,
spotify_authorized: state.spotify.authorization,
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),
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)