Skip to content

Instantly share code, notes, and snippets.

@dbismut
Last active April 9, 2018 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbismut/b2e517e6675edac70fbb6dee75798877 to your computer and use it in GitHub Desktop.
Save dbismut/b2e517e6675edac70fbb6dee75798877 to your computer and use it in GitHub Desktop.
Part 1 - The data middleware that will fetch the data
import transitionPath from 'router5-transition-path';
export default routes => router => (toState, fromState) => {
const { toActivate } = transitionPath(toState, fromState);
const onActivateHandlers = toActivate
.map(segment =>
routes.find(r => r.name === segment).onActivate(toState.params)
)
.filter(Boolean);
return Promise.all(onActivateHandlers).then(data => {
const routeData = data.reduce(
(accData, rData) => Object.assign(accData, rData),
{}
);
return { ...toState, data: routeData };
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment