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'
|
2016-11-03 08:57:16 +13:00
|
|
|
|
2017-03-23 21:20:25 +13:00
|
|
|
import * as helpers from '../helpers'
|
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) {
|
|
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount(){
|
2017-03-24 08:46:20 +13:00
|
|
|
this.props.uiActions.startSearch(this.props.params.type, this.props.params.query)
|
2016-11-03 08:57:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps){
|
2016-11-09 15:49:25 +13:00
|
|
|
|
2017-03-24 23:05:43 +13:00
|
|
|
if (this.props.params.query != newProps.params.query || this.props.params.type != newProps.params.type){
|
2017-03-24 08:46:20 +13:00
|
|
|
this.props.uiActions.startSearch(newProps.params.type, newProps.params.query)
|
2016-11-09 15:49:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mopidy comes online
|
2017-03-24 23:05:43 +13:00
|
|
|
if (!this.props.mopidy_connected && newProps.mopidy_connected){
|
2017-03-24 08:46:20 +13:00
|
|
|
this.props.uiActions.startSearch(newProps.params.type, newProps.params.query, true)
|
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-01-16 18:31:06 +13:00
|
|
|
<ArtistGrid artists={artists} />
|
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-01-16 18:31:06 +13:00
|
|
|
<AlbumGrid albums={albums} />
|
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-01-16 18:31:06 +13:00
|
|
|
<PlaylistGrid playlists={playlists} />
|
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-03-23 21:20:25 +13:00
|
|
|
<TrackList show_source_icon={true} tracks={ tracks } />
|
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:
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<div className="search-result-sections cf">
|
|
|
|
|
<section>
|
|
|
|
|
<div className="inner">
|
2017-03-25 09:40:13 +13:00
|
|
|
<h4><Link to={global.baseURL+'search/artists/'+this.props.params.query}>Artists</Link></h4>
|
2017-03-25 10:21:10 +13:00
|
|
|
{artists.length > 0 ? <ArtistGrid className="mini" artists={artists.slice(0,6)} /> : <span className="grey-text">No results</span>}
|
2016-11-06 15:47:37 +13:00
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
<section>
|
|
|
|
|
<div className="inner">
|
2017-03-25 09:40:13 +13:00
|
|
|
<h4><Link to={global.baseURL+'search/albums/'+this.props.params.query}>Albums</Link></h4>
|
2017-03-25 10:21:10 +13:00
|
|
|
{albums.length > 0 ? <AlbumGrid className="mini" albums={albums.slice(0,6)} /> : <span className="grey-text">No results</span>}
|
2016-11-06 15:47:37 +13:00
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
<section>
|
|
|
|
|
<div className="inner">
|
2017-03-25 09:40:13 +13:00
|
|
|
<h4><Link to={global.baseURL+'search/playlists/'+this.props.params.query}>Playlists</Link></h4>
|
2017-03-25 10:21:10 +13:00
|
|
|
{playlists.length > 0 ? <PlaylistGrid className="mini" playlists={playlists.slice(0,6)} /> : <span className="grey-text">No results</span>}
|
2016-11-06 15:47:37 +13:00
|
|
|
</div>
|
|
|
|
|
</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">
|
2016-12-21 15:24:07 +13:00
|
|
|
<h4 className="left-padding"><Link to={global.baseURL+'search/'+this.props.params.query+'/tracks'}>Tracks</Link></h4>
|
2017-03-25 10:21:10 +13:00
|
|
|
<TrackList show_source_icon={true} tracks={tracks} />
|
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-03-24 08:46:20 +13:00
|
|
|
hashHistory.push(global.baseURL+'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-03-14 08:55:10 +13:00
|
|
|
<Header icon="search" title="Search results" options={options} uiActions={this.props.uiActions} />
|
2016-11-06 15:47:37 +13:00
|
|
|
{ this.renderResults() }
|
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-01-16 18:31:06 +13:00
|
|
|
tracks: (state.ui.search_results ? state.ui.search_results.tracks : []),
|
2017-01-18 16:52:55 +13:00
|
|
|
tracks_more: (state.ui.search_results && state.ui.search_results.tracks_more ? state.ui.search_results.tracks_more : null),
|
2017-03-24 16:06:49 +13:00
|
|
|
artists: (state.ui.artists ? state.ui.artists : []),
|
2017-01-16 18:31:06 +13:00
|
|
|
artists_uris: (state.ui.search_results ? state.ui.search_results.artists_uris : []),
|
|
|
|
|
artists_more: (state.ui.search_results ? state.ui.search_results.artists_more : null),
|
2017-03-24 16:06:49 +13:00
|
|
|
albums: (state.ui.albums ? state.ui.albums : []),
|
2017-01-16 18:31:06 +13:00
|
|
|
albums_uris: (state.ui.search_results ? state.ui.search_results.albums_uris : []),
|
|
|
|
|
albums_more: (state.ui.search_results ? state.ui.search_results.albums_more : null),
|
2017-03-24 16:06:49 +13:00
|
|
|
playlists: (state.ui.playlists ? state.ui.playlists : []),
|
2017-01-16 18:31:06 +13:00
|
|
|
playlists_uris: (state.ui.search_results ? state.ui.search_results.playlists_uris : []),
|
|
|
|
|
playlists_more: (state.ui.search_results ? state.ui.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 {
|
|
|
|
|
uiActions: bindActionCreators(uiActions, dispatch),
|
|
|
|
|
mopidyActions: bindActionCreators(mopidyActions, dispatch),
|
|
|
|
|
spotifyActions: bindActionCreators(spotifyActions, dispatch)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(Search)
|