redoc/demo/playground/hmr-playground.tsx

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-10-12 00:01:37 +03:00
import * as React from 'react';
import { render } from 'react-dom';
// tslint:disable-next-line
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';
2017-11-21 14:00:33 +03:00
import { RedocRawOptions } from '../../src/services/RedocNormalizedOptions';
2018-01-22 21:30:53 +03:00
import { loadAndBundleSpec } from '../../src/utils/loadAndBundleSpec';
2017-10-12 00:01:37 +03:00
const renderRoot = (props: RedocProps) =>
2017-10-12 00:01:37 +03:00
render(
<AppContainer>
<Redoc {...props} />
</AppContainer>,
2017-10-12 00:01:37 +03:00
document.getElementById('example'),
);
const big = window.location.search.indexOf('big') > -1;
const swagger = window.location.search.indexOf('swagger') > -1;
2017-10-12 00:01:37 +03:00
const userUrl = window.location.search.match(/url=(.*)$/);
const specUrl =
(userUrl && userUrl[1]) || (swagger ? 'swagger.yaml' : big ? 'big-openapi.json' : 'openapi.yaml');
let store;
const options: RedocRawOptions = { nativeScrollbars: false, maxDisplayedEnumValues: 3 };
async function init() {
const spec = await loadAndBundleSpec(specUrl);
2017-11-21 14:00:33 +03:00
store = new AppStore(spec, specUrl, options);
renderRoot({ store });
}
init();
2017-10-12 00:01:37 +03:00
if (module.hot) {
2018-03-06 14:10:08 +03:00
const reload = (reloadStore = false) => async () => {
2017-10-12 00:01:37 +03:00
if (reloadStore) {
// create a new Store
store.dispose();
2017-10-12 00:01:37 +03:00
2018-03-06 14:10:08 +03:00
const state = await store.toJS();
store = AppStore.fromJS(state);
2017-10-12 00:01:37 +03:00
}
renderRoot({ 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
}