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

164 lines
5.1 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import FontAwesome from 'react-fontawesome'
import * as helpers from '../helpers'
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'
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(){
2016-11-09 21:13:40 +13:00
if( !this.props.playlist.tracks_more ) return
this.props.spotifyActions.getURL( this.props.playlist.tracks_more, 'SPOTIFY_PLAYLIST_LOADED_MORE' );
}
play(){
this.props.mopidyActions.playTracks([this.props.params.uri])
}
follow(){
this.props.spotifyActions.toggleFollowingPlaylist( this.props.params.uri, 'PUT' )
}
unfollow(){
this.props.spotifyActions.toggleFollowingPlaylist( this.props.params.uri, 'DELETE' )
}
delete(){
alert('Delete me')
}
removeTracks( track_indexes ){
this.props.uiActions.removeTracksFromPlaylist( this.props.playlist.uri, track_indexes )
}
renderFollowOrDeleteButton(){
if( !this.props.spotify_authorized ) return null
switch( helpers.uriSource( this.props.params.uri ) ){
case 'm3u':
return <ConfirmationButton className="large tertiary" content="Delete" confirmingContent="Are you sure?" onConfirm={ e => this.delete() } />
break
case 'spotify':
if( this.props.playlist.owner && this.props.playlist.owner.id == this.props.spotify_userid ){
return <ConfirmationButton className="large tertiary" content="Delete" confirmingContent="Are you sure?" onConfirm={ e => this.unfollow() } />
}else if( this.props.playlist.following ){
return <button className="large tertiary" onClick={ e => this.unfollow() }>Unfollow</button>
}else{
return <button className="large tertiary" onClick={ e => this.follow() }>Follow</button>
}
break
}
}
2016-11-01 13:19:51 +13:00
render(){
if( !this.props.playlist || !this.props.playlist.name ) return null;
2016-11-09 21:13:40 +13:00
var scheme = helpers.uriSource( this.props.params.uri );
2016-11-01 13:19:51 +13:00
return (
<div className="view playlist-view">
<div className="intro">
2016-11-09 21:13:40 +13:00
<Thumbnail size="large" images={ this.props.playlist.images } />
<div className="actions">
<button className="large primary" onClick={ e => this.play() }>Play</button>
{ this.renderFollowOrDeleteButton() }
</div>
<ul className="details">
2016-11-09 21:13:40 +13:00
<li>{ this.props.playlist.tracks_total } tracks, <Dater type="total-time" data={this.props.playlist.tracks} /></li>
{ this.props.playlist.last_modified ? <li>Updated <Dater type="ago" data={this.props.playlist.last_modified} /> ago</li> : null }
{ scheme == 'spotify' ? <li><FontAwesome name="spotify" /> Spotify playlist</li> : null }
{ scheme == 'm3u' ? <li><FontAwesome name="folder" /> Local playlist</li> : null }
</ul>
</div>
2016-11-01 13:19:51 +13:00
<div className="main">
2016-11-07 21:21:15 +13:00
2016-11-01 13:19:51 +13:00
<div className="title">
2016-11-09 21:13:40 +13:00
<h1>{ this.props.playlist.name }</h1>
{ this.props.playlist.description ? <h3 className="grey-text" dangerouslySetInnerHTML={{__html: this.props.playlist.description}}></h3> : null }
2016-11-01 11:49:04 +13:00
</div>
2016-11-07 21:21:15 +13:00
<section className="list-wrapper">
{ this.props.playlist.tracks ? <TrackList context="editable-playlist" tracks={ this.props.playlist.tracks } removeTracks={ track_indexes => this.removeTracks(track_indexes) } /> : null }
2016-11-07 21:21:15 +13:00
<LazyLoadListener loadMore={ () => this.loadMore() }/>
</section>
2016-11-01 11:49:04 +13:00
</div>
2016-11-01 13:19:51 +13:00
</div>
)
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
2016-11-09 21:13:40 +13:00
return {
playlist: state.ui.playlist,
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)