2017-11-14 18:46:50 +03:00
|
|
|
import { Component } from 'react';
|
|
|
|
import { AppStore } from '../services/';
|
2017-11-19 13:51:59 +03:00
|
|
|
import { loadAndBundleSpec } from '../utils';
|
2017-10-12 00:01:37 +03:00
|
|
|
|
|
|
|
interface SpecProps {
|
|
|
|
specUrl?: string;
|
|
|
|
spec?: object;
|
|
|
|
store?: AppStore;
|
2017-11-14 18:46:50 +03:00
|
|
|
|
|
|
|
children?: any;
|
2017-10-12 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 18:46:50 +03:00
|
|
|
interface SpecState {
|
|
|
|
error?: Error;
|
|
|
|
loading: boolean;
|
|
|
|
store?: AppStore;
|
|
|
|
}
|
2017-10-12 00:01:37 +03:00
|
|
|
|
2017-11-14 18:46:50 +03:00
|
|
|
export class StoreProvider extends Component<SpecProps, SpecState> {
|
|
|
|
store: AppStore;
|
2017-10-12 00:01:37 +03:00
|
|
|
|
|
|
|
constructor(props: SpecProps) {
|
|
|
|
super(props);
|
2017-11-14 18:46:50 +03:00
|
|
|
|
|
|
|
this.state = {
|
2017-11-19 13:51:59 +03:00
|
|
|
loading: true,
|
2017-11-14 18:46:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
let { specUrl, spec } = this.props;
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
loading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2017-11-19 13:51:59 +03:00
|
|
|
const resolvedSpec = await loadAndBundleSpec(spec || specUrl!);
|
2017-11-14 18:46:50 +03:00
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
store: new AppStore(resolvedSpec, specUrl),
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
this.setState({
|
|
|
|
error: e,
|
|
|
|
});
|
2017-10-12 00:01:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setError(e?: Error) {
|
|
|
|
this.setState({
|
|
|
|
error: e,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.error) throw this.state.error;
|
2017-11-14 18:46:50 +03:00
|
|
|
return this.props.children(this.state);
|
2017-10-12 00:01:37 +03:00
|
|
|
}
|
|
|
|
}
|