From 635381a616da9da31bba33637ca0dcaaf9692053 Mon Sep 17 00:00:00 2001 From: jessop Date: Thu, 30 Jan 2020 01:30:27 -0500 Subject: [PATCH] convert search options to expandable object wip bundle --- dist/bundle.es.js | 63 ++++++++++++++++++++++++----------- src/redux/actions/search.js | 51 +++++++++++++++++++++------- src/redux/selectors/claims.js | 21 ++++++------ src/redux/selectors/search.js | 25 +++++++------- src/util/query-params.js | 10 ++++-- 5 files changed, 114 insertions(+), 56 deletions(-) diff --git a/dist/bundle.es.js b/dist/bundle.es.js index f029ce8..d619f34 100644 --- a/dist/bundle.es.js +++ b/dist/bundle.es.js @@ -1071,9 +1071,11 @@ function toQueryString(params) { return parts.join('&'); } -const getSearchQueryString = (query, options = {}, includeUserOptions = false, additionalOptions = {}) => { +const getSearchQueryString = (query, options = {}) => { const encodedQuery = encodeURIComponent(query); const queryParams = [`s=${encodedQuery}`, `size=${options.size || DEFAULT_SEARCH_SIZE}`, `from=${options.from || DEFAULT_SEARCH_RESULT_FROM}`]; + const { isBackgroundSearch } = options; + const includeUserOptions = typeof isBackgroundSearch === 'undefined' ? false : !isBackgroundSearch; if (includeUserOptions) { const claimType = options[SEARCH_OPTIONS.CLAIM_TYPE]; @@ -1085,6 +1087,12 @@ const getSearchQueryString = (query, options = {}, includeUserOptions = false, a } } + const additionalOptions = {}; + const { related_to } = options; + const { nsfw } = options; + if (related_to) additionalOptions['related_to'] = related_to; + if (typeof nsfw !== 'undefined') additionalOptions['nsfw'] = nsfw; + if (additionalOptions) { Object.keys(additionalOptions).forEach(key => { const option = additionalOptions[key]; @@ -1443,11 +1451,11 @@ const selectSearchSuggestions = reselect.createSelector(selectSearchValue, selec // 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 -const makeSelectQueryWithOptions = (customQuery, customSize, customFrom, isBackgroundSearch = false, // If it's a background search, don't use the users settings -additionalOptions = {}) => reselect.createSelector(selectSearchValue, selectSearchOptions, (query, options) => { - const size = customSize || options[SEARCH_OPTIONS.RESULT_COUNT]; - const queryString = getSearchQueryString(customQuery || query, _extends$1({}, options, { size, from: customFrom }), !isBackgroundSearch, additionalOptions); + +const makeSelectQueryWithOptions = (customQuery, options) => reselect.createSelector(selectSearchValue, selectSearchOptions, (query, defaultOptions) => { + const searchOptions = _extends$1({}, defaultOptions, options); + const queryString = getSearchQueryString(customQuery || query, searchOptions); return queryString; }); @@ -2249,11 +2257,11 @@ const makeSelectRecommendedContentForUri = uri => reselect.createSelector(makeSe return; } - const options = { related_to: claim.claim_id }; + const options = { related_to: claim.claim_id, isBackgroundSearch: true }; if (!isMature) { options['nsfw'] = false; } - const searchQuery = getSearchQueryString(title.replace(/\//, ' '), undefined, undefined, options); + const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options); let searchUris = searchUrisByQuery[searchQuery]; if (searchUris) { @@ -2326,7 +2334,7 @@ const makeSelectMyStreamUrlsForPage = (page = 1) => reselect.createSelector(sele const selectMyStreamUrlsCount = reselect.createSelector(selectMyClaimUrisWithoutChannels, channels => channels.length); -const makeSelectResolvedRecommendedContentForUri = (uri, size) => reselect.createSelector(makeSelectClaimForUri(uri), selectResolvedSearchResultsByQuery, (claim, resolvedResultsByQuery) => { +const makeSelectResolvedRecommendedContentForUri = (uri, size) => reselect.createSelector(makeSelectClaimForUri(uri), selectResolvedSearchResultsByQuery, makeSelectClaimIsNsfw(uri), (claim, resolvedResultsByQuery, isMature) => { const atVanityURI = !uri.includes('#'); let recommendedContent; @@ -2340,9 +2348,12 @@ const makeSelectResolvedRecommendedContentForUri = (uri, size) => reselect.creat return; } - const searchQuery = getSearchQueryString(title.replace(/\//, ' '), { size }, undefined, { - related_to: claim.claim_id - }); + const options = { related_to: claim.claim_id, isBackgroundSearch: true }; + if (!isMature) { + options['nsfw'] = false; + } + + const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options); let results = resolvedResultsByQuery[searchQuery]; if (results) { @@ -3968,7 +3979,7 @@ function handleFetchResponse(response) { return response.status === 200 ? Promise.resolve(response.json()) : Promise.reject(new Error(response.statusText)); } -// +var _extends$7 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; const DEBOUNCED_SEARCH_SUGGESTION_MS = 300; @@ -4025,9 +4036,10 @@ const doUpdateSearchQuery = (query, shouldSkipSuggestions) => dispatch => { } }; -const doSearch = (rawQuery, size, // only pass in if you don't want to use the users setting (ex: related content) -from, isBackgroundSearch = false, options = {}, resolveResults = true) => (dispatch, getState) => { +const doSearch = (rawQuery, searchOptions) => (dispatch, getState) => { const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' '); + const resolveResults = searchOptions && searchOptions.resolveResults; + const isBackgroundSearch = searchOptions && searchOptions.isBackgroundSearch || false; if (!query) { dispatch({ @@ -4037,7 +4049,8 @@ from, isBackgroundSearch = false, options = {}, resolveResults = true) => (dispa } const state = getState(); - let queryWithOptions = makeSelectQueryWithOptions(query, size, from, isBackgroundSearch, options)(state); + + let queryWithOptions = makeSelectQueryWithOptions(query, searchOptions)(state); // If we have already searched for something, we don't need to do anything const urisForQuery = makeSelectSearchUris(queryWithOptions)(state); @@ -4108,11 +4121,23 @@ from, isBackgroundSearch = false, options = {}) => (dispatch, getState) => { return; } + const optionsWithFrom = _extends$7({ + size, + from, + isBackgroundSearch + }, options); + + const optionsWithoutFrom = _extends$7({ + size, + isBackgroundSearch + }, options); + const state = getState(); - let queryWithOptions = makeSelectQueryWithOptions(query, size, from, isBackgroundSearch, options)(state); + + let queryWithOptions = makeSelectQueryWithOptions(query, optionsWithFrom)(state); // make from null so that we can maintain a reference to the same query for multiple pages and simply append the found results - let queryWithoutFrom = makeSelectQueryWithOptions(query, size, null, isBackgroundSearch, options)(state); + let queryWithoutFrom = makeSelectQueryWithOptions(query, optionsWithoutFrom)(state); // If we have already searched for something, we don't need to do anything // TODO: Tweak this check for multiple page results @@ -4162,7 +4187,7 @@ const doBlurSearchInput = () => dispatch => dispatch({ type: SEARCH_BLUR }); -const doUpdateSearchOptions = newOptions => (dispatch, getState) => { +const doUpdateSearchOptions = (newOptions, additionalOptions) => (dispatch, getState) => { const state = getState(); const searchValue = selectSearchValue(state); @@ -4173,7 +4198,7 @@ const doUpdateSearchOptions = newOptions => (dispatch, getState) => { if (searchValue) { // After updating, perform a search with the new options - dispatch(doSearch(searchValue)); + dispatch(doSearch(searchValue, additionalOptions)); } }; diff --git a/src/redux/actions/search.js b/src/redux/actions/search.js index e00d7ba..9701929 100644 --- a/src/redux/actions/search.js +++ b/src/redux/actions/search.js @@ -17,6 +17,20 @@ const DEBOUNCED_SEARCH_SUGGESTION_MS = 300; type Dispatch = (action: any) => any; type GetState = () => { search: SearchState }; +type SearchOptions = { + size?: number, + from?: number, + related_to?: string, + nsfw?: boolean, + isBackgroundSearch?: boolean, + resolveResults?: boolean, +} + +type AdditionalOptions = { + isBackgroundSearch: boolean, + nsfw?: boolean, +} + // We can't use env's because they aren't passed into node_modules let CONNECTION_STRING = 'https://lighthouse.lbry.com/'; @@ -75,17 +89,14 @@ export const doUpdateSearchQuery = (query: string, shouldSkipSuggestions: ?boole } }; + export const doSearch = ( rawQuery: string, - size: ?number, // only pass in if you don't want to use the users setting (ex: related content) - from: ?number, - isBackgroundSearch: boolean = false, - options: { - related_to?: string, - } = {}, - resolveResults: boolean = true + searchOptions: SearchOptions, ) => (dispatch: Dispatch, getState: GetState) => { const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' '); + const resolveResults = searchOptions && searchOptions.resolveResults; + const isBackgroundSearch = (searchOptions && searchOptions.isBackgroundSearch) || false; if (!query) { dispatch({ @@ -95,7 +106,8 @@ export const doSearch = ( } const state = getState(); - let queryWithOptions = makeSelectQueryWithOptions(query, size, from, isBackgroundSearch, options)( + + let queryWithOptions = makeSelectQueryWithOptions(query, searchOptions)( state ); @@ -167,6 +179,7 @@ export const doResolvedSearch = ( isBackgroundSearch: boolean = false, options: { related_to?: string, + // nsfw here } = {} ) => (dispatch: Dispatch, getState: GetState) => { const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' '); @@ -178,13 +191,27 @@ export const doResolvedSearch = ( return; } + const optionsWithFrom: SearchOptions = { + size, + from, + isBackgroundSearch, + ...options, + } + + const optionsWithoutFrom: SearchOptions = { + size, + isBackgroundSearch, + ...options, + } + const state = getState(); - let queryWithOptions = makeSelectQueryWithOptions(query, size, from, isBackgroundSearch, options)( + + let queryWithOptions = makeSelectQueryWithOptions(query, optionsWithFrom)( state ); // make from null so that we can maintain a reference to the same query for multiple pages and simply append the found results - let queryWithoutFrom = makeSelectQueryWithOptions(query, size, null, isBackgroundSearch, options)( + let queryWithoutFrom = makeSelectQueryWithOptions(query, optionsWithoutFrom)( state ); @@ -241,7 +268,7 @@ export const doBlurSearchInput = () => (dispatch: Dispatch) => type: ACTIONS.SEARCH_BLUR, }); -export const doUpdateSearchOptions = (newOptions: SearchOptions) => ( +export const doUpdateSearchOptions = (newOptions: SearchOptions, additionalOptions: AdditionalOptions) => ( dispatch: Dispatch, getState: GetState ) => { @@ -255,6 +282,6 @@ export const doUpdateSearchOptions = (newOptions: SearchOptions) => ( if (searchValue) { // After updating, perform a search with the new options - dispatch(doSearch(searchValue)); + dispatch(doSearch(searchValue, additionalOptions)); } }; diff --git a/src/redux/selectors/claims.js b/src/redux/selectors/claims.js index cc8aff6..c7f489b 100644 --- a/src/redux/selectors/claims.js +++ b/src/redux/selectors/claims.js @@ -530,16 +530,11 @@ export const makeSelectRecommendedContentForUri = (uri: string) => return; } - const options = { related_to: claim.claim_id }; + const options = { related_to: claim.claim_id, isBackgroundSearch: true }; if (!isMature) { options['nsfw'] = false; } - const searchQuery = getSearchQueryString( - title.replace(/\//, ' '), - undefined, - undefined, - options - ); + const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options); let searchUris = searchUrisByQuery[searchQuery]; if (searchUris) { @@ -671,7 +666,8 @@ export const makeSelectResolvedRecommendedContentForUri = (uri: string, size: nu createSelector( makeSelectClaimForUri(uri), selectResolvedSearchResultsByQuery, - (claim, resolvedResultsByQuery) => { + makeSelectClaimIsNsfw(uri), + (claim, resolvedResultsByQuery, isMature) => { const atVanityURI = !uri.includes('#'); let recommendedContent; @@ -685,9 +681,12 @@ export const makeSelectResolvedRecommendedContentForUri = (uri: string, size: nu return; } - const searchQuery = getSearchQueryString(title.replace(/\//, ' '), { size }, undefined, { - related_to: claim.claim_id, - }); + const options = { related_to: claim.claim_id, isBackgroundSearch: true } + if (!isMature) { + options['nsfw'] = false; + } + + const searchQuery = getSearchQueryString(title.replace(/\//, ' '), options); let results = resolvedResultsByQuery[searchQuery]; if (results) { diff --git a/src/redux/selectors/search.js b/src/redux/selectors/search.js index 48dca04..e3efd11 100644 --- a/src/redux/selectors/search.js +++ b/src/redux/selectors/search.js @@ -165,26 +165,27 @@ export const selectSearchSuggestions: Array = createSelector( // 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, - customSize: ?number, - customFrom: ?number, - isBackgroundSearch: boolean = false, // If it's a background search, don't use the users settings - additionalOptions: { - related_to?: string, - } = {} + options: CustomOptions, ) => createSelector( selectSearchValue, selectSearchOptions, - (query, options) => { - const size = customSize || options[SEARCH_OPTIONS.RESULT_COUNT]; - + (query, defaultOptions) => { + const searchOptions = { ...defaultOptions, ...options }; const queryString = getSearchQueryString( customQuery || query, - { ...options, size, from: customFrom }, - !isBackgroundSearch, - additionalOptions + searchOptions, ); return queryString; diff --git a/src/util/query-params.js b/src/util/query-params.js index 7a3da29..80a1d54 100644 --- a/src/util/query-params.js +++ b/src/util/query-params.js @@ -36,8 +36,6 @@ export function toQueryString(params: { [string]: string | number }) { export const getSearchQueryString = ( query: string, options: any = {}, - includeUserOptions: boolean = false, - additionalOptions: {} = {} ) => { const encodedQuery = encodeURIComponent(query); const queryParams = [ @@ -45,6 +43,8 @@ export const getSearchQueryString = ( `size=${options.size || DEFAULT_SEARCH_SIZE}`, `from=${options.from || DEFAULT_SEARCH_RESULT_FROM}`, ]; + const { isBackgroundSearch } = options; + const includeUserOptions = typeof isBackgroundSearch === 'undefined' ? false : !isBackgroundSearch; if (includeUserOptions) { const claimType = options[SEARCH_OPTIONS.CLAIM_TYPE]; @@ -68,6 +68,12 @@ export const getSearchQueryString = ( } } + const additionalOptions = {} + const { related_to } = options; + const { nsfw } = options; + if (related_to) additionalOptions['related_to'] = related_to; + if (typeof nsfw !== 'undefined') additionalOptions['nsfw'] = nsfw; + if (additionalOptions) { Object.keys(additionalOptions).forEach(key => { const option = additionalOptions[key];