Remove hardcoded filters in doSearch

## Issue
6414 <Don't use "exact match" setting outside of main search - cuases no results in related>

`doSearch` was used in Wunderbar and Recommended, but it was adding in Wunderbar options all the time.

## Changes
- Fix `doSearch` to not use hardcoded options (take from arguments).
- Refactors the searchOption-creation code so that we don't duplicate the logic in both .js and .jsx.
This commit is contained in:
infinite-persistence 2021-07-13 14:28:09 +08:00
parent 052aa87ddd
commit b822ae39e1
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
4 changed files with 25 additions and 72 deletions

View file

@ -1,43 +1,42 @@
import { connect } from 'react-redux';
import { withRouter } from 'react-router';
import { doSearch } from 'redux/actions/search';
import { SIMPLE_SITE } from 'config';
import {
selectIsSearching,
makeSelectSearchUris,
makeSelectQueryWithOptions,
selectSearchOptions,
makeSelectHasReachedMaxResultsLength,
} from 'redux/selectors/search';
import { selectShowMatureContent } from 'redux/selectors/settings';
import { selectUserVerifiedEmail } from 'redux/selectors/user';
import { getSearchQueryString } from 'util/query-params';
import SearchPage from './view';
const select = (state, props) => {
const showMature = selectShowMatureContent(state);
const urlParams = new URLSearchParams(props.location.search);
let urlQuery = urlParams.get('q') || null;
if (urlQuery) {
urlQuery = urlQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' ');
}
const query = makeSelectQueryWithOptions(
urlQuery,
SIMPLE_SITE
? { nsfw: false, isBackgroundSearch: false }
: showMature === false
? { nsfw: false, isBackgroundSearch: false }
: { isBackgroundSearch: false }
)(state);
const searchOptions = {
...selectSearchOptions(state),
isBackgroundSearch: false,
nsfw: showMature,
};
const query = getSearchQueryString(urlQuery, searchOptions);
const uris = makeSelectSearchUris(query)(state);
const hasReachedMaxResultsLength = makeSelectHasReachedMaxResultsLength(query)(state);
return {
urlQuery,
searchOptions,
isSearching: selectIsSearching(state),
showNsfw: showMature,
uris: uris,
isAuthenticated: selectUserVerifiedEmail(state),
searchOptions: selectSearchOptions(state),
hasReachedMaxResultsLength: hasReachedMaxResultsLength,
};
};

View file

@ -11,43 +11,21 @@ import { formatLbryUrlForWeb } from 'util/url';
import { useHistory } from 'react-router';
import { SEARCH_PAGE_SIZE } from 'constants/search';
type AdditionalOptions = {
isBackgroundSearch: boolean,
nsfw?: boolean,
from?: number,
};
type Props = {
search: (string, AdditionalOptions) => void,
searchOptions: {},
urlQuery: string,
searchOptions: SearchOptions,
search: (string, SearchOptions) => void,
isSearching: boolean,
location: UrlLocation,
uris: Array<string>,
showNsfw: boolean,
isAuthenticated: boolean,
hasReachedMaxResultsLength: boolean,
};
export default function SearchPage(props: Props) {
const {
search,
uris,
location,
isSearching,
showNsfw,
isAuthenticated,
searchOptions,
hasReachedMaxResultsLength,
} = props;
const { urlQuery, searchOptions, search, uris, isSearching, isAuthenticated, hasReachedMaxResultsLength } = props;
const { push } = useHistory();
const urlParams = new URLSearchParams(location.search);
const urlQuery = urlParams.get('q') || '';
const additionalOptions: AdditionalOptions = { isBackgroundSearch: false };
const [from, setFrom] = React.useState(0);
additionalOptions['nsfw'] = SIMPLE_SITE ? false : showNsfw;
additionalOptions['from'] = from;
const modifiedUrlQuery = urlQuery.trim().replace(/\s+/g, '').replace(/:/g, '#');
const uriFromQuery = `lbry://${modifiedUrlQuery}`;
@ -78,14 +56,14 @@ export default function SearchPage(props: Props) {
} catch (e) {}
}
const stringifiedOptions = JSON.stringify(additionalOptions);
const stringifiedSearchOptions = JSON.stringify(searchOptions);
useEffect(() => {
if (urlQuery) {
const jsonOptions = JSON.parse(stringifiedOptions);
search(urlQuery, jsonOptions);
const searchOptions = JSON.parse(stringifiedSearchOptions);
search(urlQuery, { ...searchOptions, from: from });
}
}, [search, urlQuery, stringifiedOptions, stringifiedSearchOptions]);
}, [search, urlQuery, stringifiedSearchOptions, from]);
function loadMore() {
if (!isSearching && !hasReachedMaxResultsLength) {
@ -114,7 +92,7 @@ export default function SearchPage(props: Props) {
header={
<SearchOptions
simple={SIMPLE_SITE}
additionalOptions={additionalOptions}
additionalOptions={searchOptions}
onSearchOptionsChanged={resetPage}
/>
}

View file

@ -1,13 +1,9 @@
// @flow
import * as ACTIONS from 'constants/action_types';
import { buildURI, doResolveUris, batchActions, isURIValid, makeSelectClaimForUri } from 'lbry-redux';
import {
makeSelectSearchUris,
makeSelectQueryWithOptions,
selectSearchValue,
selectSearchOptions,
} from 'redux/selectors/search';
import { makeSelectSearchUris, selectSearchValue } from 'redux/selectors/search';
import handleFetchResponse from 'util/handle-fetch';
import { getSearchQueryString } from 'util/query-params';
type Dispatch = (action: any) => any;
type GetState = () => { search: SearchState };
@ -44,10 +40,9 @@ export const doSearch = (rawQuery: string, searchOptions: SearchOptions) => (
const state = getState();
const mainOptions: any = selectSearchOptions(state);
const queryWithOptions = makeSelectQueryWithOptions(query, searchOptions)(state);
const queryWithOptions = getSearchQueryString(query, searchOptions);
const size = mainOptions.size;
const size = searchOptions.size;
const from = searchOptions.from;
// If we have already searched for something, we don't need to do anything
@ -101,7 +96,7 @@ export const doSearch = (rawQuery: string, searchOptions: SearchOptions) => (
});
dispatch(batchActions(...actions));
})
.catch((e) => {
.catch(() => {
dispatch({
type: ACTIONS.SEARCH_FAIL,
});

View file

@ -57,25 +57,6 @@ export const makeSelectHasReachedMaxResultsLength = (query: string): ((state: St
return hasReachedMaxResultsLength[query];
});
// Creates a query string based on the state in the search reducer
// Can be overrided by passing in custom sizes/from values for other areas pagination
type CustomOptions = {
isBackgroundSearch?: boolean,
size?: number,
from?: number,
related_to?: string,
nsfw?: boolean,
};
export const makeSelectQueryWithOptions = (customQuery: ?string, options: CustomOptions) =>
createSelector(selectSearchValue, selectSearchOptions, (query, defaultOptions) => {
const searchOptions = { ...defaultOptions, ...options };
const queryString = getSearchQueryString(customQuery || query, searchOptions);
return queryString;
});
export const makeSelectRecommendedContentForUri = (uri: string) =>
createSelector(
makeSelectClaimForUri(uri),