Files
Iris/src/js/views/Playlist.js

270 lines
9.4 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
2018-09-17 16:21:02 +12:00
import { Link, hashHistory } from 'react-router'
2017-01-04 15:24:13 +13:00
import ReactGA from 'react-ga'
import TrackList from '../components/TrackList'
2016-10-31 16:49:42 +13:00
import Thumbnail from '../components/Thumbnail'
import Dater from '../components/Dater'
import ConfirmationButton from '../components/Fields/ConfirmationButton'
import LazyLoadListener from '../components/LazyLoadListener'
import FollowButton from '../components/Fields/FollowButton'
import Header from '../components/Header'
import ContextMenuTrigger from '../components/ContextMenuTrigger'
2017-11-10 15:56:01 +13:00
import URILink from '../components/URILink'
2018-05-21 06:01:53 +12:00
import Icon from '../components/Icon'
2016-11-18 13:21:30 +13:00
import * as helpers from '../helpers'
2017-08-02 21:11:22 +12:00
import * as coreActions from '../services/core/actions'
import * as uiActions from '../services/ui/actions'
import * as mopidyActions from '../services/mopidy/actions'
import * as spotifyActions from '../services/spotify/actions'
class Playlist extends React.Component{
2017-10-10 13:05:28 +13:00
constructor(props){
super(props);
}
2018-09-17 16:21:02 +12:00
componentWillMount(){
var uri = this.props.params.uri;
// Spotify upgraded their playlists URI to remove user component (Sept 2018)
// We accept the old format, and redirect to the new one
if (uri.includes("spotify:user:")){
uri = uri.replace(/spotify:user:([^:]*?):/i, "spotify:");
hashHistory.push(global.baseURL+'playlist/'+encodeURIComponent(uri));
}
}
componentDidMount(){
this.setWindowTitle();
this.props.coreActions.loadPlaylist(this.props.params.uri);
}
2017-10-10 13:05:28 +13:00
componentWillReceiveProps(nextProps){
if (nextProps.params.uri != this.props.params.uri){
this.props.coreActions.loadPlaylist(nextProps.params.uri);
} else if (!this.props.mopidy_connected && nextProps.mopidy_connected){
if (helpers.uriSource(this.props.params.uri) != 'spotify'){
this.props.coreActions.loadPlaylist(nextProps.params.uri);
2016-11-01 11:49:04 +13:00
}
}
if (!this.props.playlist && nextProps.playlist){
this.setWindowTitle(nextProps.playlist);
}
if (this.props.params.uri !== nextProps.params.uri && nextProps.playlist){
this.setWindowTitle(nextProps.playlist);
}
}
setWindowTitle(playlist = this.props.playlist){
if (playlist){
this.props.uiActions.setWindowTitle(playlist.name+" (playlist)");
} else{
this.props.uiActions.setWindowTitle("Playlist");
}
}
loadMore(){
this.props.spotifyActions.getMore(
this.props.playlist.tracks_more,
{
parent_type: 'playlist',
parent_key: this.props.playlist.uri,
records_type: 'track'
}
);
}
handleContextMenu(e){
var data = {
e: e,
context: (this.props.playlist.can_edit ? 'editable-playlist' : 'playlist'),
items: [this.props.playlist],
uris: [this.props.params.uri]
}
this.props.uiActions.showContextMenu(data)
}
play(){
this.props.mopidyActions.playPlaylist(this.props.playlist.uri)
}
follow(){
if (this.props.allow_reporting){
ReactGA.event({ category: 'Playlist', action: 'Follow', label: this.props.playlist.uri });
}
this.props.spotifyActions.toggleFollowingPlaylist(this.props.playlist.uri, 'PUT')
}
2016-11-14 21:02:46 +13:00
// TODO: Once unfollowing occurs, remove playlist from global playlists list
unfollow(){
if (this.props.allow_reporting){
ReactGA.event({ category: 'Playlist', action: 'Unfollow', label: this.props.playlist.uri });
}
2017-10-10 13:05:28 +13:00
this.props.spotifyActions.toggleFollowingPlaylist(this.props.playlist.uri, 'DELETE' )
}
2016-11-14 21:02:46 +13:00
// TODO: Once deletion occurs, remove playlist from global playlists list
delete(){
2017-11-10 15:56:01 +13:00
this.props.mopidyActions.deletePlaylist(this.props.playlist.uri);
}
2017-10-10 13:05:28 +13:00
reorderTracks(indexes, index){
2017-11-10 15:56:01 +13:00
this.props.coreActions.reorderPlaylistTracks(this.props.playlist.uri, indexes, index, this.props.playlist.snapshot_id);
}
2017-10-10 13:05:28 +13:00
removeTracks(tracks_indexes){
2017-11-10 15:56:01 +13:00
this.props.coreActions.removeTracksFromPlaylist(this.props.playlist.uri, tracks_indexes);
}
inLibrary(){
2017-11-10 15:56:01 +13:00
var library = helpers.uriSource(this.props.params.uri)+'_library_playlists';
return (this.props[library] && this.props[library].indexOf(this.props.params.uri) > -1);
}
2017-02-08 11:12:53 +13:00
renderActions(){
switch(helpers.uriSource(this.props.playlist.uri)){
case 'm3u':
2016-11-14 17:32:36 +13:00
return (
2017-02-08 11:12:53 +13:00
<div className="actions">
2017-02-12 21:11:50 +13:00
<button className="primary" onClick={ e => this.play() }>Play</button>
<Link className="button secondary" to={global.baseURL+'playlist/'+encodeURIComponent(this.props.params.uri)+'/edit'}>Edit</Link>
2017-10-21 21:58:53 +13:00
<ContextMenuTrigger onTrigger={e => this.handleContextMenu(e)} />
2017-02-08 11:12:53 +13:00
</div>
2016-11-14 17:32:36 +13:00
)
2016-11-14 11:20:02 +13:00
case 'spotify':
2017-10-10 13:05:28 +13:00
if (this.props.playlist.can_edit){
2016-11-14 17:32:36 +13:00
return (
2017-02-08 11:12:53 +13:00
<div className="actions">
2017-02-12 21:11:50 +13:00
<button className="primary" onClick={ e => this.play() }>Play</button>
2018-07-08 21:12:18 +12:00
<Link className="button secondary" to={global.baseURL+'playlist/'+encodeURIComponent(this.props.params.uri)+'/edit'}>Edit</Link>
2017-10-21 21:58:53 +13:00
<ContextMenuTrigger onTrigger={e => this.handleContextMenu(e)} />
2017-02-08 11:12:53 +13:00
</div>
2016-11-14 17:32:36 +13:00
)
}
2017-02-08 11:12:53 +13:00
return (
<div className="actions">
2017-02-12 21:11:50 +13:00
<button className="primary" onClick={ e => this.play() }>Play</button>
<FollowButton className="secondary" uri={this.props.params.uri} addText="Add to library" removeText="Remove from library" is_following={this.inLibrary()} />
2017-10-21 21:58:53 +13:00
<ContextMenuTrigger onTrigger={e => this.handleContextMenu(e)} />
2017-02-08 11:12:53 +13:00
</div>
)
2017-02-08 11:12:53 +13:00
default:
return (
<div className="actions">
2017-02-12 21:36:45 +13:00
<button className="primary" onClick={ e => this.play() }>Play</button>
2017-10-21 21:58:53 +13:00
<ContextMenuTrigger onTrigger={e => this.handleContextMenu(e)} />
2017-02-08 11:12:53 +13:00
</div>
)
}
}
2016-11-01 13:19:51 +13:00
render(){
var scheme = helpers.uriSource(this.props.params.uri);
var playlist_id = helpers.getFromUri('playlistid',this.props.params.uri);
if (!this.props.playlist){
if (helpers.isLoading(this.props.load_queue,['spotify_playlists/'+playlist_id+'?'])){
return (
<div className="body-loader loading">
<div className="loader"></div>
</div>
)
} else {
return null;
}
}
var playlist = helpers.collate(this.props.playlist, {tracks: this.props.tracks, users: this.props.users});
var context = 'playlist';
if (playlist.can_edit){
context = 'editable-playlist';
2017-05-22 04:19:44 +12:00
}
2016-11-01 13:19:51 +13:00
2018-09-18 16:59:10 +12:00
if (!playlist.tracks_uris || (playlist.tracks_uris && !playlist.tracks) || (playlist.tracks_uris.length !== playlist.tracks.length)){
2018-04-14 13:48:03 +12:00
var is_loading_tracks = true;
} else {
var is_loading_tracks = false;
}
return (
<div className="view playlist-view content-wrapper">
<div className="thumbnail-wrapper">
<Thumbnail size="large" canZoom images={playlist.images} />
</div>
<div className="title">
<h1>{playlist.name}</h1>
{playlist.description ? <h2 className="description mid_grey-text" dangerouslySetInnerHTML={{__html: playlist.description}}></h2> : null }
<ul className="details">
2018-09-21 11:58:54 +12:00
{!this.props.slim_mode ? <li className="tooltip"><Icon type="fontawesome" name={helpers.sourceIcon(playlist.uri)} /><span className="tooltip__content">{helpers.uriSource(playlist.uri)} playlist</span></li> : null }
{playlist.user_uri && !this.props.slim_mode ? <li><URILink type="user" uri={playlist.user_uri}>{playlist.user ? playlist.user.name : helpers.getFromUri('userid',playlist.user_uri)}</URILink></li> : null }
{playlist.followers !== undefined ? <li>{playlist.followers.toLocaleString()} followers</li> : null }
{playlist.last_modified_date ? <li>Edited <Dater type="ago" data={playlist.last_modified_date} /></li> : null }
<li>
{playlist.tracks_total ? playlist.tracks_total : (playlist.tracks ? playlist.tracks.length : '0')} tracks,&nbsp;
<Dater type="total-time" data={playlist.tracks} />
</li>
</ul>
</div>
2018-09-20 16:29:16 +12:00
{this.renderActions()}
<section className="list-wrapper">
<TrackList uri={playlist.uri} className="playlist-track-list" context={context} tracks={playlist.tracks} removeTracks={ tracks_indexes => this.removeTracks(tracks_indexes) } reorderTracks={ (indexes, index) => this.reorderTracks(indexes, index) } />
2018-09-03 08:24:49 +12:00
<LazyLoadListener
loadKey={playlist.tracks_more}
2018-09-20 16:29:16 +12:00
showLoader={is_loading_tracks || playlist.tracks_more}
2018-09-03 08:24:49 +12:00
loadMore={() => this.loadMore()}
/>
</section>
</div>
)
}
}
/**
* 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 {
allow_reporting: state.ui.allow_reporting,
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,
users: state.core.users,
2017-11-09 09:12:23 +13:00
tracks: state.core.tracks,
playlist: (state.core.playlists[uri] !== undefined ? state.core.playlists[uri] : false ),
spotify_library_playlists: state.spotify.library_playlists,
local_library_playlists: state.mopidy.library_playlists,
mopidy_connected: state.mopidy.connected,
spotify_authorized: state.spotify.authorization,
spotify_userid: (state.spotify.me && state.spotify.me.id ? state.spotify.me.id : null)
2016-11-09 21:13:40 +13:00
}
}
const mapDispatchToProps = (dispatch) => {
return {
2017-08-02 21:11:22 +12:00
coreActions: bindActionCreators(coreActions, dispatch),
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Playlist)