Query.tsx
1 import React from 'react'; 2 import { withRouter, RouteComponentProps } from 'react-router-dom'; 3 import queryString from 'query-string'; 4 5 import { getParam } from 'utils/helpers'; 6 7 const parse = (location: RouteComponentProps<any>['location']): Query => { 8 const searchStr = location.search; 9 const query = queryString.parse(searchStr); 10 return query; 11 }; 12 13 interface IQueryResults { 14 [key: string]: string | null; 15 } 16 17 export type Param = 18 | 'to' 19 | 'data' 20 | 'readOnly' 21 | 'tokenSymbol' 22 | 'value' 23 | 'gaslimit' 24 | 'limit' 25 | 'windowSize' 26 | 'windowStart' 27 | 'scheduleTimestamp' 28 | 'timeBounty' 29 | 'network'; 30 31 interface Props extends RouteComponentProps<{}> { 32 params: Param[]; 33 withQuery(query: IQueryResults): React.ReactElement<any> | null; 34 } 35 36 interface Query { 37 [key: string]: string; 38 } 39 40 export const Query = withRouter<Props>( 41 class extends React.Component<Props, {}> { 42 public render() { 43 const { withQuery, params, location } = this.props; 44 const query = parse(location); 45 const res = params.reduce((obj, param) => ({ ...obj, [param]: getParam(query, param) }), {}); 46 47 return withQuery(res); 48 } 49 } 50 );