-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from dlmr/support-async-routes
Adds support for async routes
- Loading branch information
Showing
6 changed files
with
49 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,35 @@ | ||
export default function findRouteByComponent(component, routes) { | ||
import isPlainObject from 'lodash.isplainobject'; | ||
|
||
const isNamedComponents = isPlainObject; | ||
|
||
function searchNamedComponents(component, namedComponents, correspondingRoute, result) { | ||
return Object.keys(namedComponents) | ||
.some(name => { | ||
const isMatch = namedComponents[name] === component; | ||
if (isMatch) { | ||
/* eslint-disable no-param-reassign */ | ||
result.name = name; | ||
result.route = correspondingRoute; | ||
/* eslint-enable no-param-reassign */ | ||
} | ||
return isMatch; | ||
}); | ||
} | ||
|
||
export default function findRouteByComponent(component, matchedRoutes, matchedComponents) { | ||
const result = {}; | ||
for (const route of routes) { | ||
if (route.component === component) { | ||
result.route = route; | ||
return result; | ||
|
||
matchedComponents.some((matchedComponent, i) => { | ||
if (isNamedComponents(matchedComponent)) { | ||
return searchNamedComponents(component, matchedComponent, matchedRoutes[i], result); | ||
} | ||
if (route.components) { | ||
const foundNamedComponent = Object.keys(route.components) | ||
.some(key => { | ||
const found = route.components[key] === component; | ||
if (found) { | ||
result.name = key; | ||
} | ||
return found; | ||
}); | ||
if (foundNamedComponent) { | ||
result.route = route; | ||
return result; | ||
} | ||
|
||
const isMatch = component === matchedComponent; | ||
if (isMatch) { | ||
result.route = matchedRoutes[i]; | ||
} | ||
} | ||
return isMatch; | ||
}); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters