accounts-frontend/packages/app/functions.ts

110 lines
2.9 KiB
TypeScript
Raw Normal View History

import {
connect as reduxConnect,
MapDispatchToPropsParam,
MapStateToPropsParam,
useDispatch,
useSelector,
} from 'react-redux';
import { State, Dispatch } from 'app/types';
export const connect = <TStateProps = {}, TDispatchProps = {}, TOwnProps = {}>(
mapStateToProps?: MapStateToPropsParam<TStateProps, TOwnProps, State> | null | undefined,
dispatchProps?: MapDispatchToPropsParam<TDispatchProps, TOwnProps> | null | undefined,
// mergeProps,
// options,
) => reduxConnect(mapStateToProps, dispatchProps /*, mergeProps, options*/);
export const useReduxDispatch = () => useDispatch<Dispatch>();
export const useReduxSelector = <TSelected>(
selector: (state: State) => TSelected,
equalityFn?: (left: TSelected, right: TSelected) => boolean,
): TSelected => useSelector<State, TSelected>(selector, equalityFn);
let lastId = 0;
2017-07-22 21:27:38 +05:30
export function uniqueId(prefix: string = 'id'): string {
2020-05-24 04:38:24 +05:30
return `${prefix}${++lastId}`;
}
/**
* @param {object} obj
* @param {Array} keys
*
* @returns {object}
*/
2020-05-24 04:38:24 +05:30
export function omit(obj: { [key: string]: any }, keys: Array<string>): { [key: string]: any } {
const newObj = { ...obj };
2020-05-24 04:38:24 +05:30
keys.forEach((key) => {
Reflect.deleteProperty(newObj, key);
});
2020-05-24 04:38:24 +05:30
return newObj;
}
/**
* Asynchronously loads script
*
* @param {string} src
*
* @returns {Promise}
*/
2019-12-07 16:58:52 +05:30
export function loadScript(src: string): Promise<void> {
2020-05-24 04:38:24 +05:30
const script = document.createElement('script');
2020-05-24 04:38:24 +05:30
script.async = true;
script.defer = true;
script.src = src;
2020-05-24 04:38:24 +05:30
return new Promise((resolve, reject) => {
script.onload = () => resolve();
script.onerror = reject;
2020-05-24 04:38:24 +05:30
if (document && document.body) {
document.body.appendChild(script);
}
});
}
2016-08-14 14:31:04 +05:30
/**
* Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing. The function also has a property 'clear'
* that is a function which will clear the timer to prevent previously scheduled executions.
*
* @source https://github.com/component/debounce
*
* @param {Function} function - function to wrap
* @param {number} [timeout=100] - timeout in ms
* @param {bool} [immediate=false] - whether to execute at the beginning
*/
2017-07-22 21:27:38 +05:30
export { default as debounce } from 'debounce';
/**
* @param {string} jwt
*
* @throws {Error} If can not decode token
*
* @returns {object} - decoded jwt payload
*/
export function getJwtPayloads(
2020-05-24 04:38:24 +05:30
jwt: string,
): {
2020-05-24 04:38:24 +05:30
sub: string;
jti: number;
exp: number;
} {
2020-05-24 04:38:24 +05:30
const parts = (jwt || '').split('.');
2020-05-24 04:38:24 +05:30
if (parts.length !== 3) {
throw new Error('Invalid jwt token');
}
2020-05-24 04:38:24 +05:30
try {
return JSON.parse(atob(parts[1]));
} catch (err) {
throw new Error('Can not decode jwt token');
}
}