-
Notifications
You must be signed in to change notification settings - Fork 2
/
withVehiclesRouter.js
37 lines (32 loc) · 1 KB
/
withVehiclesRouter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import qs from "query-string";
import * as React from "react";
import { withRouter } from "react-router-dom";
function withVehiclesRouter(BaseComponent) {
class WithVehiclesRouter extends React.Component {
render() {
const { location, match } = this.props;
return (
<BaseComponent
{...this.props}
changeQuery={this._changeQuery}
query={qs.parse(location.search)}
vehicleId={parseInt(match.params.vehicleId, 10)}
/>
);
}
_changeQuery = params => {
const { history, location } = this.props;
const currentParams = qs.parse(location.search);
const newParams = { ...currentParams, ...removeFalseyValues(params) };
history.push({ search: qs.stringify(newParams) });
};
}
return withRouter(WithVehiclesRouter);
}
export default withVehiclesRouter;
function removeFalseyValues(params) {
return Object.entries(params).reduce(
(memo, [key, value]) => ({ ...memo, [key]: value || undefined }),
{},
);
}