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 { RedocRawOptions } from '../services/RedocNormalizedOptions';
import { loadAndBundleSpec } from '../utils';
import { OpenAPISpec } from '../types';
interface StoreProviderProps {
specUrl?: string;
@ -23,6 +24,8 @@ interface StoreProviderState {
export class StoreProvider extends Component<StoreProviderProps, StoreProviderState> {
store: AppStore;
private _resolvedSpec: OpenAPISpec;
constructor(props: StoreProviderProps) {
super(props);
@ -43,10 +46,21 @@ export class StoreProvider extends Component<StoreProviderProps, StoreProviderSt
});
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({
loading: false,
store: new AppStore(resolvedSpec, specUrl, options),
error: undefined,
});
} catch (e) {
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) {
this.setState({
error: e,