Skip to content

Instantly share code, notes, and snippets.

@taion
Created August 14, 2019 01:53
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 taion/4699590ba5672896d2ef9fc50ef3d3c8 to your computer and use it in GitHub Desktop.
Save taion/4699590ba5672896d2ef9fc50ef3d3c8 to your computer and use it in GitHub Desktop.
Found Relay <Route>
import HttpError from 'found/lib/HttpError';
import BaseRoute from 'found/lib/Route';
import React from 'react';
import LoadingIndicator from '@bfly/ui/lib/LoadingIndicator';
function defaultGetDataFrom({ location }) {
return location.action === 'POP' ? 'STORE_OR_NETWORK' : 'STORE_THEN_NETWORK';
}
export function createRender({ prerender, render, renderFetched }) {
return renderArgs => {
const { resolving, Component, props, error } = renderArgs;
if (error) {
// TODO: What if we're not resolving?
throw new HttpError(500);
}
if (prerender && resolving) {
prerender(renderArgs);
}
if (render) {
return render(renderArgs);
}
if (props && renderFetched) {
return renderFetched(renderArgs);
}
if (!Component) {
return null;
}
if (!props) {
return <LoadingIndicator />;
}
return <Component {...props} />;
};
}
export default class Route extends BaseRoute {
constructor(props) {
if (!(props.query || props.getQuery)) {
super(props);
return;
}
super({
getDataFrom: defaultGetDataFrom,
...props,
render: createRender(props),
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment