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

202 lines
7.6 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { Link } from 'react-router'
import FontAwesome from 'react-fontawesome'
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/ConfirmationButton'
import LazyLoadListener from '../components/LazyLoadListener'
2016-11-18 13:21:30 +13:00
import FollowButton from '../components/FollowButton'
import SidebarToggleButton from '../components/SidebarToggleButton'
2016-11-18 13:21:30 +13:00
import * as helpers from '../helpers'
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{
constructor(props) {
super(props);
}
componentDidMount(){
2016-11-01 11:49:04 +13:00
this.loadPlaylist();
}
componentWillReceiveProps( nextProps ){
if( nextProps.params.uri != this.props.params.uri ){
2016-11-01 11:49:04 +13:00
this.loadPlaylist( nextProps )
2016-11-09 21:13:40 +13:00
}else if( !this.props.mopidy_connected && nextProps.mopidy_connected ){
2016-11-01 11:49:04 +13:00
if( helpers.uriSource( this.props.params.uri ) == 'm3u' ){
this.loadPlaylist( nextProps )
}
}
}
2016-11-01 11:49:04 +13:00
loadPlaylist( props = this.props ){
switch( helpers.uriSource( props.params.uri ) ){
case 'spotify':
this.props.spotifyActions.getPlaylist( props.params.uri );
break
case 'm3u':
if( props.mopidy_connected ) this.props.mopidyActions.getPlaylist( props.params.uri );
break
2016-11-01 11:49:04 +13:00
}
}
loadMore(){
this.props.spotifyActions.getURL( this.props.playlist.tracks_more, 'PLAYLIST_LOADED_MORE_TRACKS', this.props.playlist.uri );
}
play(){
2017-01-04 15:24:13 +13:00
ReactGA.event({ category: 'Playlist', action: 'Play', label: this.props.playlist.uri })
this.props.mopidyActions.playURIs([this.props.playlist.uri])
}
follow(){
2017-01-04 15:24:13 +13:00
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(){
2017-01-04 15:24:13 +13:00
ReactGA.event({ category: 'Playlist', action: 'Unfollow', label: this.props.playlist.uri })
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-01-04 15:24:13 +13:00
ReactGA.event({ category: 'Playlist', action: 'Delete', label: this.props.playlist.uri })
this.props.mopidyActions.deletePlaylist( this.props.playlist.uri )
}
reorderTracks( indexes, index ){
2017-01-04 15:24:13 +13:00
ReactGA.event({ category: 'Playlist', action: 'Reorder tracks', label: this.props.playlist.uri })
2016-12-14 08:29:24 +13:00
this.props.uiActions.reorderPlaylistTracks( this.props.playlist.uri, indexes, index, this.props.playlist.snapshot_id )
}
removeTracks( tracks_indexes ){
2017-01-04 15:24:13 +13:00
ReactGA.event({ category: 'Playlist', action: 'Remove tracks', label: this.props.playlist.uri })
this.props.uiActions.removeTracksFromPlaylist( this.props.playlist.uri, tracks_indexes )
}
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">
<button className="rounded primary" onClick={ e => this.play() }>Play</button>
2017-02-07 20:07:04 +13:00
<button className="rounded outline" onClick={ e => this.props.uiActions.openModal('edit_playlist', { uri: this.props.playlist.uri, name: this.props.playlist.name }) }>Edit</button>
<ConfirmationButton className="rounded destructive" content="Delete" confirmingContent="Are you sure?" onConfirm={ e => this.delete() } />
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':
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">
<button className="rounded primary" onClick={ e => this.play() }>Play</button>
2017-02-07 20:07:04 +13:00
<button className="outline rounded" onClick={ e => this.props.uiActions.openModal('edit_playlist', { uri: this.props.playlist.uri, name: this.props.playlist.name, is_public: this.props.playlist.public }) }>Edit</button>
<ConfirmationButton className="rounded destructive" content="Delete" confirmingContent="Are you sure?" onConfirm={ e => this.unfollow() } />
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">
<button className="rounded primary" onClick={ e => this.play() }>Play</button>
<FollowButton className="rounded outline" uri={this.props.playlist.uri} addText="Add to library" removeText="Remove from library" is_following={this.props.playlist.is_following} />
</div>
)
2017-02-08 11:12:53 +13:00
default:
return (
<div className="actions">
<button className="rounded primary" onClick={ e => this.play() }>Play</button>
</div>
)
}
}
2016-11-01 13:19:51 +13:00
render(){
if( !this.props.playlist || !this.props.playlist.name ) return null
var scheme = helpers.uriSource( this.props.playlist.uri )
var context = 'playlist-track'
if (this.props.playlist.can_edit) context = 'editable-playlist-track'
2016-11-01 13:19:51 +13:00
return (
<div className="view playlist-view">
<SidebarToggleButton />
2017-02-07 20:07:04 +13:00
<Thumbnail size="large" canZoom images={ this.props.playlist.images } />
2017-02-07 20:07:04 +13:00
<div className="title">
<div className="source grey-text">
<FontAwesome name={helpers.sourceIcon( this.props.params.uri )} /> {helpers.uriSource( this.props.params.uri )} playlist
</div>
2017-02-07 20:07:04 +13:00
<h1>{ this.props.playlist.name }</h1>
{ this.props.playlist.description ? <div className="description grey-text" dangerouslySetInnerHTML={{__html: this.props.playlist.description}}></div> : null }
<ul className="details">
2017-02-07 20:07:04 +13:00
{ this.props.playlist.owner ? <li><Link to={'/user/'+this.props.playlist.owner.uri}>{this.props.playlist.owner.id}</Link></li> : null }
{ this.props.playlist.followers ? <li>{this.props.playlist.followers.total.toLocaleString()} followers</li> : null }
{ this.props.playlist.last_modified ? <li><Dater type="ago" data={this.props.playlist.last_modified} /></li> : null }
2016-11-28 07:27:30 +13:00
<li>
2017-02-07 20:07:04 +13:00
{ this.props.playlist.tracks_total ? this.props.playlist.tracks_total : '0'} tracks,&nbsp;
{ this.props.playlist.tracks ? <Dater type="total-time" data={this.props.playlist.tracks} /> : '0 mins' }
2016-11-28 07:27:30 +13:00
</li>
</ul>
</div>
2016-11-07 21:21:15 +13:00
2017-02-08 11:12:53 +13:00
{ this.renderActions() }
2017-02-07 20:07:04 +13:00
<section className="list-wrapper">
{ this.props.playlist.tracks ? <TrackList tracklist_uri={this.props.params.uri} context={context} tracks={this.props.playlist.tracks} removeTracks={ tracks_indexes => this.removeTracks(tracks_indexes) } reorderTracks={ (indexes, index) => this.reorderTracks(indexes, index) } /> : null }
<LazyLoadListener enabled={this.props.playlist.tracks_more} loadMore={ () => this.loadMore() }/>
</section>
2016-11-01 13:19:51 +13:00
</div>
)
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
var uri = ownProps.params.uri
uri = uri.replace(' ','%20')
2016-11-09 21:13:40 +13:00
return {
playlist: (state.ui.playlists && typeof(state.ui.playlists[uri]) !== 'undefined' ? state.ui.playlists[uri] : false ),
mopidy_connected: state.mopidy.connected,
spotify_authorized: state.spotify.authorized,
spotify_userid: state.spotify.me.id
2016-11-09 21:13:40 +13:00
}
}
const mapDispatchToProps = (dispatch) => {
return {
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Playlist)