Skip to content

Commit 011983f

Browse files
markbrocatodijs
andauthored
Adds the ability to customize the URL scheme for search pages when fi… (#134)
* Adds the ability to customize the URL scheme for search pages when filters and sort are applied. * Clean up and version bump Co-authored-by: Richard van der Dys <[email protected]>
1 parent 6b0e96d commit 011983f

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-storefront",
3-
"version": "8.16.4",
3+
"version": "8.17.0",
44
"description": "Build and deploy e-commerce progressive web apps (PWAs) in record time.",
55
"module": "./index.js",
66
"license": "Apache-2.0",

src/mock-connector/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export { default as home } from './home.js'
77
export { default as product } from './product.js'
88
export { default as productMedia } from './productMedia.js'
99
export { default as productSuggestions } from './productSuggestions.js'
10-
export { default as routes } from './routes.js'
1110
export { default as search } from './search.js'
1211
export { default as searchSuggestions } from './searchSuggestions.js'
1312
export { default as session } from './session.js'

src/mock-connector/routes.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/plp/SearchResultsProvider.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import getAPIURL from '../api/getAPIURL'
2424
* }
2525
* ```
2626
*/
27-
export default function SearchResultsProvider({ store, updateStore, children }) {
27+
export default function SearchResultsProvider({ store, updateStore, queryForState, children }) {
2828
useEffect(() => {
2929
if (store.reloading) {
3030
async function refresh() {
@@ -133,6 +133,8 @@ export default function SearchResultsProvider({ store, updateStore, children })
133133
* Computes the query for the current state of the search controls
134134
*/
135135
const getQueryForState = () => {
136+
if (queryForState) return queryForState(store.pageData)
137+
136138
const { filters, page, sort } = store.pageData
137139
const { search } = window.location
138140
const query = qs.parse(search, { ignoreQueryPrefix: true })
@@ -167,7 +169,6 @@ export default function SearchResultsProvider({ store, updateStore, children })
167169
*/
168170
const getURLForState = query => {
169171
const { pathname, hash } = window.location
170-
171172
return pathname + qs.stringify(query, { addQueryPrefix: true }) + hash
172173
}
173174

@@ -211,4 +212,10 @@ SearchResultsProvider.propTypes = {
211212
* The update function returned from [`useSearchResultsStore`](/apiReference/plp/useSearchResultsStore).
212213
*/
213214
updateStore: PropTypes.func.isRequired,
215+
216+
/**
217+
* An optional function to customize the URL format for search pages when the user
218+
* changes filters and sort.
219+
*/
220+
queryForState: PropTypes.func,
214221
}

test/plp/SearchResultsProvider.test.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('SearchResultsProvider', () => {
2929
delete window.__NEXT_DATA__
3030
})
3131

32-
const Test = () => {
32+
const Test = props => {
3333
const [store, updateStore] = useState(initialStore)
3434

3535
const ContextGetter = () => {
@@ -40,7 +40,7 @@ describe('SearchResultsProvider', () => {
4040
}
4141

4242
return (
43-
<SearchResultsProvider store={store} updateStore={updateStore}>
43+
<SearchResultsProvider store={store} updateStore={updateStore} {...props}>
4444
<ContextGetter />
4545
</SearchResultsProvider>
4646
)
@@ -220,4 +220,31 @@ describe('SearchResultsProvider', () => {
220220
expect(fetch).toHaveBeenCalled()
221221
})
222222
})
223+
224+
it('should use the query string returned by queryForState', async () => {
225+
fetchMock.mockResponseOnce(
226+
JSON.stringify({
227+
pageData: {
228+
products: [],
229+
},
230+
}),
231+
)
232+
233+
let providedState
234+
235+
const queryForState = state => {
236+
providedState = state
237+
return { foo: 'bar' }
238+
}
239+
240+
wrapper = mount(<Test queryForState={queryForState} />)
241+
242+
await act(async () => {
243+
await context.actions.toggleFilter({ code: 'red' }, true)
244+
await wrapper.update()
245+
})
246+
247+
expect(providedState.filters).toEqual(['blue', 'red'])
248+
expect(fetch).toHaveBeenCalledWith('/api/test?foo=bar&__v__=development')
249+
})
223250
})

0 commit comments

Comments
 (0)