import { getParam } from 'utils/helpers'; import queryString from 'query-string'; import React from 'react'; import { withRouter, RouteComponentProps } from 'react-router-dom'; const parse = (location: RouteComponentProps['location']): Query => { const searchStr = location.search; const query = queryString.parse(searchStr); return query; }; interface IQueryResults { [key: string]: string | null; } export type Param = 'to' | 'data' | 'readOnly' | 'tokenSymbol' | 'value' | 'gaslimit' | 'limit'; interface Props { params: Param[]; withQuery(query: IQueryResults): React.ReactElement | null; } interface Query { [key: string]: string; } export const Query = withRouter( class extends React.Component { get injected() { return this.props as Props & RouteComponentProps; } public render() { const { withQuery, params } = this.props; const { location } = this.injected; const query = parse(location); const res = params.reduce((obj, param) => ({ ...obj, [param]: getParam(query, param) }), {}); return withQuery(res); } } );