Files
Iris/src/js/views/Search.js

336 lines
11 KiB
JavaScript
Raw Normal View History

2016-11-03 08:57:16 +13:00
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { Link, hashHistory } from 'react-router'
2016-11-03 08:57:16 +13:00
import { bindActionCreators } from 'redux'
import FontAwesome from 'react-fontawesome'
2017-01-04 15:24:13 +13:00
import ReactGA from 'react-ga'
2016-11-03 08:57:16 +13:00
import Header from '../components/Header'
import DropdownField from '../components/DropdownField'
2016-11-03 08:57:16 +13:00
import TrackList from '../components/TrackList'
import ArtistGrid from '../components/ArtistGrid'
import AlbumGrid from '../components/AlbumGrid'
import PlaylistGrid from '../components/PlaylistGrid'
2016-11-06 15:47:37 +13:00
import LazyLoadListener from '../components/LazyLoadListener'
2017-06-20 19:55:47 +12:00
import SearchForm from '../components/SearchForm'
2016-11-03 08:57:16 +13:00
import * as helpers from '../helpers'
import * as coreActions from '../services/core/actions'
2016-11-03 08:57:16 +13:00
import * as uiActions from '../services/ui/actions'
import * as mopidyActions from '../services/mopidy/actions'
import * as spotifyActions from '../services/spotify/actions'
class Search extends React.Component{
2017-10-10 13:05:28 +13:00
constructor(props){
2017-06-21 09:16:26 +12:00
super(props)
2016-11-03 08:57:16 +13:00
}
componentDidMount(){
2017-06-20 19:55:47 +12:00
// Auto-focus on the input field
$(document).find('.search-form input').focus();
2017-09-05 08:26:04 +12:00
if (this.props.params.query && this.props.params.query !== ''){
if (this.props.mopidy_connected && this.props.search_uri_schemes){
2017-09-05 08:26:04 +12:00
this.props.mopidyActions.getSearchResults(this.props.view, this.props.params.query)
}
if (this.props.spotify_connected && this.props.search_uri_schemes && this.props.search_uri_schemes.includes('spotify:')){
2017-09-05 08:26:04 +12:00
this.props.spotifyActions.getSearchResults(this.props.view, this.props.params.query)
}
}
2016-11-03 08:57:16 +13:00
}
componentWillReceiveProps(newProps){
2017-09-05 08:26:04 +12:00
if (!this.props.mopidy_connected && newProps.mopidy_connected && newProps.params.query){
this.props.mopidyActions.getSearchResults(newProps.view, newProps.params.query)
}
2016-11-09 15:49:25 +13:00
if (!this.props.spotify_connected && newProps.spotify_connected && newProps.params.query && newProps.search_uri_schemes.includes('spotify:')){
this.props.spotifyActions.getSearchResults(newProps.view, newProps.params.query)
2017-03-23 19:20:41 +13:00
}
2017-09-05 08:26:04 +12:00
// Search changed
if (this.props.params.query !== newProps.params.query || this.props.view !== newProps.view){
this.props.mopidyActions.clearSearchResults()
this.props.spotifyActions.clearSearchResults()
if (this.props.mopidy_connected && this.props.search_uri_schemes){
2017-09-05 08:26:04 +12:00
this.props.mopidyActions.getSearchResults(newProps.view, newProps.params.query)
}
if (this.props.mopidy_connected && this.props.search_uri_schemes && this.props.search_uri_schemes.includes('spotify:')){
2017-09-05 08:26:04 +12:00
this.props.spotifyActions.getSearchResults(newProps.view, newProps.params.query)
}
}
2016-11-03 08:57:16 +13:00
}
2016-11-06 15:47:37 +13:00
loadMore(type){
alert('load more: '+type)
2017-10-10 13:05:28 +13:00
//this.props.spotifyActions.getURL(this.props['spotify_'+type+'_more'], 'SPOTIFY_SEARCH_RESULTS_LOADED_MORE_'+type.toUpperCase());
2016-11-03 08:57:16 +13:00
}
2017-12-04 08:43:09 +13:00
setSort(value){
var reverse = false
if (this.props.sort == value ) reverse = !this.props.sort_reverse
var data = {
search_results_sort_reverse: reverse,
search_results_sort: value
}
this.props.uiActions.set(data)
}
2016-11-06 15:47:37 +13:00
renderResults(){
var spotify_search_enabled = (this.props.search_settings && this.props.search_settings.spotify);
2017-03-24 23:05:43 +13:00
if (this.props.sort == 'uri'){
var sort_map = this.props.search_uri_schemes;
} else {
var sort_map = null;
}
var artists = [];
if (this.props.mopidy_search_results.artists){
artists = [...artists, ...helpers.getIndexedRecords(this.props.artists,this.props.mopidy_search_results.artists)];
2017-01-16 18:31:06 +13:00
}
2017-09-05 15:47:35 +12:00
if (this.props.spotify_search_results.artists){
artists = [...artists, ...helpers.getIndexedRecords(this.props.artists,this.props.spotify_search_results.artists)];
2017-09-05 15:47:35 +12:00
}
artists = helpers.sortItems(artists, this.props.sort, this.props.sort_reverse, sort_map);
2017-01-16 18:31:06 +13:00
var albums = [];
if (this.props.mopidy_search_results.albums){
2017-12-04 08:43:09 +13:00
albums = [...albums, ...helpers.getIndexedRecords(this.props.albums,this.props.mopidy_search_results.albums)];
2017-01-16 18:31:06 +13:00
}
2017-09-05 15:47:35 +12:00
if (this.props.spotify_search_results.albums){
albums = [...albums, ...helpers.getIndexedRecords(this.props.albums,this.props.spotify_search_results.albums)]
2017-12-04 08:43:09 +13:00
}
albums = helpers.sortItems(albums, this.props.sort, this.props.sort_reverse, sort_map);
2017-01-16 18:31:06 +13:00
var playlists = []
if (this.props.mopidy_search_results.playlists){
playlists = [...playlists, ...helpers.getIndexedRecords(this.props.playlists,this.props.mopidy_search_results.playlists)];
2017-01-16 18:31:06 +13:00
}
2017-09-05 15:47:35 +12:00
if (this.props.spotify_search_results.playlists){
playlists = [...playlists, ...helpers.getIndexedRecords(this.props.playlists,this.props.spotify_search_results.playlists)];
2017-09-05 15:47:35 +12:00
}
playlists = helpers.sortItems(playlists, this.props.sort, this.props.sort_reverse, sort_map);
2017-01-16 18:31:06 +13:00
var tracks = [];
if (this.props.mopidy_search_results.tracks){
tracks = [...tracks, ...this.props.mopidy_search_results.tracks];
}
2017-09-05 15:47:35 +12:00
if (this.props.spotify_search_results.tracks){
tracks = [...tracks, ...this.props.spotify_search_results.tracks];
2017-09-05 15:47:35 +12:00
}
tracks = helpers.sortItems(tracks, this.props.sort, this.props.sort_reverse, sort_map);
switch (this.props.view){
2016-11-06 15:47:37 +13:00
case 'artists':
return (
<div>
<section className="grid-wrapper">
2017-03-27 08:29:18 +13:00
<ArtistGrid artists={artists} show_source_icon />
2017-03-24 23:05:43 +13:00
<LazyLoadListener enabled={this.props['artists_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('artists') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
break;
2016-11-06 15:47:37 +13:00
case 'albums':
return (
<div>
<section className="grid-wrapper">
2017-03-27 08:29:18 +13:00
<AlbumGrid albums={albums} show_source_icon />
2017-03-24 23:05:43 +13:00
<LazyLoadListener enabled={this.props['albums_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('albums') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
break;
2016-11-06 15:47:37 +13:00
case 'playlists':
return (
<div>
<section className="grid-wrapper">
2017-03-27 08:29:18 +13:00
<PlaylistGrid playlists={playlists} show_source_icon />
2017-03-24 23:05:43 +13:00
<LazyLoadListener enabled={this.props['playlists_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('playlists') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
break;
2016-11-06 15:47:37 +13:00
case 'tracks':
return (
<div>
<section className="list-wrapper">
<TrackList tracks={tracks} uri={'iris:search:'+this.props.params.query} show_source_icon />
2017-03-24 23:05:43 +13:00
<LazyLoadListener enabled={this.props['tracks_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('tracks') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
break;
2016-11-06 15:47:37 +13:00
default:
2017-03-27 08:29:18 +13:00
if (artists.length > 0){
var artists_section = (
<section>
<div className="inner">
<h4>Artists</h4>
2017-09-03 16:10:32 +12:00
<ArtistGrid show_source_icon single_row artists={artists.slice(0,5)} />
2017-03-27 08:29:18 +13:00
</div>
</section>
)
} else {
var artists_section = null;
2017-03-27 08:29:18 +13:00
}
if (albums.length > 0){
var albums_section = (
<section>
<div className="inner">
<h4>Albums</h4>
2017-09-03 16:10:32 +12:00
<AlbumGrid show_source_icon single_row albums={albums.slice(0,5)} />
2017-03-27 08:29:18 +13:00
</div>
</section>
)
} else {
var albums_section = null;
2017-03-27 08:29:18 +13:00
}
if (playlists.length > 0){
var playlists_section = (
<section>
<div className="inner">
<h4>Playlists</h4>
2017-09-03 16:10:32 +12:00
<PlaylistGrid show_source_icon single_row playlists={playlists.slice(0,5)} />
2017-03-27 08:29:18 +13:00
</div>
</section>
)
} else {
var playlists_section = null;
2017-03-27 08:29:18 +13:00
}
2017-09-05 08:26:04 +12:00
if (tracks.length > 0){
var tracks_section = (
<section className="list-wrapper">
<h4>Tracks</h4>
<TrackList tracks={tracks} uri={'iris:search:'+this.props.params.query} show_source_icon />
<LazyLoadListener loading={this.props['tracks_more'] && spotify_search_enabled} loadMore={ () => this.loadMore('tracks') }/>
</section>
)
} else {
var tracks_section = null;
2017-09-05 08:26:04 +12:00
}
2016-11-06 15:47:37 +13:00
return (
<div>
<div className="search-result-sections cf">
2017-03-27 08:29:18 +13:00
{artists_section}
{albums_section}
{playlists_section}
</div>
2017-09-05 08:26:04 +12:00
{tracks_section}
2016-11-06 15:47:37 +13:00
</div>
)
}
}
2016-11-06 15:47:37 +13:00
render(){
var view_options = [
{
2017-03-23 19:20:41 +13:00
value: 'all',
label: 'All'
},
{
value: 'artists',
label: 'Artists'
},
{
value: 'albums',
label: 'Albums'
},
{
value: 'playlists',
label: 'Playlists'
},
{
value: 'tracks',
label: 'Tracks'
}
]
2017-12-04 08:43:09 +13:00
var sort_options = [
{
value: 'name',
label: 'Name'
},
{
value: 'artists.name',
label: 'Artist'
},
{
value: 'duration',
label: 'Duration'
},
{
value: 'uri',
2017-12-04 08:43:09 +13:00
label: 'Source'
}
]
var options = (
<span>
2017-12-04 08:43:09 +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({ search_view: val }); this.props.uiActions.hideContextMenu() }} />
<button className="no-hover" onClick={e => this.props.uiActions.openModal('search_uri_schemes', {query: this.props.params.query})}>
<FontAwesome name="wrench" />&nbsp;
2017-03-25 10:21:10 +13:00
Sources
</button>
</span>
)
2016-11-06 15:47:37 +13:00
return (
<div className="view search-view">
<Header icon="search" options={options} uiActions={this.props.uiActions} />
<SearchForm query={this.props.params.query} />
2017-06-18 05:47:36 +12:00
<div className="content-wrapper">
{ this.renderResults() }
</div>
2016-11-03 08:57:16 +13:00
</div>
);
}
}
const mapStateToProps = (state, ownProps) => {
2016-11-09 15:49:25 +13:00
return {
view: (state.ui.search_view ? state.ui.search_view : 'all'),
2016-11-09 15:49:25 +13:00
mopidy_connected: state.mopidy.connected,
spotify_connected: state.spotify.connected,
albums: (state.core.albums ? state.core.albums : []),
artists: (state.core.artists ? state.core.artists : []),
playlists: (state.core.playlists ? state.core.playlists : []),
tracks: (state.core.tracks ? state.core.tracks : []),
search_uri_schemes: (state.ui.search_uri_schemes ? state.ui.search_uri_schemes : []),
mopidy_search_results: (state.mopidy.search_results ? state.mopidy.search_results : {}),
2017-12-04 08:43:09 +13:00
spotify_search_results: (state.spotify.search_results ? state.spotify.search_results : {}),
sort: (state.ui.search_results_sort ? state.ui.search_results_sort : 'name'),
sort_reverse: (state.ui.search_results_sort_reverse ? true : false),
2016-11-09 15:49:25 +13:00
}
2016-11-03 08:57:16 +13:00
}
const mapDispatchToProps = (dispatch) => {
return {
coreActions: bindActionCreators(coreActions, dispatch),
2016-11-03 08:57:16 +13:00
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Search)