routeConfigForPath inlined in Router

This commit is contained in:
NikolaBorislavovHristov 2019-09-13 16:58:27 +03:00
parent 8e5cee3540
commit d94b8cd4f7
2 changed files with 13 additions and 14 deletions

View file

@ -4,7 +4,6 @@ const PropTypes = require('prop-types');
const UrlUtils = require('url');
const Route = require('../Route');
const { RoutesContainerProvider } = require('../RoutesContainerContext');
const routeConfigForPath = require('./routeConfigForPath');
const urlParamsForPath = require('./urlParamsForPath');
const Router = ({ className, onPathNotMatch, ...props }) => {
@ -12,6 +11,17 @@ const Router = ({ className, onPathNotMatch, ...props }) => {
homePath: props.homePath,
viewsConfig: props.viewsConfig
}));
const routeConfigForPath = React.useCallback((path) => {
for (const viewConfig of viewsConfig) {
for (const routeConfig of viewConfig) {
if (typeof path === 'string' && path.match(routeConfig.regexp)) {
return routeConfig;
}
}
}
return null;
}, []);
const [views, setViews] = React.useState(() => {
return Array(viewsConfig.length).fill(null);
});
@ -20,7 +30,7 @@ const Router = ({ className, onPathNotMatch, ...props }) => {
const { pathname, path } = UrlUtils.parse(window.location.hash.slice(1));
if (homePath !== path) {
window.location.replace(`#${homePath}`);
const routeConfig = routeConfigForPath(viewsConfig, pathname);
const routeConfig = routeConfigForPath(pathname);
if (routeConfig) {
window.location = `#${path}`;
}
@ -31,7 +41,7 @@ const Router = ({ className, onPathNotMatch, ...props }) => {
const onLocationHashChange = () => {
const { pathname, query } = UrlUtils.parse(window.location.hash.slice(1));
const queryParams = new URLSearchParams(typeof query === 'string' ? query : '');
const routeConfig = routeConfigForPath(viewsConfig, pathname);
const routeConfig = routeConfigForPath(pathname);
if (!routeConfig) {
if (typeof onPathNotMatch === 'function') {
const component = onPathNotMatch();

View file

@ -1,11 +0,0 @@
module.exports = (viewsConfig, path) => {
if (typeof path === 'string') {
for (const viewConfig of viewsConfig) {
for (const routeConfig of viewConfig) {
if (path.match(routeConfig.regexp)) {
return routeConfig;
}
}
}
}
};