Files
Iris/src/js/views/library/LibraryPlaylists.js

285 lines
8.0 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
2016-10-17 14:27:48 +13:00
import { Link } from 'react-router'
2016-11-14 11:20:02 +13:00
import FontAwesome from 'react-fontawesome'
2016-11-12 16:36:36 +13:00
import PlaylistGrid from '../../components/PlaylistGrid'
2016-11-21 16:26:47 +13:00
import List from '../../components/List'
import DropdownField from '../../components/DropdownField'
import Header from '../../components/Header'
2017-08-14 20:02:41 +12:00
import FilterField from '../../components/FilterField'
2016-12-09 18:47:10 +13:00
import * as helpers from '../../helpers'
2017-08-11 21:59:14 +12:00
import * as coreActions from '../../services/core/actions'
2016-11-14 11:20:02 +13:00
import * as uiActions from '../../services/ui/actions'
import * as mopidyActions from '../../services/mopidy/actions'
import * as spotifyActions from '../../services/spotify/actions'
class LibraryPlaylists extends React.Component{
2017-10-10 13:05:28 +13:00
constructor(props){
2017-08-14 20:02:41 +12:00
super(props)
this.state = {
filter: ''
}
}
componentDidMount(){
2017-08-14 20:02:41 +12:00
if (!this.props.mopidy_library_playlists && this.props.mopidy_connected && (this.props.source == 'all' || this.props.source == 'local')){
this.props.mopidyActions.getLibraryPlaylists()
}
if (this.props.spotify_library_playlists_status !== 'finished' && this.props.spotify_connected && (this.props.source == 'all' || this.props.source == 'spotify')){
this.props.spotifyActions.getLibraryPlaylists()
}
}
componentWillReceiveProps(newProps){
2017-08-14 20:02:41 +12:00
if (newProps.mopidy_connected && (newProps.source == 'all' || newProps.source == 'local')){
// We've just connected
if (!this.props.mopidy_connected){
this.props.mopidyActions.getLibraryPlaylists()
}
// Filter changed, but we haven't got this provider's library yet
2017-08-14 20:02:41 +12:00
if (this.props.source != 'all' && this.props.source != 'local' && !newProps.mopidy_library_playlists){
this.props.mopidyActions.getLibraryPlaylists()
}
}
2017-08-14 20:02:41 +12:00
if (newProps.spotify_connected && (newProps.source == 'all' || newProps.source == 'spotify')){
// We've just connected
if (!this.props.spotify_connected){
this.props.spotifyActions.getLibraryPlaylists()
}
// Filter changed, but we haven't got this provider's library yet
if (this.props.source != 'all' && this.props.source != 'spotify' && newProps.spotify_library_playlists_status !== 'finished'){
this.props.spotifyActions.getLibraryPlaylists()
}
}
}
handleContextMenu(e,item){
var data = {
e: e,
context: 'playlist',
uris: [item.uri],
items: [item]
}
this.props.uiActions.showContextMenu(data)
}
setSort(value){
var reverse = false
2017-10-10 13:05:28 +13:00
if (this.props.sort == value ) reverse = !this.props.sort_reverse
var data = {
library_playlists_sort_reverse: reverse,
library_playlists_sort: value
}
this.props.uiActions.set(data)
}
2016-11-21 16:26:47 +13:00
renderView(){
var playlists = []
2017-07-06 08:11:15 +12:00
// Spotify library items
2017-08-14 20:02:41 +12:00
if (this.props.spotify_library_playlists && (this.props.source == 'all' || this.props.source == 'spotify')){
for (var i = 0; i < this.props.spotify_library_playlists.length; i++){
var uri = this.props.spotify_library_playlists[i]
if (this.props.playlists.hasOwnProperty(uri)){
playlists.push(this.props.playlists[uri])
2017-07-06 08:11:15 +12:00
}
}
}
2016-11-21 16:26:47 +13:00
// Mopidy library items
2017-08-14 20:02:41 +12:00
if (this.props.mopidy_library_playlists && (this.props.source == 'all' || this.props.source == 'local')){
for (var i = 0; i < this.props.mopidy_library_playlists.length; i++){
var uri = this.props.mopidy_library_playlists[i]
if (this.props.playlists.hasOwnProperty(uri)){
playlists.push(this.props.playlists[uri])
}
}
2016-12-09 18:47:10 +13:00
}
playlists = helpers.sortItems(playlists, this.props.sort, this.props.sort_reverse)
playlists = helpers.removeDuplicates(playlists)
2017-08-14 20:02:41 +12:00
if (this.state.filter !== ''){
playlists = helpers.applyFilter('name', this.state.filter, playlists)
}
if (this.props.view == 'list'){
2017-08-23 07:39:33 +12:00
if (this.props.slim_mode){
2017-07-17 08:27:39 +12:00
var columns = [
{
label: 'Name',
name: 'name'
},
{
label: 'Tracks',
name: 'tracks_total'
}
]
} else {
var columns = [
{
label: 'Name',
name: 'name'
},
{
label: 'Owner',
name: 'owner'
},
{
label: 'Tracks',
name: 'tracks_total'
},
{
label: 'Editable',
name: 'can_edit'
2017-08-07 21:34:33 +12:00
},
{
label: 'Source',
name: 'source'
2017-07-17 08:27:39 +12:00
}
]
}
2016-11-21 16:26:47 +13:00
return (
2017-06-18 05:47:36 +12:00
<section className="content-wrapper">
<List
handleContextMenu={(e,item) => this.handleContextMenu(e,item)}
rows={playlists}
columns={columns}
className="playlist-list"
2017-03-06 09:03:20 +13:00
link_prefix={global.baseURL+"playlist/"} />
2016-11-21 16:26:47 +13:00
</section>
)
} else {
2016-11-21 16:26:47 +13:00
return (
2017-06-18 05:47:36 +12:00
<section className="content-wrapper">
<PlaylistGrid
handleContextMenu={(e,item) => this.handleContextMenu(e,item)}
playlists={playlists} />
2016-11-21 16:26:47 +13:00
</section>
)
}
}
render(){
2017-08-14 20:02:41 +12:00
var source_options = [
2017-07-06 08:11:15 +12:00
{
value: 'all',
label: 'All'
},
{
value: 'local',
label: 'Local'
2017-07-06 08:11:15 +12:00
},
{
value: 'spotify',
label: 'Spotify'
2017-07-06 08:11:15 +12:00
}
]
2016-11-21 16:26:47 +13:00
var view_options = [
{
value: 'thumbnails',
label: 'Thumbnails'
},
{
value: 'list',
label: 'List'
}
2016-11-02 21:23:35 +13:00
]
2016-12-09 18:47:10 +13:00
var sort_options = [
{
value: 'name',
label: 'Name'
},
{
value: 'can_edit',
label: 'Editable'
},
{
value: 'owner.id',
label: 'Owner'
},
2016-12-09 18:47:10 +13:00
{
value: 'tracks.total',
label: 'Tracks'
},
{
value: 'source',
label: 'Source'
2016-12-09 18:47:10 +13:00
}
]
2017-03-06 09:03:20 +13:00
var options = (
<span>
2017-08-14 20:02:41 +12:00
<FilterField handleChange={value => this.setState({filter: value})} />
2017-03-06 09:03:20 +13:00
<DropdownField icon="sort" name="Sort" value={this.props.sort} options={sort_options} reverse={this.props.sort_reverse} handleChange={val => {this.setSort(val); this.props.uiActions.hideContextMenu() }} />
<DropdownField icon="eye" name="View" value={this.props.view} options={view_options} handleChange={val => {this.props.uiActions.set({ library_playlists_view: val}); this.props.uiActions.hideContextMenu() }} />
2017-08-14 20:02:41 +12:00
<DropdownField icon="database" name="Source" value={this.props.source} options={source_options} handleChange={val => {this.props.uiActions.set({ library_playlists_source: val}); this.props.uiActions.hideContextMenu() }} />
<button className="no-hover" onClick={ () => this.props.uiActions.openModal('create_playlist', {} ) }>
2016-11-21 16:26:47 +13:00
<FontAwesome name="plus" />&nbsp;
New
</button>
2017-03-06 09:03:20 +13:00
</span>
2016-11-14 17:32:36 +13:00
)
return (
2016-10-26 16:55:47 +13:00
<div className="view library-playlists-view">
2017-03-06 09:03:20 +13:00
<Header icon="playlist" title="My playlists" options={options} uiActions={this.props.uiActions} />
2016-11-21 16:26:47 +13:00
{ this.renderView() }
</div>
)
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
return {
2017-08-23 07:39:33 +12:00
slim_mode: state.ui.slim_mode,
mopidy_connected: state.mopidy.connected,
spotify_connected: state.spotify.connected,
mopidy_library_playlists: state.mopidy.library_playlists,
mopidy_library_playlists_status: state.mopidy.library_playlists_status,
spotify_library_playlists: state.spotify.library_playlists,
spotify_library_playlists_status: state.spotify.library_playlists_status,
2017-05-22 04:19:44 +12:00
load_queue: state.ui.load_queue,
me_id: (state.spotify.me ? state.spotify.me.id : false),
2017-08-02 21:11:22 +12:00
view: state.ui.library_playlists_view,
2017-08-14 20:02:41 +12:00
source: (state.ui.library_playlists_source ? state.ui.library_playlists_source : 'all'),
2017-08-02 21:11:22 +12:00
sort: (state.ui.library_playlists_sort ? state.ui.library_playlists_sort : 'name'),
sort_reverse: (state.ui.library_playlists_sort_reverse ? true : false),
playlists: state.core.playlists
}
}
2016-11-14 11:20:02 +13:00
const mapDispatchToProps = (dispatch) => {
return {
2017-08-11 21:59:14 +12:00
coreActions: bindActionCreators(coreActions, dispatch),
2016-11-14 11:20:02 +13:00
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(LibraryPlaylists)