mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-21 03:22:11 +00:00
urlParamsForPath moved to separate file
This commit is contained in:
parent
49df82a2da
commit
aabca44a29
2 changed files with 16 additions and 10 deletions
|
|
@ -7,6 +7,7 @@ const deepEqual = require('deep-equal');
|
||||||
const { RouteFocusedProvider } = require('../RouteFocusedContext');
|
const { RouteFocusedProvider } = require('../RouteFocusedContext');
|
||||||
const Route = require('../Route');
|
const Route = require('../Route');
|
||||||
const routeConfigForPath = require('./routeConfigForPath');
|
const routeConfigForPath = require('./routeConfigForPath');
|
||||||
|
const urlParamsForPath = require('./urlParamsForPath');
|
||||||
|
|
||||||
const Router = ({ className, onPathNotMatch, ...props }) => {
|
const Router = ({ className, onPathNotMatch, ...props }) => {
|
||||||
const { homePath, viewsConfig } = React.useMemo(() => ({
|
const { homePath, viewsConfig } = React.useMemo(() => ({
|
||||||
|
|
@ -52,16 +53,7 @@ const Router = ({ className, onPathNotMatch, ...props }) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const matches = pathname.match(routeConfig.regexp);
|
const urlParams = urlParamsForPath(routeConfig, pathname);
|
||||||
const urlParams = routeConfig.urlParamsNames.reduce((urlParams, name, index) => {
|
|
||||||
if (Array.isArray(matches) && typeof matches[index + 1] === 'string') {
|
|
||||||
urlParams[name] = decodeURIComponent(matches[index + 1]);
|
|
||||||
} else {
|
|
||||||
urlParams[name] = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return urlParams;
|
|
||||||
}, {});
|
|
||||||
const routeViewIndex = viewsConfig.findIndex((vc) => vc.includes(routeConfig));
|
const routeViewIndex = viewsConfig.findIndex((vc) => vc.includes(routeConfig));
|
||||||
const routeIndex = viewsConfig[routeViewIndex].findIndex((rc) => rc === routeConfig);
|
const routeIndex = viewsConfig[routeViewIndex].findIndex((rc) => rc === routeConfig);
|
||||||
setViews((views) => {
|
setViews((views) => {
|
||||||
|
|
|
||||||
14
src/router/Router/urlParamsForPath.js
Normal file
14
src/router/Router/urlParamsForPath.js
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
const urlParamsForPath = (routeConfig, path) => {
|
||||||
|
const matches = path.match(routeConfig.regexp);
|
||||||
|
return routeConfig.urlParamsNames.reduce((urlParams, name, index) => {
|
||||||
|
if (Array.isArray(matches) && typeof matches[index + 1] === 'string') {
|
||||||
|
urlParams[name] = decodeURIComponent(matches[index + 1]);
|
||||||
|
} else {
|
||||||
|
urlParams[name] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return urlParams;
|
||||||
|
}, {});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = urlParamsForPath;
|
||||||
Loading…
Reference in a new issue