import React, { PropTypes } from 'react' import { connect } from 'react-redux' import { Link, hashHistory } from 'react-router' import { bindActionCreators } from 'redux' import FontAwesome from 'react-fontawesome' import ReactGA from 'react-ga' import Header from '../components/Header' import DropdownField from '../components/DropdownField' import TrackList from '../components/TrackList' import ArtistGrid from '../components/ArtistGrid' import AlbumGrid from '../components/AlbumGrid' import PlaylistGrid from '../components/PlaylistGrid' import LazyLoadListener from '../components/LazyLoadListener' import SearchForm from '../components/SearchForm' import * as helpers from '../helpers' 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' class Search extends React.Component{ constructor(props){ super(props) } componentDidMount(){ // Auto-focus on the input field $(document).find('.search-form input').focus(); if (this.props.params.query && this.props.params.query !== ''){ if (this.props.mopidy_connected && this.props.search_uri_schemes){ 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:')){ this.props.spotifyActions.getSearchResults(this.props.view, this.props.params.query) } } } componentWillReceiveProps(newProps){ if (!this.props.mopidy_connected && newProps.mopidy_connected && newProps.params.query){ this.props.mopidyActions.getSearchResults(newProps.view, newProps.params.query) } 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) } // 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){ 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:')){ this.props.spotifyActions.getSearchResults(newProps.view, newProps.params.query) } } } loadMore(type){ alert('load more: '+type) //this.props.spotifyActions.getURL(this.props['spotify_'+type+'_more'], 'SPOTIFY_SEARCH_RESULTS_LOADED_MORE_'+type.toUpperCase()); } 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) } renderResults(){ var spotify_search_enabled = (this.props.search_settings && this.props.search_settings.spotify) var artists = [] if (this.props.mopidy_search_results.artists){ artists = [...artists, ...helpers.getIndexedRecords(this.props.artists,this.props.mopidy_search_results.artists)] } if (this.props.spotify_search_results.artists){ artists = [...artists, ...helpers.getIndexedRecords(this.props.artists,this.props.spotify_search_results.artists)] } artists = helpers.sortItems(artists, this.props.sort, this.props.sort_reverse); var albums = [] if (this.props.mopidy_search_results.albums){ albums = [...albums, ...helpers.getIndexedRecords(this.props.albums,this.props.mopidy_search_results.albums)]; } if (this.props.spotify_search_results.albums){ albums = [...albums, ...helpers.getIndexedRecords(this.props.albums,this.props.spotify_search_results.albums)] } albums = helpers.sortItems(albums, this.props.sort, this.props.sort_reverse); var playlists = [] if (this.props.mopidy_search_results.playlists){ playlists = [...playlists, ...helpers.getIndexedRecords(this.props.playlists,this.props.mopidy_search_results.playlists)] } if (this.props.spotify_search_results.playlists){ playlists = [...playlists, ...helpers.getIndexedRecords(this.props.playlists,this.props.spotify_search_results.playlists)] } playlists = helpers.sortItems(playlists, this.props.sort, this.props.sort_reverse); var tracks = [] if (this.props.mopidy_search_results.tracks){ tracks = [...tracks, ...this.props.mopidy_search_results.tracks] } if (this.props.spotify_search_results.tracks){ tracks = [...tracks, ...this.props.spotify_search_results.tracks] } tracks = helpers.sortItems(tracks, this.props.sort, this.props.sort_reverse); switch (this.props.view){ case 'artists': return (