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

272 lines
8.0 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'
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(){
this.performSearch()
}
componentWillReceiveProps(newProps){
2016-11-09 15:49:25 +13:00
// new query
if( this.props.params.query != newProps.params.query ){
this.performSearch( newProps )
}
// mopidy comes online
if( !this.props.mopidy_connected && newProps.mopidy_connected ){
this.props.mopidyActions.getSearchResults( newProps.params.query, newProps.uri_schemes )
2016-11-03 08:57:16 +13:00
}
}
performSearch( props = this.props ){
2017-03-23 19:20:41 +13:00
var source = props.params.source
var type = props.params.type
this.props.uiActions.startSearch(props.params.query)
2017-03-23 19:20:41 +13:00
if (!source || source == 'all' || source == 'spotify'){
this.props.spotifyActions.getSearchResults( props.params.query )
}
if( props.mopidy_connected ){
this.props.mopidyActions.getSearchResults( props.params.query, props.uri_schemes )
}
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-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]
if (this.props.artists.hasOwnProperty(uri)){
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]
if (this.props.albums.hasOwnProperty(uri)){
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]
if (this.props.playlists.hasOwnProperty(uri)){
playlists.push(this.props.playlists[uri])
}
}
}
2016-11-06 15:47:37 +13:00
switch( this.props.params.type ){
case 'artists':
return (
<div>
<section className="grid-wrapper">
2017-01-16 18:31:06 +13:00
<ArtistGrid artists={artists} />
2017-01-27 08:15:59 +13:00
<LazyLoadListener enabled={this.props['artists_more']} 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-01-27 08:15:59 +13:00
<LazyLoadListener enabled={this.props['albums_more']} 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-01-27 08:15:59 +13:00
<LazyLoadListener enabled={this.props['playlists_more']} loadMore={ () => this.loadMore('playlists') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
break
case 'tracks':
return (
<div>
<section className="list-wrapper">
2017-01-16 18:31:06 +13:00
<TrackList show_source_icon={true} tracks={ this.props.tracks } />
2017-01-27 08:15:59 +13:00
<LazyLoadListener enabled={this.props['tracks_more']} 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">
2016-12-21 15:24:07 +13:00
<h4><Link to={global.baseURL+'search/'+this.props.params.query+'/artists'}>Artists</Link></h4>
2017-01-16 18:31:06 +13:00
<ArtistGrid className="mini" artists={artists.slice(0,6)} />
2016-11-06 15:47:37 +13:00
</div>
</section>
<section>
<div className="inner">
2016-12-21 15:24:07 +13:00
<h4><Link to={global.baseURL+'search/'+this.props.params.query+'/albums'}>Albums</Link></h4>
2017-01-16 18:31:06 +13:00
<AlbumGrid className="mini" albums={albums.slice(0,6)} />
2016-11-06 15:47:37 +13:00
</div>
</section>
<section>
<div className="inner">
2016-12-21 15:24:07 +13:00
<h4><Link to={global.baseURL+'search/'+this.props.params.query+'/playlists'}>Playlists</Link></h4>
2017-01-16 18:31:06 +13:00
<PlaylistGrid className="mini" playlists={playlists.slice(0,6)} />
2016-11-06 15:47:37 +13:00
</div>
</section>
</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-01-16 18:31:06 +13:00
<TrackList show_source_icon={true} tracks={ this.props.tracks } />
2016-11-07 21:21:15 +13:00
<LazyLoadListener loadMore={ () => this.loadMore('tracks') }/>
2016-11-06 15:47:37 +13:00
</section>
</div>
)
}
}
2017-03-23 19:20:41 +13:00
handleTypeChange(val){
this.props.uiActions.hideContextMenu()
var source = (this.props.params.source ? this.props.params.source : 'all')
hashHistory.push(global.baseURL+'search/'+this.props.params.query+'/'+source+'/'+val)
}
handleSourceChange(val){
this.props.uiActions.hideContextMenu()
2017-03-23 19:20:41 +13:00
var type = (this.props.params.type ? this.props.params.type : 'all')
hashHistory.push(global.baseURL+'search/'+this.props.params.query+'/'+val+'/'+type)
}
2016-11-06 15:47:37 +13:00
render(){
2017-03-23 19:20:41 +13:00
var type_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-03-23 19:20:41 +13:00
var source_options = [
{
value: 'all',
label: 'All'
},
{
value: 'spotify',
label: 'Spotify'
}
]
for (var i = 0; i < this.props.uri_schemes.length; i++){
var scheme = this.props.uri_schemes[i].replace(':','')
source_options.push({
value: scheme,
label: scheme
})
}
var options = (
<span>
2017-03-23 19:20:41 +13:00
<DropdownField icon="cube" name="Type" value={this.props.params.type} options={type_options} handleChange={val => this.handleTypeChange(val)} />
<DropdownField icon="server" name="Source" value={this.props.params.source} options={source_options} handleChange={val => this.handleSourceChange(val)} />
</span>
)
2016-11-06 15:47:37 +13:00
return (
<div className="view search-view">
<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,
uri_schemes: state.mopidy.uri_schemes,
2017-01-16 18:31:06 +13:00
tracks: (state.ui.search_results ? state.ui.search_results.tracks : []),
tracks_more: (state.ui.search_results && state.ui.search_results.tracks_more ? state.ui.search_results.tracks_more : null),
2017-01-16 18:31:06 +13:00
artists: state.ui.artists,
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),
albums: state.ui.albums,
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),
playlists: state.ui.playlists,
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)