Skip to content

Commit

Permalink
[not verified] VideoPress: minor TS enhancements in the useSearchPara…
Browse files Browse the repository at this point in the history
…ms() hook (#28250)

* set SearchParamNameProp type

* minor jsdoc changes

* changelog
  • Loading branch information
retrofox authored and n3f committed Jan 11, 2023
1 parent 98b772c commit 44dd53e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

VideoPress: minor TS enhancement in the useSearchParams() hook
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import { useLocation, useHistory } from 'react-router-dom';

type SearchParamNameProp = 'page' | 'q';

export const useSearchParams = () => {
const location = useLocation();
const history = useHistory();
Expand All @@ -11,31 +13,31 @@ export const useSearchParams = () => {
/**
* Gets a given parameter from the search query.
*
* @param {string} parameterName - The name of the parameter to get from the query string.
* @param {string} defaultValue - The default value to return if the given parameter is not set on the query string.
* @returns {string} - The value of the parameter if it's set. The defaultValue if the parameter is not set.
* @param {SearchParamNameProp} parameterName - The name of the parameter to get from the query string.
* @param {string} defaultValue - The default value to return if the given parameter is not set on the query string.
* @returns {string|null} The value of the parameter if it's set. The defaultValue if the parameter is not set.
*/
const getParam = ( parameterName: string, defaultValue: string = null ) => {
const getParam = ( parameterName: SearchParamNameProp, defaultValue: string = null ): string => {
return searchParams.has( parameterName ) ? searchParams.get( parameterName ) : defaultValue;
};

/**
* Sets a given parameter on the search query data, but does not refresh the URL.
*
* @param {string} parameterName - The name of the parameter to set on the query string.
* @param {string} value - The value to be set for the parameter on the query string.
* @param {SearchParamNameProp} parameterName - The name of the parameter to set on the query string.
* @param {string} value - The value to be set for the parameter on the query string.
*/
const setParam = ( parameterName: string, value: string = null ) => {
const setParam = ( parameterName: SearchParamNameProp, value: string = null ) => {
searchParams.set( parameterName, value );
};

/**
* Deletes a given parameter from the search query data, which results on removing
* it from the URL when it gets updated.
*
* @param {string} parameterName - The name of the parameter to delete.
* @param {SearchParamNameProp} parameterName - The name of the parameter to delete.
*/
const deleteParam = ( parameterName: string ) => {
const deleteParam = ( parameterName: SearchParamNameProp ) => {
searchParams.delete( parameterName );
};

Expand Down

0 comments on commit 44dd53e

Please sign in to comment.