forked from nishantwrp/gsoc-organizations
-
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.
refactor: use redux initial state to get values from url
Signed-off-by: Nishant Mittal <[email protected]>
- Loading branch information
1 parent
b968330
commit cef9a75
Showing
4 changed files
with
62 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Updates the search params in the browser using JS history API. | ||
* This will not re-render the complete application. Using this API | ||
* may cause the react-router to go out of sync and give false values if | ||
* accessed elsewhere. | ||
* @param {{ [param: string]: string }} params | ||
*/ | ||
export const setSearchParams = (params) => { | ||
if (typeof window !== 'undefined' && window) { | ||
const url = new URL(window.location); | ||
url.search = ""; | ||
for (const [param, value] of Object.entries(params)) { | ||
url.searchParams.set(param, value); | ||
}; | ||
window.history.pushState({}, "", url); | ||
} else { | ||
console.error("error setting search params. window is not defined."); | ||
} | ||
}; | ||
|
||
/** | ||
* Gets the value of the search param in the url. Returns null if doesn't exist. | ||
* @param {string} param | ||
* @returns {string | null} | ||
*/ | ||
export const getSearchParam = (param) => { | ||
if (typeof window !== 'undefined' && window) { | ||
return new URL(window.location).searchParams.get(param); | ||
} else { | ||
console.error("error getting search params. window is not defined."); | ||
return null; | ||
} | ||
}; |