Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Filter emtpy queryParams (#1881)
Browse files Browse the repository at this point in the history
# Summary

Closes #1750 

**Proposal:**
- Filtering `queryParams` when they are empty

## To test
- Copy the link 'https://pr1881--gpswapui.review.gnosisdev.com/#/?referral='
- Empty URL queryParams will be removed.
  • Loading branch information
henrypalacios authored Nov 25, 2021
1 parent d8687e5 commit 443a98f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/custom/hooks/useFilterEmptyQueryParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useEffect } from 'react'
import { useHistory } from 'react-router-dom'
import qs from 'qs'

import useParsedQueryString from 'hooks/useParsedQueryString'

/**
* When query parameter is empty will be filtered
* example: ?referral=&paramFullfilled=123
* result: {paramFullfilled: 123}
*
* @param queryParams The object to check
*/
function filterEmptyQueryValues(queryParams: { [s: string]: unknown }) {
return Object.fromEntries(Object.entries(queryParams).filter(([, v]) => v))
}

export function useFilterEmptyQueryParams() {
const queryParams = useParsedQueryString()
const history = useHistory()

useEffect(() => {
history.replace({
search: qs.stringify(filterEmptyQueryValues(queryParams)),
})
}, [history, queryParams])
}
4 changes: 4 additions & 0 deletions src/custom/pages/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as Sentry from '@sentry/react'
import { Integrations } from '@sentry/tracing'
import { version } from '@src/../package.json'
import { environmentName } from 'utils/environments'
import { useFilterEmptyQueryParams } from 'hooks/useFilterEmptyQueryParams'

const SENTRY_DSN = process.env.REACT_APP_SENTRY_DSN
const SENTRY_TRACES_SAMPLE_RATE = process.env.REACT_APP_SENTRY_TRACES_SAMPLE_RATE
Expand Down Expand Up @@ -60,6 +61,9 @@ function createRedirectExternal(url: string) {
}

export default function App() {
// Dealing with empty URL queryParameters
useFilterEmptyQueryParams()

return (
<Wrapper>
<Switch>
Expand Down

0 comments on commit 443a98f

Please sign in to comment.