routeConfigForPath moved to a separate file

This commit is contained in:
NikolaBorislavovHristov 2019-12-05 18:14:39 +02:00
parent 6e76843246
commit 49df82a2da
2 changed files with 18 additions and 15 deletions

View file

@ -6,23 +6,13 @@ const UrlUtils = require('url');
const deepEqual = require('deep-equal');
const { RouteFocusedProvider } = require('../RouteFocusedContext');
const Route = require('../Route');
const routeConfigForPath = require('./routeConfigForPath');
const Router = ({ className, onPathNotMatch, ...props }) => {
const [{ homePath, viewsConfig }] = React.useState(() => ({
const { homePath, viewsConfig } = React.useMemo(() => ({
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);
});
@ -31,7 +21,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(pathname);
const routeConfig = routeConfigForPath(viewsConfig, pathname);
if (routeConfig) {
window.location = `#${path}`;
}
@ -42,7 +32,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(pathname);
const routeConfig = routeConfigForPath(viewsConfig, pathname);
if (!routeConfig) {
if (typeof onPathNotMatch === 'function') {
const component = onPathNotMatch();

View file

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