-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): allow custom feature app rendering
- Loading branch information
Showing
13 changed files
with
1,332 additions
and
129 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
packages/demos/src/react-loading-and-error-ui/feature-app.tsx
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import {Text} from '@blueprintjs/core'; | ||
import {FeatureAppDefinition} from '@feature-hub/core'; | ||
import {ReactFeatureApp} from '@feature-hub/react'; | ||
import * as React from 'react'; | ||
|
||
const getFakeServerLag = async (ms: number) => | ||
new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
async function fetchDataFromServer(): Promise<string> { | ||
await getFakeServerLag(3000); | ||
|
||
if (Math.random() > 0.5) { | ||
throw Error(`Server Error: Bad luck! | ||
For performance reasons this server flips a coin to consider which calls to answer. | ||
Reload the demo to try again.`); | ||
} | ||
|
||
return 'You got lucky! Have some juicy server data. Reload the example for a chance at an error.'; | ||
} | ||
|
||
interface AppProps { | ||
loadingDone: () => void; | ||
loadingError: (err: Error) => void; | ||
} | ||
|
||
const App: React.FunctionComponent<AppProps> = ({ | ||
loadingDone, | ||
loadingError | ||
}) => { | ||
const [serverResponse, setServerResponse] = React.useState<string | null>( | ||
null | ||
); | ||
|
||
React.useEffect(() => { | ||
fetchDataFromServer() | ||
.then(setServerResponse) | ||
.then(loadingDone) | ||
.catch(loadingError); | ||
}, [loadingDone, loadingError]); | ||
|
||
if (!serverResponse) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Text> | ||
<span>The feature app is ready to display, after server responded:</span> | ||
<pre>{serverResponse}</pre> | ||
</Text> | ||
); | ||
}; | ||
|
||
const featureAppDefinition: FeatureAppDefinition<ReactFeatureApp> = { | ||
create: () => { | ||
let resolve: () => void; | ||
let reject: (err: Error) => void; | ||
|
||
const loadingPromise: Promise<void> = new Promise( | ||
(res, rej) => ([resolve, reject] = [res, rej]) | ||
); | ||
|
||
return { | ||
loadingPromise, | ||
render: () => <App loadingDone={resolve} loadingError={reject} /> | ||
}; | ||
} | ||
}; | ||
|
||
export default featureAppDefinition; |
56 changes: 56 additions & 0 deletions
56
packages/demos/src/react-loading-and-error-ui/integrator.tsx
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {Callout, Card, H4, Intent, Spinner} from '@blueprintjs/core'; | ||
import {createFeatureHub} from '@feature-hub/core'; | ||
import {defineExternals, loadAmdModule} from '@feature-hub/module-loader-amd'; | ||
import {FeatureAppLoader, FeatureHubContextProvider} from '@feature-hub/react'; | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import '../blueprint-css'; | ||
|
||
defineExternals({react: React}); | ||
|
||
const {featureAppManager} = createFeatureHub('test:integrator', { | ||
moduleLoader: loadAmdModule, | ||
providedExternals: {react: process.env.REACT_VERSION as string} | ||
}); | ||
|
||
function ErrorUi({error}: {error: Error}): JSX.Element { | ||
return ( | ||
<Callout intent={Intent.DANGER}> | ||
<H4>Example Error UI</H4> | ||
<p>{error.message}</p> | ||
</Callout> | ||
); | ||
} | ||
|
||
ReactDOM.render( | ||
<div style={{padding: '20px'}}> | ||
<FeatureHubContextProvider value={{featureAppManager}}> | ||
<FeatureAppLoader | ||
featureAppId="test:invalid" | ||
src="feature-app.umd.js" | ||
onError={console.warn} | ||
> | ||
{({error, featureAppNode, loading}) => { | ||
if (error) { | ||
return <ErrorUi error={error} />; | ||
} | ||
|
||
return ( | ||
<Card> | ||
<div style={{display: loading ? 'none' : 'initial'}}> | ||
{featureAppNode} | ||
</div> | ||
{loading && ( | ||
<div> | ||
<Spinner /> | ||
Example Loading UI… | ||
</div> | ||
)} | ||
</Card> | ||
); | ||
}} | ||
</FeatureAppLoader> | ||
</FeatureHubContextProvider> | ||
</div>, | ||
document.querySelector('main') | ||
); |
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
Oops, something went wrong.