2016-11-03 08:57:16 +13:00
|
|
|
|
|
|
|
|
import React, { PropTypes } from 'react'
|
|
|
|
|
import { connect } from 'react-redux'
|
2017-03-14 08:55:10 +13:00
|
|
|
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'
|
2017-03-14 08:55:10 +13:00
|
|
|
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
|
|
|
|
2017-03-23 21:20:25 +13:00
|
|
|
import * as helpers from '../helpers'
|
2017-08-03 15:26:54 +12:00
|
|
|
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{
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// Make sure we have search parameters to start with
|
|
|
|
|
if (this.props.params && this.props.params.type && this.props.params.query){
|
2017-08-03 15:26:54 +12:00
|
|
|
this.props.coreActions.startSearch(this.props.params.type, this.props.params.query)
|
2017-06-20 19:55:47 +12:00
|
|
|
}
|
2017-06-22 09:01:50 +12:00
|
|
|
|
|
|
|
|
// Auto-focus on the input field
|
|
|
|
|
$(document).find('.search-form input').focus();
|
2016-11-03 08:57:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps){
|
2017-06-20 19:55:47 +12:00
|
|
|
|
|
|
|
|
// Make sure we have some search parameters
|
|
|
|
|
if (newProps.params && newProps.params.type && newProps.params.query){
|
2016-11-09 15:49:25 +13:00
|
|
|
|
2017-06-20 19:55:47 +12:00
|
|
|
if (this.props.params.query != newProps.params.query || this.props.params.type != newProps.params.type){
|
2017-08-03 15:26:54 +12:00
|
|
|
this.props.coreActions.startSearch(newProps.params.type, newProps.params.query)
|
2017-06-20 19:55:47 +12:00
|
|
|
}
|
2016-11-09 15:49:25 +13:00
|
|
|
|
2017-06-20 19:55:47 +12:00
|
|
|
// mopidy comes online
|
|
|
|
|
if (!this.props.mopidy_connected && newProps.mopidy_connected){
|
2017-08-03 15:26:54 +12:00
|
|
|
this.props.coreActions.startSearch(newProps.params.type, newProps.params.query, true)
|
2017-06-20 19:55:47 +12:00
|
|
|
}
|
2017-03-23 19:20:41 +13:00
|
|
|
}
|
2016-11-03 08:57:16 +13:00
|
|
|
}
|
|
|
|
|
|
2016-11-06 15:47:37 +13:00
|
|
|
loadMore(type){
|
2017-01-27 08:15:59 +13:00
|
|
|
this.props.spotifyActions.getURL( this.props[type+'_more'], 'SPOTIFY_SEARCH_RESULTS_LOADED_MORE_'+type.toUpperCase());
|
2016-11-03 08:57:16 +13:00
|
|
|
}
|
|
|
|
|
|
2016-11-06 15:47:37 +13:00
|
|
|
renderResults(){
|
2017-03-24 23:05:43 +13:00
|
|
|
var spotify_search_enabled = (this.props.search_settings && this.props.search_settings.spotify)
|
|
|
|
|
|
2017-01-16 18:31:06 +13:00
|
|
|
var artists = []
|
|
|
|
|
if (this.props.artists_uris){
|
|
|
|
|
for (var i = 0; i < this.props.artists_uris.length; i++){
|
|
|
|
|
var uri = this.props.artists_uris[i]
|
2017-03-24 08:46:20 +13:00
|
|
|
if (this.props.artists.hasOwnProperty(uri)){
|
2017-01-16 18:31:06 +13:00
|
|
|
artists.push(this.props.artists[uri])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var albums = []
|
|
|
|
|
if (this.props.albums_uris){
|
|
|
|
|
for (var i = 0; i < this.props.albums_uris.length; i++){
|
|
|
|
|
var uri = this.props.albums_uris[i]
|
2017-03-24 08:46:20 +13:00
|
|
|
if (this.props.albums.hasOwnProperty(uri)){
|
2017-01-16 18:31:06 +13:00
|
|
|
albums.push(this.props.albums[uri])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var playlists = []
|
|
|
|
|
if (this.props.playlists_uris){
|
|
|
|
|
for (var i = 0; i < this.props.playlists_uris.length; i++){
|
|
|
|
|
var uri = this.props.playlists_uris[i]
|
2017-03-24 08:46:20 +13:00
|
|
|
if (this.props.playlists.hasOwnProperty(uri)){
|
2017-01-16 18:31:06 +13:00
|
|
|
playlists.push(this.props.playlists[uri])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 21:20:25 +13:00
|
|
|
var tracks = []
|
|
|
|
|
if (this.props.tracks){
|
2017-03-24 08:46:20 +13:00
|
|
|
for (var i = 0; i < this.props.tracks.length; i++){
|
|
|
|
|
if (helpers.uriSource(this.props.tracks[i].uri)){
|
|
|
|
|
tracks.push(this.props.tracks[i])
|
2017-03-23 21:20:25 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 08:46:20 +13:00
|
|
|
switch (this.props.params.type){
|
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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
case 'tracks':
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<section className="list-wrapper">
|
2017-05-05 10:47:18 +12:00
|
|
|
<TrackList tracks={tracks} uri={'iris:search:'+this.props.params.type+':'+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
|
|
|
|
|
|
|
|
|
|
default:
|
2017-03-27 08:29:18 +13:00
|
|
|
|
|
|
|
|
if (artists.length > 0){
|
|
|
|
|
var artists_section = (
|
|
|
|
|
<section>
|
|
|
|
|
<div className="inner">
|
2017-05-18 08:35:57 +12:00
|
|
|
<h4><Link to={global.baseURL+'search/iris:search:artists:'+this.props.params.query}>Artists</Link></h4>
|
2017-03-27 08:29:18 +13:00
|
|
|
<ArtistGrid show_source_icon artists={artists.slice(0,5)} />
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
var artists_section = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (albums.length > 0){
|
|
|
|
|
var albums_section = (
|
|
|
|
|
<section>
|
|
|
|
|
<div className="inner">
|
2017-05-18 08:35:57 +12:00
|
|
|
<h4><Link to={global.baseURL+'search/iris:search:albums:'+this.props.params.query}>Albums</Link></h4>
|
2017-03-27 08:29:18 +13:00
|
|
|
<AlbumGrid show_source_icon albums={albums.slice(0,5)} />
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
var albums_section = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (playlists.length > 0){
|
|
|
|
|
var playlists_section = (
|
|
|
|
|
<section>
|
|
|
|
|
<div className="inner">
|
2017-05-18 08:35:57 +12:00
|
|
|
<h4><Link to={global.baseURL+'search/iris:search:playlists:'+this.props.params.query}>Playlists</Link></h4>
|
2017-03-27 08:29:18 +13:00
|
|
|
<PlaylistGrid show_source_icon playlists={playlists.slice(0,5)} />
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
var playlists_section = null
|
|
|
|
|
}
|
|
|
|
|
|
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}
|
2016-11-04 09:35:39 +13:00
|
|
|
</div>
|
2016-11-03 08:57:16 +13:00
|
|
|
|
2016-11-06 15:47:37 +13:00
|
|
|
<section className="list-wrapper">
|
2017-06-20 08:29:56 +12:00
|
|
|
<h4><Link to={global.baseURL+'search/'+this.props.params.query+'/tracks'}>Tracks</Link></h4>
|
2017-05-05 10:47:18 +12:00
|
|
|
<TrackList tracks={tracks} uri={'iris:search:'+this.props.params.type+':'+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>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-04 09:35:39 +13:00
|
|
|
|
2017-03-23 19:20:41 +13:00
|
|
|
handleTypeChange(val){
|
|
|
|
|
this.props.uiActions.hideContextMenu()
|
2017-05-05 10:47:18 +12:00
|
|
|
hashHistory.push(global.baseURL+'search/iris:search:'+val+':'+this.props.params.query)
|
2017-03-14 08:55:10 +13:00
|
|
|
}
|
|
|
|
|
|
2016-11-06 15:47:37 +13:00
|
|
|
render(){
|
2017-03-23 19:20:41 +13:00
|
|
|
var type_options = [
|
2017-03-14 08:55:10 +13:00
|
|
|
{
|
2017-03-23 19:20:41 +13:00
|
|
|
value: 'all',
|
2017-03-14 08:55:10 +13:00
|
|
|
label: 'All'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'artists',
|
|
|
|
|
label: 'Artists'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'albums',
|
|
|
|
|
label: 'Albums'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'playlists',
|
|
|
|
|
label: 'Playlists'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: 'tracks',
|
|
|
|
|
label: 'Tracks'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
var options = (
|
|
|
|
|
<span>
|
2017-03-24 08:46:20 +13:00
|
|
|
<DropdownField icon="eye" name="Type" value={this.props.params.type} options={type_options} handleChange={val => this.handleTypeChange(val)} />
|
|
|
|
|
<button onClick={e => this.props.uiActions.openModal('search_settings', {type: this.props.params.type, query: this.props.params.query})}>
|
|
|
|
|
<FontAwesome name="wrench" />
|
2017-03-25 10:21:10 +13:00
|
|
|
Sources
|
2017-03-24 08:46:20 +13:00
|
|
|
</button>
|
2017-03-14 08:55:10 +13:00
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
|
2016-11-06 15:47:37 +13:00
|
|
|
return (
|
|
|
|
|
<div className="view search-view">
|
2017-06-22 09:01:50 +12:00
|
|
|
<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 {
|
|
|
|
|
mopidy_connected: state.mopidy.connected,
|
2017-03-24 08:46:20 +13:00
|
|
|
search_settings: (state.ui.search_settings ? state.ui.search_settings : null),
|
2017-08-03 15:26:54 +12:00
|
|
|
tracks: (state.core.search_results ? state.core.search_results.tracks : []),
|
|
|
|
|
tracks_more: (state.core.search_results && state.core.search_results.tracks_more ? state.core.search_results.tracks_more : null),
|
2017-08-02 10:53:30 +12:00
|
|
|
artists: (state.core.artists ? state.core.artists : []),
|
2017-08-03 15:26:54 +12:00
|
|
|
artists_uris: (state.core.search_results ? state.core.search_results.artists_uris : []),
|
|
|
|
|
artists_more: (state.core.search_results ? state.core.search_results.artists_more : null),
|
2017-08-02 10:53:30 +12:00
|
|
|
albums: (state.core.albums ? state.core.albums : []),
|
2017-08-03 15:26:54 +12:00
|
|
|
albums_uris: (state.core.search_results ? state.core.search_results.albums_uris : []),
|
|
|
|
|
albums_more: (state.core.search_results ? state.core.search_results.albums_more : null),
|
2017-08-02 10:53:30 +12:00
|
|
|
playlists: (state.core.playlists ? state.core.playlists : []),
|
2017-08-03 15:26:54 +12:00
|
|
|
playlists_uris: (state.core.search_results ? state.core.search_results.playlists_uris : []),
|
|
|
|
|
playlists_more: (state.core.search_results ? state.core.search_results.playlists_more : null)
|
2016-11-09 15:49:25 +13:00
|
|
|
}
|
2016-11-03 08:57:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
|
return {
|
2017-08-03 15:26:54 +12:00
|
|
|
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)
|