import React, { PropTypes } from 'react' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' import { Link } from 'react-router' import FontAwesome from 'react-fontawesome' import ReactGA from 'react-ga' import TrackList from '../components/TrackList' import Thumbnail from '../components/Thumbnail' import Dater from '../components/Dater' import ConfirmationButton from '../components/ConfirmationButton' import LazyLoadListener from '../components/LazyLoadListener' import FollowButton from '../components/FollowButton' import SidebarToggleButton from '../components/SidebarToggleButton' 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(){ this.loadPlaylist(); } componentWillReceiveProps( nextProps ){ if( nextProps.params.uri != this.props.params.uri ){ this.loadPlaylist( nextProps ) }else if( !this.props.mopidy_connected && nextProps.mopidy_connected ){ if( helpers.uriSource( this.props.params.uri ) == 'm3u' ){ this.loadPlaylist( nextProps ) } } } 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 } } loadMore(){ this.props.spotifyActions.getURL( this.props.playlist.tracks_more, 'PLAYLIST_LOADED_MORE_TRACKS', this.props.playlist.uri ); } play(){ ReactGA.event({ category: 'Playlist', action: 'Play', label: this.props.playlist.uri }) this.props.mopidyActions.playURIs([this.props.playlist.uri]) } follow(){ ReactGA.event({ category: 'Playlist', action: 'Follow', label: this.props.playlist.uri }) this.props.spotifyActions.toggleFollowingPlaylist( this.props.playlist.uri, 'PUT' ) } // TODO: Once unfollowing occurs, remove playlist from global playlists list unfollow(){ ReactGA.event({ category: 'Playlist', action: 'Unfollow', label: this.props.playlist.uri }) this.props.spotifyActions.toggleFollowingPlaylist( this.props.playlist.uri, 'DELETE' ) } // TODO: Once deletion occurs, remove playlist from global playlists list delete(){ ReactGA.event({ category: 'Playlist', action: 'Delete', label: this.props.playlist.uri }) this.props.mopidyActions.deletePlaylist( this.props.playlist.uri ) } reorderTracks( indexes, index ){ ReactGA.event({ category: 'Playlist', action: 'Reorder tracks', label: this.props.playlist.uri }) this.props.uiActions.reorderPlaylistTracks( this.props.playlist.uri, indexes, index, this.props.playlist.snapshot_id ) } removeTracks( tracks_indexes ){ ReactGA.event({ category: 'Playlist', action: 'Remove tracks', label: this.props.playlist.uri }) this.props.uiActions.removeTracksFromPlaylist( this.props.playlist.uri, tracks_indexes ) } renderActions(){ switch( helpers.uriSource( this.props.playlist.uri ) ){ case 'm3u': return (
this.delete() } />
) case 'spotify': if( this.props.playlist.can_edit ){ return (
this.unfollow() } />
) } return (
) default: return (
) } } 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' return (
{helpers.uriSource( this.props.params.uri )} playlist

{ this.props.playlist.name }

{ this.props.playlist.description ?
: null }
    { this.props.playlist.owner ?
  • {this.props.playlist.owner.id}
  • : null } { this.props.playlist.followers ?
  • {this.props.playlist.followers.total.toLocaleString()} followers
  • : null } { this.props.playlist.last_modified ?
  • : null }
  • { this.props.playlist.tracks_total ? this.props.playlist.tracks_total : '0'} tracks,  { this.props.playlist.tracks ? : '0 mins' }
{ this.renderActions() }
{ this.props.playlist.tracks ? this.removeTracks(tracks_indexes) } reorderTracks={ (indexes, index) => this.reorderTracks(indexes, index) } /> : null } this.loadMore() }/>
) } } /** * Export our component * * We also integrate our global store, using connect() **/ const mapStateToProps = (state, ownProps) => { var uri = ownProps.params.uri uri = uri.replace(' ','%20') 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 } } const mapDispatchToProps = (dispatch) => { return { uiActions: bindActionCreators(uiActions, dispatch), mopidyActions: bindActionCreators(mopidyActions, dispatch), spotifyActions: bindActionCreators(spotifyActions, dispatch) } } export default connect(mapStateToProps, mapDispatchToProps)(Playlist)