-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor SuperAdmin dashboard table filtering
- Loading branch information
Showing
2 changed files
with
39 additions
and
84 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,82 +1,33 @@ | ||
import _filter from "lodash/filter"; | ||
import _isEmpty from "lodash/isEmpty"; | ||
import _omit from "lodash/omit"; | ||
import queryString from "query-string"; | ||
import { Component } from "react"; | ||
import WithSearch from "../HOCs/WithSearch/WithSearch"; | ||
|
||
const SEARCH_FIELD_GETTER = { | ||
challenges: (item) => item.name, | ||
projects: (item) => item.displayName, | ||
users: (item) => item.osmProfile?.displayName, | ||
}; | ||
|
||
/** | ||
* WithMetricsSearchResults acts as a filter that applies the named search query to an | ||
* array of candidate items, presenting to the wrapped component only those | ||
* items that match the query. | ||
* | ||
* @param {string} searchName - the name of the search/query to work with | ||
* @param {string} itemsProp - the name of the prop containing the array | ||
* of items to search (e.g. 'challenges' or 'projects' or 'users'). | ||
* @param {string} outputProp - optional name of the prop to use when passing | ||
* down the filtered results. By default it will be the same as | ||
* itemsProp. | ||
**/ | ||
export function WithMetricsSearchResults(WrappedComponent) { | ||
return function (props) { | ||
const params = queryString.parse(props.location.search); | ||
const searchType = params["searchType"] || "challenges"; | ||
const getSearchField = SEARCH_FIELD_GETTER[searchType]; | ||
let items = props[searchType]; | ||
|
||
export const WithMetricsSearchResults = function ( | ||
WrappedComponent, | ||
searchName, | ||
itemsProp, | ||
outputProp, | ||
) { | ||
return class extends Component { | ||
/** | ||
* @private | ||
*/ | ||
render() { | ||
const query = this.props.searchCriteria?.query ?? ""; | ||
let items, searchType; | ||
|
||
const params = queryString.parse(this.props.location.search); | ||
searchType = params["searchType"] || "challenges"; | ||
items = this.props[searchType]; | ||
|
||
let searchResults = this.props[searchType]; | ||
let searchActive = false; | ||
|
||
if (searchType === "challenges" && query) { | ||
searchResults = _filter( | ||
items, | ||
(item) => (item?.name ?? "").toLowerCase().indexOf(query) !== -1, | ||
); | ||
} else if (searchType === "projects" && query) { | ||
searchResults = _filter( | ||
items, | ||
(item) => (item?.displayName ?? "").toLowerCase().indexOf(query) !== -1, | ||
); | ||
} else if (searchType === "users" && query) { | ||
searchResults = _filter( | ||
items, | ||
(item) => (item?.osmProfile?.displayName ?? "").toLowerCase().indexOf(query) !== -1, | ||
); | ||
} | ||
|
||
if (_isEmpty(outputProp)) { | ||
outputProp = itemsProp; | ||
} | ||
outputProp = searchType; | ||
searchName = searchType; | ||
return ( | ||
<WrappedComponent | ||
{...{ | ||
[outputProp]: searchResults, | ||
[`${searchName}SearchActive`]: searchActive, | ||
}} | ||
{..._omit(this.props, outputProp)} | ||
/> | ||
); | ||
const query = props.searchCriteria?.query ?? ""; | ||
if (query && getSearchField) { | ||
items = items.filter((item) => getSearchField(item)?.toLowerCase().includes(query)); | ||
} | ||
|
||
const forwardedProps = { ...props, [searchType]: items }; | ||
return <WrappedComponent {...forwardedProps} />; | ||
}; | ||
}; | ||
} | ||
|
||
export default (WrappedComponent, searchName, itemsProp, outputProp, searchFunction = null) => | ||
WithSearch( | ||
WithMetricsSearchResults(WrappedComponent, searchName, itemsProp, outputProp), | ||
searchName, | ||
searchFunction, | ||
); | ||
export default (WrappedComponent, searchName, itemsProp, searchFunction = null) => | ||
WithSearch(WithMetricsSearchResults(WrappedComponent), searchName, searchFunction); |