From 16313b8293af4f4d26b1a289e8de639822b3819a Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Mon, 25 Jun 2018 13:48:51 +0300 Subject: [PATCH] feat: pass error in init callback + onLoaded for RedocStandalone --- src/components/RedocStandalone.tsx | 7 ++++--- src/components/StoreProvider.ts | 31 ++++++++++++++++++++---------- src/standalone.tsx | 4 ++-- yarn.lock | 4 ++++ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/components/RedocStandalone.tsx b/src/components/RedocStandalone.tsx index 807a7505..ea6743cf 100644 --- a/src/components/RedocStandalone.tsx +++ b/src/components/RedocStandalone.tsx @@ -11,9 +11,10 @@ export interface RedocStandaloneProps { spec?: object; specUrl?: string; options?: RedocRawOptions; + onLoaded?: (e?: Error) => any; } -export class RedocStandalone extends React.Component { +export class RedocStandalone extends React.PureComponent { static propTypes = { spec: (props, _, componentName) => { if (!props.spec && !props.specUrl) { @@ -36,14 +37,14 @@ export class RedocStandalone extends React.Component { }; render() { - const { spec, specUrl, options = {} } = this.props; + const { spec, specUrl, options = {}, onLoaded } = this.props; const hideLoading = options.hideLoading !== undefined; const normalizedOpts = new RedocNormalizedOptions(options); return ( - + {({ loading, store }) => !loading ? ( diff --git a/src/components/StoreProvider.ts b/src/components/StoreProvider.ts index e54c2dc0..df926302 100644 --- a/src/components/StoreProvider.ts +++ b/src/components/StoreProvider.ts @@ -13,22 +13,27 @@ export interface StoreProviderProps { options?: RedocRawOptions; + onLoaded?: (e?: Error) => void; + children: (props: { loading: boolean; store?: AppStore }) => any; } export interface StoreProviderState { error?: Error; loading: boolean; - spec?: any; + resolvedSpec?: any; + prevSpec?: any; prevSpecUrl?: string; } export class StoreProvider extends Component { - static getDerivedStateFromProps(props, state: StoreProviderState) { - if (props.specUrl !== state.prevSpecUrl) { + static getDerivedStateFromProps(nextProps: StoreProviderProps, prevState: StoreProviderState) { + if (nextProps.specUrl !== prevState.prevSpecUrl || nextProps.spec !== prevState.prevSpec) { return { - spec: null, - prevSpecUrl: props.specUrl, + loading: true, + resolvedSpec: null, + prevSpec: nextProps.spec, + prevSpecUrl: nextProps.specUrl, }; } @@ -37,7 +42,7 @@ export class StoreProvider extends Component void, + callback?: (e?: Error) => void, ) { if (element === null) { throw new Error('"element" argument is not provided and tag is not found on the page'); @@ -57,13 +57,13 @@ export function init( RedocStandalone, { spec, + onLoaded: callback, specUrl, options: { ...options, ...parseOptionsFromElement(element) }, }, ['Loading...'], ), element, - callback, ); } diff --git a/yarn.lock b/yarn.lock index 8ba498cc..d1969f5c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5914,6 +5914,10 @@ mem@^1.1.0: dependencies: mimic-fn "^1.0.0" +memoize-one@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-3.1.1.tgz#ef609811e3bc28970eac2884eece64d167830d17" + memory-fs@^0.4.0, memory-fs@~0.4.1: version "0.4.1" resolved "https://registry.yarnpkg.com/memory-fs/-/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552"