import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import Button from '../../components/Button';
import PlaylistGrid from '../../components/PlaylistGrid';
import List from '../../components/List';
import DropdownField from '../../components/Fields/DropdownField';
import Header from '../../components/Header';
import FilterField from '../../components/Fields/FilterField';
import LazyLoadListener from '../../components/LazyLoadListener';
import Icon from '../../components/Icon';
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';
import { applyFilter, removeDuplicates, sortItems } from '../../util/arrays';
import { I18n, i18n } from '../../locale';
class LibraryPlaylists extends React.Component {
constructor(props) {
super(props);
this.state = {
filter: '',
limit: 50,
per_page: 50,
};
}
componentDidMount() {
// Restore any limit defined in our location state
const state = (this.props.location.state ? this.props.location.state : {});
if (state.limit) {
this.setState({
limit: state.limit,
});
}
this.props.uiActions.setWindowTitle(i18n('library.playlists.title'));
this.getMopidyLibrary();
this.getSpotifyLibrary();
}
getMopidyLibrary = () => {
const {
source,
mopidy_library_playlists,
mopidyActions: {
getLibraryPlaylists,
},
} = this.props;
if (source !== 'local' && source !== 'all') return;
if (mopidy_library_playlists) return;
getLibraryPlaylists();
};
getSpotifyLibrary = () => {
const {
source,
spotify_available,
spotify_library_playlists_status,
spotifyActions: {
getLibraryPlaylists,
},
} = this.props;
if (!spotify_available) return;
if (source !== 'spotify' && source !== 'all') return;
if (spotify_library_playlists_status === 'finished') return;
if (spotify_library_playlists_status === 'started') return;
getLibraryPlaylists();
};
componentDidUpdate = ({ source: prevSource }) => {
const { source } = this.props;
if (source !== prevSource) {
this.getMopidyLibrary();
this.getSpotifyLibrary();
}
}
handleContextMenu(e, item) {
const data = {
e,
context: 'playlist',
uris: [item.uri],
items: [item],
};
this.props.uiActions.showContextMenu(data);
}
loadMore() {
const new_limit = this.state.limit + this.state.per_page;
this.setState({ limit: new_limit });
// Set our pagination to location state
const state = (this.props.location && this.props.location.state ? this.props.location.state : {});
state.limit = new_limit;
this.props.history.replace({ state });
}
setSort(value) {
let reverse = false;
if (this.props.sort == value) reverse = !this.props.sort_reverse;
const data = {
library_playlists_sort_reverse: reverse,
library_playlists_sort: value,
};
this.props.uiActions.set(data);
}
renderView() {
let playlists = [];
// Spotify library items
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]);
}
}
}
// Mopidy library items
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]);
}
}
}
if (this.props.sort) {
playlists = sortItems(playlists, this.props.sort, this.props.sort_reverse);
}
playlists = removeDuplicates(playlists);
if (this.state.filter !== '') {
playlists = applyFilter('name', this.state.filter, playlists);
}
// Apply our lazy-load-rendering
const total_playlists = playlists.length;
playlists = playlists.slice(0, this.state.limit);
if (this.props.view == 'list') {
return (
this.handleContextMenu(e, item)}
rows={playlists}
thumbnail
details={['owner', 'tracks_total', 'last_modified']}
right_column={['source']}
className="playlists"
link_prefix="/playlist/"
/>