redoc/demo/playground/hmr-playground.tsx

53 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-10-12 00:01:37 +03:00
import * as React from 'react';
import { render } from 'react-dom';
import { AppContainer } from 'react-hot-loader';
2017-10-12 00:01:37 +03:00
// import DevTools from 'mobx-react-devtools';
import { Redoc, RedocProps } from '../../src/components/Redoc/Redoc';
2017-11-19 23:11:40 +03:00
import { AppStore } from '../../src/services/AppStore';
import { loadAndBundleSpec } from '../../src/utils/loadAndBundleSpec';
2017-11-21 14:00:33 +03:00
import { RedocRawOptions } from '../../src/services/RedocNormalizedOptions';
2017-10-12 00:01:37 +03:00
const renderRoot = (Component: typeof Redoc, props: RedocProps) =>
2017-10-12 00:01:37 +03:00
render(
<div>
<AppContainer>
<Component {...props} />
</AppContainer>
</div>,
document.getElementById('example'),
);
const big = window.location.search.indexOf('big') > -1;
2017-11-15 00:40:38 +03:00
const swagger = window.location.search.indexOf('swagger') > -1; //compatibility mode ?
2017-10-12 00:01:37 +03:00
const specUrl = swagger ? 'swagger.yaml' : big ? 'big-openapi.json' : 'openapi.yaml';
let store;
2017-11-21 17:37:31 +03:00
const options: RedocRawOptions = {};
async function init() {
const spec = await loadAndBundleSpec(specUrl);
2017-11-21 14:00:33 +03:00
store = new AppStore(spec, specUrl, options);
renderRoot(Redoc, { store: store });
}
init();
2017-10-12 00:01:37 +03:00
if (module.hot) {
const reload = (reloadStore = false) => () => {
if (reloadStore) {
// create a new Store
store.dispose();
2017-10-12 00:01:37 +03:00
const state = store.toJS();
store = AppStore.fromJS(state);
2017-10-12 00:01:37 +03:00
}
2017-11-21 14:00:33 +03:00
renderRoot(Redoc, { store: store });
2017-10-12 00:01:37 +03:00
};
2017-11-19 23:11:40 +03:00
module.hot.accept(['../../src/components/Redoc/Redoc'], reload());
module.hot.accept(['../../src/services/AppStore'], reload(true));
2017-10-12 00:01:37 +03:00
}