Initial commit from prod-batam
This commit is contained in:
4
platform/app/src/hooks/index.js
Normal file
4
platform/app/src/hooks/index.js
Normal file
@@ -0,0 +1,4 @@
|
||||
import useDebounce from './useDebounce.js';
|
||||
import useSearchParams from './useSearchParams';
|
||||
|
||||
export { useDebounce, useSearchParams };
|
||||
31
platform/app/src/hooks/useDebounce.js
Normal file
31
platform/app/src/hooks/useDebounce.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
/**
|
||||
* See: https://usehooks.com/useDebounce/
|
||||
*
|
||||
* @param {*} value
|
||||
* @param {number} delay - delat in ms
|
||||
*/
|
||||
export default function useDebounce(value, delay) {
|
||||
// State and setters for debounced value
|
||||
const [debouncedValue, setDebouncedValue] = useState(value);
|
||||
|
||||
useEffect(
|
||||
() => {
|
||||
// Update debounced value after delay
|
||||
const handler = setTimeout(() => {
|
||||
setDebouncedValue(value);
|
||||
}, delay);
|
||||
|
||||
// Cancel the timeout if value changes (also on delay change or unmount)
|
||||
// This is how we prevent debounced value from updating if value is changed ...
|
||||
// .. within the delay period. Timeout gets cleared and restarted.
|
||||
return () => {
|
||||
clearTimeout(handler);
|
||||
};
|
||||
},
|
||||
[value, delay] // Only re-call effect if value or delay changes
|
||||
);
|
||||
|
||||
return debouncedValue;
|
||||
}
|
||||
24
platform/app/src/hooks/useSearchParams.ts
Normal file
24
platform/app/src/hooks/useSearchParams.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { useLocation } from 'react-router';
|
||||
|
||||
/**
|
||||
* It returns a URLSearchParams of the query parameters in the URL, where the keys are
|
||||
* either lowercase or maintain their case based on the lowerCaseKeys parameter.
|
||||
* @param {lowerCaseKeys:boolean} true to return lower case keys; false (default) to maintain casing;
|
||||
* @returns {URLSearchParams}
|
||||
*/
|
||||
export default function useSearchParams(options = { lowerCaseKeys: false }) {
|
||||
const { lowerCaseKeys } = options;
|
||||
const searchParams = new URLSearchParams(useLocation().search);
|
||||
|
||||
if (!lowerCaseKeys) {
|
||||
return searchParams;
|
||||
}
|
||||
|
||||
const lowerCaseSearchParams = new URLSearchParams();
|
||||
|
||||
for (const [key, value] of searchParams) {
|
||||
lowerCaseSearchParams.set(key.toLowerCase(), value);
|
||||
}
|
||||
|
||||
return lowerCaseSearchParams;
|
||||
}
|
||||
Reference in New Issue
Block a user