forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request datahub-project#21 from acryldata/GabeRefactoringS…
…earchRoutingWithoutExtaCommits [React App] Moving filters into url params + consolidating search routing logic (#2)
- Loading branch information
Showing
10 changed files
with
124 additions
and
52 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
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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const FILTER_URL_PREFIX = 'filter_'; |
13 changes: 13 additions & 0 deletions
13
datahub-web-react/src/components/search/utils/filtersToQueryStringParams.ts
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { FacetFilterInput } from '../../../types.generated'; | ||
import { FILTER_URL_PREFIX } from './constants'; | ||
|
||
// transform filters from [{ filter, value }, { filter, value }] to { filter: [value, value ] } that QueryString can parse | ||
export default function filtersToQueryStringParams(filters: Array<FacetFilterInput> = []) { | ||
return filters.reduce((acc, filter) => { | ||
acc[`${FILTER_URL_PREFIX}${filter.field}`] = [ | ||
...(acc[`${FILTER_URL_PREFIX}${filter.field}`] || []), | ||
filter.value, | ||
]; | ||
return acc; | ||
}, {} as Record<string, string[]>); | ||
} |
35 changes: 35 additions & 0 deletions
35
datahub-web-react/src/components/search/utils/navigateToSearchUrl.ts
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as QueryString from 'query-string'; | ||
import { RouteComponentProps } from 'react-router-dom'; | ||
|
||
import filtersToQueryStringParams from './filtersToQueryStringParams'; | ||
import { EntityType, FacetFilterInput } from '../../../types.generated'; | ||
import { toPathName } from '../../shared/EntityTypeUtil'; | ||
import { PageRoutes } from '../../../conf/Global'; | ||
|
||
export const navigateToSearchUrl = ({ | ||
type: newType, | ||
query: newQuery, | ||
page: newPage = 1, | ||
filters: newFilters, | ||
history, | ||
}: { | ||
type: EntityType; | ||
query?: string; | ||
page?: number; | ||
filters?: Array<FacetFilterInput>; | ||
history: RouteComponentProps['history']; | ||
}) => { | ||
const search = QueryString.stringify( | ||
{ | ||
...filtersToQueryStringParams(newFilters), | ||
query: newQuery, | ||
page: newPage, | ||
}, | ||
{ arrayFormat: 'comma' }, | ||
); | ||
|
||
history.push({ | ||
pathname: `${PageRoutes.SEARCH}/${toPathName(newType)}`, | ||
search, | ||
}); | ||
}; |
27 changes: 27 additions & 0 deletions
27
datahub-web-react/src/components/search/utils/useFilters.ts
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useMemo } from 'react'; | ||
import * as QueryString from 'query-string'; | ||
|
||
import { FILTER_URL_PREFIX } from './constants'; | ||
import { FacetFilterInput } from '../../../types.generated'; | ||
|
||
export default function useFilters(params: QueryString.ParsedQuery<string>): Array<FacetFilterInput> { | ||
return useMemo( | ||
() => | ||
// get all query params | ||
Object.entries(params) | ||
// select only the ones with the `filter_` prefix | ||
.filter(([key, _]) => key.indexOf(FILTER_URL_PREFIX) >= 0) | ||
// transform the filters currently in format [key, [value1, value2]] to [{key: key, value: value1}, { key: key, value: value2}] format that graphql expects | ||
.flatMap(([key, value]) => { | ||
// remove the `filter_` prefix | ||
const field = key.replace(FILTER_URL_PREFIX, ''); | ||
if (!value) return []; | ||
|
||
if (Array.isArray(value)) { | ||
return value.map((distinctValue) => ({ field, value: distinctValue })); | ||
} | ||
return [{ field, value }]; | ||
}), | ||
[params], | ||
); | ||
} |
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