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

145 lines
2.8 KiB
JavaScript
Raw Normal View History

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-04-11 23:29:19 +01:00
const mapDispatchToProps = (dispatch) => ({
onFetch() {
2020-02-22 23:26:23 +00:00
dispatch(fetchBundleRequest())
},
onFetchSuccess() {
2020-02-22 23:26:23 +00:00
dispatch(fetchBundleSuccess())
},
onFetchFail(error) {
2020-02-22 23:26:23 +00:00
dispatch(fetchBundleFail(error))
},
2020-02-22 23:26:23 +00:00
})
2019-07-02 08:10:25 +01:00
2020-02-22 23:26:23 +00:00
const emptyComponent = () => null
const noop = () => { }
2019-07-02 08:10:25 +01:00
2020-02-22 23:26:23 +00:00
export default
@connect(null, mapDispatchToProps)
class Bundle extends PureComponent {
2019-07-02 08:10:25 +01:00
static propTypes = {
fetchComponent: PropTypes.func.isRequired,
loading: PropTypes.func,
error: PropTypes.func,
children: PropTypes.func.isRequired,
renderDelay: PropTypes.number,
onFetch: PropTypes.func,
onFetchSuccess: PropTypes.func,
onFetchFail: PropTypes.func,
}
static defaultProps = {
loading: emptyComponent,
error: emptyComponent,
renderDelay: 0,
onFetch: noop,
onFetchSuccess: noop,
onFetchFail: noop,
}
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
}
}