Skip to content

Commit

Permalink
Instant Search: Hook up default options (inc. sort) (#13742)
Browse files Browse the repository at this point in the history
  • Loading branch information
gibrown authored Oct 15, 2019
1 parent 1d461d7 commit d21855a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
12 changes: 10 additions & 2 deletions modules/search/class.jetpack-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ public function load_assets() {
$script_path = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
wp_enqueue_script( 'jetpack-instant-search', $script_path, array(), $script_version, true );

$widget_options = Jetpack_Search_Helpers::get_widgets_from_option();
if ( is_array( $widget_options ) ) {
$widget_options = end( $widget_options );
}

$filters = Jetpack_Search_Helpers::get_filters_from_widgets();
$widgets = array();
foreach( $filters as $key => $filter ) {
Expand All @@ -228,8 +233,11 @@ public function load_assets() {
// This is probably a temporary filter for testing the prototype.
$options = array(
'postTypes' => $post_type_labels,
'siteId' => Jetpack::get_option( 'id' ),
'widgets' => array_values( $widgets ),
'siteId' => Jetpack::get_option( 'id' ),
'widgets' => array_values( $widgets ),
'sort' => $widget_options['sort'],
'postTypeFilters' => $widget_options['post_types'],
'enableLoadOnScroll' => false,
'locale' => str_replace( '_', '-', get_locale() ),
);
/**
Expand Down
1 change: 1 addition & 0 deletions modules/search/instant-search/components/search-app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class SearchApp extends Component {
query={ this.state.query }
response={ this.state.response }
resultFormat={ this.props.options.resultFormat }
enableLoadOnScroll={ this.props.options.enableLoadOnScroll }
/>
</Portal>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SearchResults extends Component {
{ results.map( result => this.render_result( result ) ) }
{ this.props.hasNextPage && (
<ScrollButton
enableLoadOnScroll
enableLoadOnScroll={ this.props.enableLoadOnScroll }
isLoading={ this.props.isLoading }
onLoadNextPage={ this.props.onLoadNextPage }
/>
Expand Down
4 changes: 2 additions & 2 deletions modules/search/instant-search/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { h, render } from 'preact';
* Internal dependencies
*/
import SearchApp from './components/search-app';
import { getSearchQuery, getFilterQuery, getSortQuery } from './lib/query-string';
import { getSearchQuery, getFilterQuery, determineDefaultSort } from './lib/query-string';
import { getThemeOptions } from './lib/dom';
import { SERVER_OBJECT_NAME } from './lib/constants';

Expand All @@ -18,8 +18,8 @@ const injectSearchApp = grabFocus => {
<SearchApp
grabFocus={ grabFocus }
initialFilters={ getFilterQuery() }
initialSort={ getSortQuery() }
initialValue={ getSearchQuery() }
initialSort={ determineDefaultSort( window[ SERVER_OBJECT_NAME ].sort ) }
options={ window[ SERVER_OBJECT_NAME ] }
themeOptions={ getThemeOptions( window[ SERVER_OBJECT_NAME ] ) }
/>,
Expand Down
17 changes: 17 additions & 0 deletions modules/search/instant-search/lib/query-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ export function setSearchQuery( searchValue ) {
pushQueryString( encode( query ) );
}

export function determineDefaultSort( widgetOptions ) {
const query = getQuery();
if ( 'orderby' in query ) {
return getSortQuery();
}

switch ( widgetOptions ) {
case 'date|DESC':
return 'date_desc';
case 'date|ASC':
return 'date_asc';
case 'relevance|DESC':
default:
return 'score_default';
}
}

export function getSortQuery() {
const query = getQuery();
const order = 'order' in query ? query.order : 'DESC';
Expand Down

0 comments on commit d21855a

Please sign in to comment.