-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor changeUrlQueryString method, update comments
- Loading branch information
Showing
3 changed files
with
26 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
import { Url } from "next/dist/shared/lib/router/router"; | ||
import { NextRouter } from "next/router"; | ||
import { ParsedUrlQueryInput } from "querystring"; | ||
|
||
export const changeUrlQueryString = (query: ParsedUrlQueryInput, router: NextRouter) => { | ||
const { envId, ...restQuery } = query; // get rid of envId | ||
const asPath = { | ||
pathname: router.asPath.split('?')[0], | ||
query: restQuery, | ||
/** | ||
* Helper method for shallow routing. | ||
* Modifies the input query to omit envId, then passes it as the "as" parameter of router.replace. | ||
* | ||
* @param query Parsed query parameters from the router. | ||
* @param router Instance of NextRouter. | ||
* @param asQuery URL to show in the address bar. | ||
*/ | ||
export const changeUrlQueryString = ( | ||
query: ParsedUrlQueryInput, | ||
router: NextRouter, | ||
asQuery?: ParsedUrlQueryInput, | ||
) => { | ||
// | ||
const asPath: Url = { | ||
// ensures only base URL is used for each call (omits envId and all query params) | ||
pathname: router.asPath.split("?")[0], | ||
query: asQuery | ||
}; | ||
|
||
router.replace({ query: query }, asPath, { scroll: false, shallow: true }); | ||
} | ||
}; |
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