aretherecookies-mobile/js/enhancers/routeEnhancers.js
2020-03-29 19:47:33 +00:00

78 lines
1.5 KiB
JavaScript

import { getContext, withProps, compose } from 'recompose';
import { shape, func, string } from 'prop-types';
import { path, pathOr } from 'ramda';
import { getViewMode } from '../helpers/RouteHelpers';
export type routeMatch = {
isExact: boolean,
path: string,
route: string,
params: { [string]: any },
};
export type routeLocation = {
pathname: string,
search: string,
hash: string,
};
export type routerContext = {
route: {
location: routeLocation,
match: routeMatch,
},
history: {
push: Function,
replace: Function,
goBack: Function,
},
};
export const withRouterContext = getContext({
router: shape({
route: shape({
location: shape({
pathname: string,
search: string,
hash: string,
}),
}),
history: shape({
push: func.isRequired,
replace: func.isRequired,
goBack: func.isRequired,
}).isRequired,
}).isRequired,
});
export const withViewMode = withProps((props: Object) => {
return {
viewMode: getViewMode(props),
};
});
export const withPushRoute = withProps((props: Object) => {
return {
pushRoute: path(['router', 'history', 'push'], props),
};
});
export const withReplaceRoute = compose(
withRouterContext,
withProps((props: Object) => {
return {
replaceRoute: path(['router', 'history', 'replace'], props),
};
})
);
export const withCurrentPath = compose(
withRouterContext,
withProps(props => {
const { pathname, search } = pathOr({}, ['router', 'route', 'location'], props);
return {
currentPath: `${pathname}${search || '?'}`,
};
})
);