Merge branch 'bundle-tsx' into 'develop'
Bundle: convert to TSX See merge request soapbox-pub/soapbox-fe!1763
This commit is contained in:
commit
3a63ce5270
|
@ -1,22 +1,30 @@
|
||||||
import PropTypes from 'prop-types';
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
const emptyComponent = () => null;
|
const emptyComponent = () => null;
|
||||||
const noop = () => { };
|
const noop = () => { };
|
||||||
|
|
||||||
class Bundle extends React.PureComponent {
|
interface BundleProps {
|
||||||
|
fetchComponent: () => Promise<any>,
|
||||||
static propTypes = {
|
loading: React.ComponentType,
|
||||||
fetchComponent: PropTypes.func.isRequired,
|
error: React.ComponentType<{ onRetry: (props: BundleProps) => void }>,
|
||||||
loading: PropTypes.func,
|
children: (mod: any) => React.ReactNode,
|
||||||
error: PropTypes.func,
|
renderDelay?: number,
|
||||||
children: PropTypes.func.isRequired,
|
onFetch: () => void,
|
||||||
renderDelay: PropTypes.number,
|
onFetchSuccess: () => void,
|
||||||
onFetch: PropTypes.func,
|
onFetchFail: (error: any) => void,
|
||||||
onFetchSuccess: PropTypes.func,
|
|
||||||
onFetchFail: PropTypes.func,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface BundleState {
|
||||||
|
mod: any,
|
||||||
|
forceRender: boolean,
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Fetches and renders an async component. */
|
||||||
|
class Bundle extends React.PureComponent<BundleProps, BundleState> {
|
||||||
|
|
||||||
|
timeout: NodeJS.Timeout | undefined;
|
||||||
|
timestamp: Date | undefined;
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
loading: emptyComponent,
|
loading: emptyComponent,
|
||||||
error: emptyComponent,
|
error: emptyComponent,
|
||||||
|
@ -37,7 +45,7 @@ class Bundle extends React.PureComponent {
|
||||||
this.load(this.props);
|
this.load(this.props);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps: BundleProps) {
|
||||||
if (nextProps.fetchComponent !== this.props.fetchComponent) {
|
if (nextProps.fetchComponent !== this.props.fetchComponent) {
|
||||||
this.load(nextProps);
|
this.load(nextProps);
|
||||||
}
|
}
|
||||||
|
@ -49,7 +57,7 @@ class Bundle extends React.PureComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
load = (props) => {
|
load = (props: BundleProps) => {
|
||||||
const { fetchComponent, onFetch, onFetchSuccess, onFetchFail, renderDelay } = props || this.props;
|
const { fetchComponent, onFetch, onFetchSuccess, onFetchFail, renderDelay } = props || this.props;
|
||||||
const cachedMod = Bundle.cache.get(fetchComponent);
|
const cachedMod = Bundle.cache.get(fetchComponent);
|
||||||
|
|
||||||
|
@ -88,10 +96,10 @@ class Bundle extends React.PureComponent {
|
||||||
render() {
|
render() {
|
||||||
const { loading: Loading, error: Error, children, renderDelay } = this.props;
|
const { loading: Loading, error: Error, children, renderDelay } = this.props;
|
||||||
const { mod, forceRender } = this.state;
|
const { mod, forceRender } = this.state;
|
||||||
const elapsed = this.timestamp ? (new Date() - this.timestamp) : renderDelay;
|
const elapsed = this.timestamp ? ((new Date()).getTime() - this.timestamp.getTime()) : renderDelay!;
|
||||||
|
|
||||||
if (mod === undefined) {
|
if (mod === undefined) {
|
||||||
return (elapsed >= renderDelay || forceRender) ? <Loading /> : null;
|
return (elapsed >= renderDelay! || forceRender) ? <Loading /> : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mod === null) {
|
if (mod === null) {
|
|
@ -3,14 +3,16 @@ import { connect } from 'react-redux';
|
||||||
import { fetchBundleRequest, fetchBundleSuccess, fetchBundleFail } from '../../../actions/bundles';
|
import { fetchBundleRequest, fetchBundleSuccess, fetchBundleFail } from '../../../actions/bundles';
|
||||||
import Bundle from '../components/bundle';
|
import Bundle from '../components/bundle';
|
||||||
|
|
||||||
const mapDispatchToProps = dispatch => ({
|
import type { AppDispatch } from 'soapbox/store';
|
||||||
|
|
||||||
|
const mapDispatchToProps = (dispatch: AppDispatch) => ({
|
||||||
onFetch() {
|
onFetch() {
|
||||||
dispatch(fetchBundleRequest());
|
dispatch(fetchBundleRequest());
|
||||||
},
|
},
|
||||||
onFetchSuccess() {
|
onFetchSuccess() {
|
||||||
dispatch(fetchBundleSuccess());
|
dispatch(fetchBundleSuccess());
|
||||||
},
|
},
|
||||||
onFetchFail(error) {
|
onFetchFail(error: any) {
|
||||||
dispatch(fetchBundleFail(error));
|
dispatch(fetchBundleFail(error));
|
||||||
},
|
},
|
||||||
});
|
});
|
Loading…
Reference in New Issue