mirror of
https://github.com/Redocly/redoc.git
synced 2025-02-27 07:20:33 +03:00
50 lines
1.0 KiB
TypeScript
50 lines
1.0 KiB
TypeScript
|
import { Component, Children } from 'react';
|
||
|
import * as PropTypes from 'prop-types';
|
||
|
import { AppStore, HistoryService } from '../services/';
|
||
|
|
||
|
interface SpecProps {
|
||
|
specUrl?: string;
|
||
|
spec?: object;
|
||
|
store?: AppStore;
|
||
|
}
|
||
|
|
||
|
export class StoreProvider extends Component<SpecProps, { error?: Error }> {
|
||
|
store: AppStore;
|
||
|
|
||
|
static childContextTypes = {
|
||
|
store: PropTypes.object.isRequired,
|
||
|
};
|
||
|
|
||
|
constructor(props: SpecProps) {
|
||
|
super(props);
|
||
|
this.state = {};
|
||
|
|
||
|
this.store = props.store || new AppStore();
|
||
|
|
||
|
if (!this.store.spec.loaded) {
|
||
|
this.store.spec
|
||
|
.load(props.spec! || props.specUrl)
|
||
|
.then(() => {
|
||
|
HistoryService.emit();
|
||
|
this.setError();
|
||
|
})
|
||
|
.catch(e => this.setError(e));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
setError(e?: Error) {
|
||
|
this.setState({
|
||
|
error: e,
|
||
|
});
|
||
|
}
|
||
|
|
||
|
getChildContext() {
|
||
|
return { store: this.props.store || this.store };
|
||
|
}
|
||
|
|
||
|
render() {
|
||
|
if (this.state.error) throw this.state.error;
|
||
|
return Children.only(this.props.children);
|
||
|
}
|
||
|
}
|