-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(demos): add initial history service demo (#178)
- Loading branch information
Showing
9 changed files
with
465 additions
and
43 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
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,71 @@ | ||
import {Card, Classes, Label} from '@blueprintjs/core'; | ||
import {FeatureAppDefinition} from '@feature-hub/core'; | ||
import {History, HistoryServiceV1} from '@feature-hub/history-service'; | ||
import {ReactFeatureApp} from '@feature-hub/react'; | ||
import * as React from 'react'; | ||
|
||
interface HistoryConsumerProps { | ||
readonly history: History; | ||
} | ||
|
||
interface HistoryConsumerState { | ||
readonly pathname: string; | ||
} | ||
|
||
class HistoryConsumer extends React.Component< | ||
HistoryConsumerProps, | ||
HistoryConsumerState | ||
> { | ||
public readonly state = {pathname: this.props.history.location.pathname}; | ||
|
||
private unlisten?: () => void; | ||
|
||
public componentDidMount(): void { | ||
const {history} = this.props; | ||
|
||
this.unlisten = history.listen(({pathname}) => this.setState({pathname})); | ||
} | ||
|
||
public componentWillUnmount(): void { | ||
if (this.unlisten) { | ||
this.unlisten(); | ||
} | ||
} | ||
|
||
public render(): React.ReactNode { | ||
const {pathname} = this.state; | ||
|
||
return ( | ||
<Card> | ||
<Label className="bp3-inline"> | ||
Pathname | ||
<input className={Classes.INPUT} value={pathname} disabled /> | ||
</Label> | ||
</Card> | ||
); | ||
} | ||
} | ||
|
||
export const historyConsumerDefinition: FeatureAppDefinition< | ||
ReactFeatureApp, | ||
undefined, | ||
{'s2:history': HistoryServiceV1} | ||
> = { | ||
id: 'test:history-consumer', | ||
|
||
dependencies: { | ||
's2:history': '^1.0' | ||
}, | ||
|
||
create: env => { | ||
const historyService = env.featureServices['s2:history']; | ||
|
||
return { | ||
render: () => ( | ||
<HistoryConsumer history={historyService.createBrowserHistory()} /> | ||
) | ||
}; | ||
} | ||
}; | ||
|
||
export default historyConsumerDefinition; |
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,46 @@ | ||
import {FeatureAppManager, FeatureServiceRegistry} from '@feature-hub/core'; | ||
import { | ||
createRootLocationTransformer, | ||
defineHistoryService | ||
} from '@feature-hub/history-service'; | ||
import {FeatureAppContainer} from '@feature-hub/react'; | ||
import {defineServerRenderer} from '@feature-hub/server-renderer'; | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import {historyConsumerDefinition} from './history-consumer'; | ||
|
||
// tslint:disable | ||
import 'normalize.css/normalize.css'; | ||
import '@blueprintjs/icons/lib/css/blueprint-icons.css'; | ||
import '@blueprintjs/core/lib/css/blueprint.css'; | ||
// tslint:enable | ||
|
||
const registry = new FeatureServiceRegistry(); | ||
|
||
registry.registerProviders( | ||
[ | ||
defineServerRenderer(undefined), | ||
defineHistoryService( | ||
createRootLocationTransformer({consumerPathsQueryParamName: 'test'}) | ||
) | ||
], | ||
'integrator' | ||
); | ||
|
||
const manager = new FeatureAppManager(registry, async () => undefined); | ||
|
||
ReactDOM.render( | ||
<> | ||
<FeatureAppContainer | ||
manager={manager} | ||
featureAppDefinition={historyConsumerDefinition} | ||
idSpecifier="a" | ||
/> | ||
<FeatureAppContainer | ||
manager={manager} | ||
featureAppDefinition={historyConsumerDefinition} | ||
idSpecifier="b" | ||
/> | ||
</>, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {join} from 'path'; | ||
import {Configuration} from 'webpack'; | ||
import {webpackBaseConfig} from '../webpack-base-config'; | ||
|
||
export default [ | ||
{ | ||
...webpackBaseConfig, | ||
entry: join(__dirname, './integrator.tsx'), | ||
output: { | ||
filename: 'integrator.js', | ||
publicPath: '/' | ||
} | ||
} | ||
] as Configuration[]; |
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,35 @@ | ||
import {join} from 'path'; | ||
import TsConfigPathsPlugin from 'tsconfig-paths-webpack-plugin'; | ||
import {Configuration} from 'webpack'; | ||
|
||
export const webpackBaseConfig: Configuration = { | ||
devtool: false, | ||
mode: 'production', | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.tsx?$/, | ||
use: 'ts-loader' | ||
}, | ||
{ | ||
test: /\.css$/, | ||
use: ['style-loader', 'css-loader'] | ||
}, | ||
{ | ||
test: /\.(png|woff|woff2|eot|ttf|svg)$/, | ||
use: 'url-loader' | ||
} | ||
] | ||
}, | ||
resolve: { | ||
extensions: ['.js', '.json', '.ts', '.tsx'], | ||
plugins: [ | ||
new TsConfigPathsPlugin({ | ||
configFile: join(__dirname, '../tsconfig.json') | ||
}) | ||
] | ||
}, | ||
resolveLoader: { | ||
modules: [join(__dirname, '../node_modules'), 'node_modules'] | ||
} | ||
}; |
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.