Skip to content

Commit

Permalink
fix: url param injection due to changing default resultsPerPage value (
Browse files Browse the repository at this point in the history
  • Loading branch information
zlatanpham authored Jun 22, 2022
1 parent eadf699 commit a004135
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/silver-scissors-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sajari/react-hooks': patch
'@sajari/react-search-ui': patch
---

Fix using a different default `resultsPerPage` param causes URL param injection.
12 changes: 10 additions & 2 deletions packages/hooks/src/ContextProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable import/named */
/* eslint-disable @typescript-eslint/no-shadow */
import { getSearchParams, isEmpty, isString } from '@sajari/react-sdk-utils';
import { getSearchParams, isEmpty, isNumber, isString } from '@sajari/react-sdk-utils';
import React, { useCallback, useEffect, useRef, useState } from 'react';

import URLStateSync from '../URLStateSync';
Expand Down Expand Up @@ -137,6 +137,9 @@ const ContextProvider: React.FC<SearchProviderValues> = ({
Object.assign(autocompleteProp, { variables: autocompleteVariables.current });
}

const resultsPerPageValue = parseInt(variables.current.get()[searchState.config.resultsPerPageParam], 10);
const defaultResultsPerPage = React.useRef(isNumber(resultsPerPageValue) ? resultsPerPageValue : 15);

if (!configDone) {
if (syncURLState) {
const params = getSearchParams();
Expand All @@ -148,7 +151,11 @@ const ContextProvider: React.FC<SearchProviderValues> = ({
variables: variables.current,
params,
mappingKeys: [
{ paramKey: 'show', variableKey: 'resultsPerPage', defaultValue: '15' },
{
paramKey: 'show',
variableKey: searchState.config.resultsPerPageParam,
defaultValue: defaultResultsPerPage.current.toString(),
},
{ paramKey: 'sort', variableKey: 'sort' },
{ paramKey: autocompleteState.config.qParam, variableKey: autocompleteState.config.qParam },
{ paramKey: searchState.config.qParam, variableKey: searchState.config.qParam },
Expand Down Expand Up @@ -352,6 +359,7 @@ const ContextProvider: React.FC<SearchProviderValues> = ({
resetFilters,
fields: search.fields,
searching,
defaultResultsPerPage: defaultResultsPerPage.current,
},
autocomplete: {
...state.autocomplete,
Expand Down
1 change: 1 addition & 0 deletions packages/hooks/src/ContextProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface PipelineContextState {
searching: boolean;
filters?: (FilterBuilder | RangeFilterBuilder)[];
redirects: Redirects;
defaultResultsPerPage: number;
}

export interface ProviderPipelineConfig {
Expand Down
6 changes: 3 additions & 3 deletions packages/hooks/src/URLStateSync/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const URLStateSync = (props: URLStateSyncProps = {}) => {
},
[setPageInner],
);
const { resultsPerPage, setResultsPerPage } = useResultsPerPage();
const { resultsPerPage, setResultsPerPage, defaultResultsPerPage } = useResultsPerPage();
const paramWatchers: QueryParam[] = [
{
key: qParam,
Expand All @@ -151,9 +151,9 @@ const URLStateSync = (props: URLStateSyncProps = {}) => {
{
key: 'show',
value: resultsPerPage,
defaultValue: 15,
defaultValue: defaultResultsPerPage,
callback: (value) => {
setResultsPerPage(Number(value) || 15);
setResultsPerPage(Number(value) || defaultResultsPerPage);
},
},
{
Expand Down
4 changes: 3 additions & 1 deletion packages/hooks/src/useResultsPerPage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function useResultsPerPage(): UseResultsPerPageResult {
search,
config: { resultsPerPageParam },
variables,
defaultResultsPerPage,
},
} = useContext();

Expand All @@ -24,7 +25,8 @@ function useResultsPerPage(): UseResultsPerPageResult {
const resultsPerPage = parseInt(variables.get()[resultsPerPageParam], 10);

return {
resultsPerPage: isNumber(resultsPerPage) ? resultsPerPage : 15,
resultsPerPage: isNumber(resultsPerPage) ? resultsPerPage : defaultResultsPerPage,
defaultResultsPerPage,
setResultsPerPage,
};
}
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/src/useResultsPerPage/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface UseResultsPerPageResult {
/** The default number of results per page */
defaultResultsPerPage: number;
/** The number of results per page */
resultsPerPage: number;
/** Set the number of results per page */
Expand Down

0 comments on commit a004135

Please sign in to comment.