Files

51 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2017-12-18 16:23:31 -05:00
import React from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';
import queryString from 'query-string';
import { getParam } from 'utils/helpers';
2017-12-18 16:23:31 -05:00
const parse = (location: RouteComponentProps<any>['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'
2018-04-15 03:21:33 +05:00
| 'windowSize'
| 'windowStart'
| 'scheduleTimestamp'
| 'timeBounty'
| 'network';
2017-12-18 16:23:31 -05:00
2017-12-19 12:23:57 -05:00
interface Props extends RouteComponentProps<{}> {
2017-12-18 16:23:31 -05:00
params: Param[];
withQuery(query: IQueryResults): React.ReactElement<any> | null;
}
interface Query {
[key: string]: string;
}
2017-12-19 12:23:57 -05:00
export const Query = withRouter<Props>(
2017-12-18 16:23:31 -05:00
class extends React.Component<Props, {}> {
public render() {
2017-12-19 12:23:57 -05:00
const { withQuery, params, location } = this.props;
2017-12-18 16:23:31 -05:00
const query = parse(location);
const res = params.reduce((obj, param) => ({ ...obj, [param]: getParam(query, param) }), {});
return withQuery(res);
}
}
);