gab-social/app/javascript/gabsocial/features/ui/util/bundle.js

147 lines
2.9 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
2020-11-25 21:22:37 +00:00
import noop from 'lodash.noop'
2020-02-22 23:26:23 +00:00
import {
fetchBundleRequest,
fetchBundleSuccess,
2020-04-28 06:33:58 +01:00
fetchBundleFail,
2020-02-22 23:26:23 +00:00
} from '../../../actions/bundles'
2020-02-22 23:26:23 +00:00
const emptyComponent = () => null
2019-07-02 08:10:25 +01:00
class Bundle extends React.PureComponent {
2019-07-02 08:10:25 +01:00
static cache = new Map
state = {
mod: undefined,
forceRender: false,
}
componentWillMount() {
2020-02-22 23:26:23 +00:00
this.load(this.props)
2019-07-02 08:10:25 +01:00
}
componentWillReceiveProps(nextProps) {
if (nextProps.fetchComponent !== this.props.fetchComponent) {
2020-02-22 23:26:23 +00:00
this.load(nextProps)
2019-07-02 08:10:25 +01:00
}
}
2020-02-22 23:26:23 +00:00
componentWillUnmount() {
2019-07-02 08:10:25 +01:00
if (this.timeout) {
2020-02-22 23:26:23 +00:00
clearTimeout(this.timeout)
2019-07-02 08:10:25 +01:00
}
}
load = (props) => {
2020-02-22 23:26:23 +00:00
const {
fetchComponent,
onFetch,
onFetchSuccess,
onFetchFail,
renderDelay
} = props || this.props
const cachedMod = Bundle.cache.get(fetchComponent)
2019-07-02 08:10:25 +01:00
if (fetchComponent === undefined) {
2020-02-22 23:26:23 +00:00
this.setState({
mod: null
})
return Promise.resolve()
2019-07-02 08:10:25 +01:00
}
2020-02-22 23:26:23 +00:00
onFetch()
2019-07-02 08:10:25 +01:00
if (cachedMod) {
2020-02-22 23:26:23 +00:00
this.setState({
mod: cachedMod.default
})
onFetchSuccess()
return Promise.resolve()
2019-07-02 08:10:25 +01:00
}
2020-02-22 23:26:23 +00:00
this.setState({
mod: undefined
})
2019-07-02 08:10:25 +01:00
if (renderDelay !== 0) {
2020-02-22 23:26:23 +00:00
this.timestamp = new Date()
this.timeout = setTimeout(() => this.setState({
forceRender: true
}), renderDelay)
2019-07-02 08:10:25 +01:00
}
return fetchComponent()
.then((mod) => {
2020-02-22 23:26:23 +00:00
Bundle.cache.set(fetchComponent, mod)
this.setState({
mod: mod.default
})
onFetchSuccess()
2019-07-02 08:10:25 +01:00
})
.catch((error) => {
2020-02-22 23:26:23 +00:00
this.setState({
mod: null
})
onFetchFail(error)
})
2019-07-02 08:10:25 +01:00
}
render() {
2020-02-22 23:26:23 +00:00
const {
loading: LoadingComponent,
error: ErrorComponent,
children,
renderDelay
} = this.props
const { mod, forceRender } = this.state
const elapsed = this.timestamp ? (new Date() - this.timestamp) : renderDelay
2019-07-02 08:10:25 +01:00
if (mod === undefined) {
2020-02-22 23:26:23 +00:00
return (elapsed >= renderDelay || forceRender) ? <LoadingComponent /> : null
} else if (mod === null) {
2020-02-22 23:26:23 +00:00
return <ErrorComponent onRetry={this.load} />
2019-07-02 08:10:25 +01:00
}
2020-02-22 23:26:23 +00:00
return children(mod)
2019-07-02 08:10:25 +01:00
}
}
const mapDispatchToProps = (dispatch) => ({
onFetch() {
dispatch(fetchBundleRequest())
},
onFetchSuccess() {
dispatch(fetchBundleSuccess())
},
onFetchFail(error) {
dispatch(fetchBundleFail(error))
},
})
Bundle.propTypes = {
fetchComponent: PropTypes.func,
loading: PropTypes.func,
error: PropTypes.func,
children: PropTypes.func.isRequired,
renderDelay: PropTypes.number,
onFetch: PropTypes.func,
onFetchSuccess: PropTypes.func,
onFetchFail: PropTypes.func,
}
Bundle.defaultProps = {
loading: emptyComponent,
error: emptyComponent,
renderDelay: 0,
onFetch: noop,
onFetchSuccess: noop,
onFetchFail: noop,
}
export default connect(null, mapDispatchToProps)(Bundle)