fix: make ReactStandalone react on props changes

This commit is contained in:
Roman Hotsiy 2018-03-09 21:11:28 +02:00
parent 18ec3ac17b
commit 0cb0af2cae
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0

View File

@ -3,6 +3,7 @@ import { Component } from 'react';
import { AppStore } from '../services/'; import { AppStore } from '../services/';
import { RedocRawOptions } from '../services/RedocNormalizedOptions'; import { RedocRawOptions } from '../services/RedocNormalizedOptions';
import { loadAndBundleSpec } from '../utils'; import { loadAndBundleSpec } from '../utils';
import { OpenAPISpec } from '../types';
interface StoreProviderProps { interface StoreProviderProps {
specUrl?: string; specUrl?: string;
@ -23,6 +24,8 @@ interface StoreProviderState {
export class StoreProvider extends Component<StoreProviderProps, StoreProviderState> { export class StoreProvider extends Component<StoreProviderProps, StoreProviderState> {
store: AppStore; store: AppStore;
private _resolvedSpec: OpenAPISpec;
constructor(props: StoreProviderProps) { constructor(props: StoreProviderProps) {
super(props); super(props);
@ -43,10 +46,21 @@ export class StoreProvider extends Component<StoreProviderProps, StoreProviderSt
}); });
try { try {
const resolvedSpec = await loadAndBundleSpec(spec || specUrl!); this._resolvedSpec = await loadAndBundleSpec(spec || specUrl!);
this.updateStore(this._resolvedSpec, specUrl, options);
} catch (e) {
this.setState({
error: e,
});
}
}
updateStore(resolvedSpec, specUrl, options) {
try {
this.setState({ this.setState({
loading: false, loading: false,
store: new AppStore(resolvedSpec, specUrl, options), store: new AppStore(resolvedSpec, specUrl, options),
error: undefined,
}); });
} catch (e) { } catch (e) {
this.setState({ this.setState({
@ -55,6 +69,16 @@ export class StoreProvider extends Component<StoreProviderProps, StoreProviderSt
} }
} }
componentWillReceiveProps(nextProps) {
if (this.props.specUrl !== nextProps.specUrl || this.props.spec !== nextProps.spec) {
setTimeout(() => this.load(), 0);
return;
}
if (this.props.options !== nextProps.options && this._resolvedSpec) {
this.updateStore(this._resolvedSpec, nextProps.specUrl, nextProps.options);
}
}
setError(e?: Error) { setError(e?: Error) {
this.setState({ this.setState({
error: e, error: e,