Created
August 14, 2019 01:53
-
-
Save taion/4699590ba5672896d2ef9fc50ef3d3c8 to your computer and use it in GitHub Desktop.
Found Relay <Route>
This file contains hidden or 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
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