Merge branch 'bundle-tsx' into 'develop'

Bundle: convert to TSX

See merge request soapbox-pub/soapbox-fe!1763
merge-requests/1762/merge
Alex Gleason 2022-09-01 17:03:34 +00:00
commit 3a63ce5270
2 zmienionych plików z 28 dodań i 18 usunięć

Wyświetl plik

@ -1,21 +1,29 @@
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>,
loading: React.ComponentType,
error: React.ComponentType<{ onRetry: (props: BundleProps) => void }>,
children: (mod: any) => React.ReactNode,
renderDelay?: number,
onFetch: () => void,
onFetchSuccess: () => void,
onFetchFail: (error: any) => void,
}
static propTypes = { interface BundleState {
fetchComponent: PropTypes.func.isRequired, mod: any,
loading: PropTypes.func, forceRender: boolean,
error: PropTypes.func, }
children: PropTypes.func.isRequired,
renderDelay: PropTypes.number, /** Fetches and renders an async component. */
onFetch: PropTypes.func, class Bundle extends React.PureComponent<BundleProps, BundleState> {
onFetchSuccess: PropTypes.func,
onFetchFail: PropTypes.func, timeout: NodeJS.Timeout | undefined;
} timestamp: Date | undefined;
static defaultProps = { static defaultProps = {
loading: emptyComponent, loading: 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) {

Wyświetl plik

@ -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));
}, },
}); });