Skip to content

Commit

Permalink
fix: make ReactStandalone react on props changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Mar 9, 2018
1 parent 18ec3ac commit 0cb0af2
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/components/StoreProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,8 @@ interface StoreProviderState {
export class StoreProvider extends Component<StoreProviderProps, StoreProviderState> {
store: AppStore;

private _resolvedSpec: OpenAPISpec;

constructor(props: StoreProviderProps) {
super(props);

Expand All @@ -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({
Expand All @@ -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,
Expand Down

0 comments on commit 0cb0af2

Please sign in to comment.