Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(starters): Filter v2 by default (#9739)
* Filter for v2 starters by default. #7900 * WIP * fix reset all filters issue * move out default search option * wip * refactor to use render prop * fix filtersApplied for metadata * pr refactor * linting
- Loading branch information
1 parent
fc06ee9
commit 4064e35
Showing
4 changed files
with
73 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,55 @@ | ||
import React from "react" | ||
import React, { Component, Fragment } from "react" | ||
import queryString from "query-string" | ||
import { navigate } from "@reach/router" | ||
|
||
// manage your state entirely within the router, so that it's copiable | ||
// https://gist.github.com/sw-yx/efd9ee71669413bca6a895d87e30742f | ||
|
||
export default defaultURLState => Component => props => { | ||
const { location } = props | ||
const urlState = { ...defaultURLState, ...queryString.parse(location.search) } | ||
const setURLState = newState => { | ||
const finalState = { ...urlState, ...newState } // merge with existing urlstate | ||
Object.keys(finalState).forEach(function(k) { | ||
if ( | ||
// Don't save some state values if it meets the conditions below. | ||
!finalState[k] || // falsy | ||
finalState[k] === `` || // string | ||
(Array.isArray(finalState[k]) && !finalState[k].length) || // array | ||
finalState[k] === defaultURLState[k] // same as default state, unnecessary | ||
) { | ||
delete finalState[k] // Drop query params with new values = falsy | ||
const emptySearchState = { s: ``, c: [], d: [], v: [], sort: `recent` } | ||
class RRSM extends Component { | ||
state = emptySearchState | ||
|
||
static defaultProps = { | ||
defaultSearchState: {}, | ||
} | ||
|
||
setUrlState = newState => { | ||
const finalState = { ...this.state, ...newState } | ||
// update RSSM state | ||
this.setState({ ...finalState }) | ||
|
||
// sync url to RSSM | ||
const params = Object.keys(finalState).reduce((merged, key) => { | ||
// right now the sort behavior is default, it doesn't show in the url | ||
if (finalState[key] && key !== `sort`) { | ||
merged[key] = finalState[key] | ||
} | ||
}) | ||
return navigate(`${location.pathname}?${queryString.stringify(finalState)}`) | ||
return merged | ||
}, {}) | ||
|
||
return navigate(`${location.pathname}?${queryString.stringify(params)}`) | ||
} | ||
|
||
componentDidMount() { | ||
const urlState = queryString.parse(location.search) | ||
|
||
// if urlState is empty, default to v2 | ||
if (Object.keys(urlState).length === 0) { | ||
return this.setUrlState(this.props.defaultSearchState) | ||
} | ||
|
||
// otherwise, set to urlState | ||
return this.setUrlState(urlState) | ||
} | ||
|
||
render() { | ||
const { render } = this.props | ||
return ( | ||
<Fragment> | ||
{render({ | ||
setURLState: this.setUrlState, | ||
urlState: this.state, | ||
})} | ||
</Fragment> | ||
) | ||
} | ||
return ( | ||
<Component | ||
setURLState={setURLState} // use this instead of `setState` | ||
urlState={urlState} // easier to read state from this instead of `location` | ||
{...props} | ||
/> | ||
) | ||
} | ||
|
||
export default RRSM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters