pleroma-fe/src/hocs/with_load_more/with_load_more.jsx

111 lines
3.4 KiB
React
Raw Normal View History

2021-04-25 10:40:08 +00:00
// eslint-disable-next-line no-unused
import { h } from 'vue'
import isEmpty from 'lodash/isEmpty'
import { getComponentProps } from '../../services/component_utils/component_utils'
2019-02-13 12:13:48 +00:00
import './with_load_more.scss'
import { FontAwesomeIcon as FAIcon } from '@fortawesome/vue-fontawesome'
import { library } from '@fortawesome/fontawesome-svg-core'
import {
faCircleNotch
} from '@fortawesome/free-solid-svg-icons'
library.add(
faCircleNotch
)
const withLoadMore = ({
2019-07-05 07:02:14 +00:00
fetch, // function to fetch entries and return a promise
select, // function to select data from store
2022-03-24 11:50:22 +00:00
unmounted, // function called at "destroyed" lifecycle
2019-07-05 07:02:14 +00:00
childPropName = 'entries', // name of the prop to be passed into the wrapped component
additionalPropNames = [] // additional prop name list of the wrapper component
}) => (WrappedComponent) => {
const originalProps = Object.keys(getComponentProps(WrappedComponent))
2019-02-26 20:26:59 +00:00
const props = originalProps.filter(v => v !== childPropName).concat(additionalPropNames)
2019-02-13 12:13:48 +00:00
return {
2019-02-13 12:13:48 +00:00
props,
data () {
return {
loading: false,
bottomedOut: false,
error: false,
entries: []
2019-02-13 12:13:48 +00:00
}
},
created () {
window.addEventListener('scroll', this.scrollLoad)
if (this.entries.length === 0) {
this.fetchEntries()
2019-02-13 12:13:48 +00:00
}
},
2021-04-25 09:50:17 +00:00
unmounted () {
2019-02-13 12:13:48 +00:00
window.removeEventListener('scroll', this.scrollLoad)
2022-03-24 11:50:22 +00:00
unmounted && unmounted(this.$props, this.$store)
2019-02-13 12:13:48 +00:00
},
methods: {
// Entries is not a computed because computed can't track the dynamic
// selector for changes and won't trigger after fetch.
updateEntries () {
this.entries = select(this.$props, this.$store) || []
},
fetchEntries () {
2019-02-13 12:13:48 +00:00
if (!this.loading) {
this.loading = true
this.error = false
fetch(this.$props, this.$store)
.then((newEntries) => {
this.loading = false
this.bottomedOut = isEmpty(newEntries)
})
.catch((e) => {
console.error(e)
this.loading = false
this.error = true
})
.finally(() => {
this.updateEntries()
})
2019-02-13 12:13:48 +00:00
}
},
scrollLoad (e) {
const bodyBRect = document.body.getBoundingClientRect()
const height = Math.max(bodyBRect.height, -(bodyBRect.y))
if (this.loading === false &&
this.bottomedOut === false &&
this.$el.offsetHeight > 0 &&
(window.innerHeight + window.pageYOffset) >= (height - 750)
) {
this.fetchEntries()
2019-02-13 12:13:48 +00:00
}
}
2019-07-05 07:02:14 +00:00
},
2021-04-25 10:40:08 +00:00
render () {
2019-07-05 07:02:14 +00:00
const props = {
2022-03-18 11:32:36 +00:00
...this.$props,
[childPropName]: this.entries
2019-07-05 07:02:14 +00:00
}
2022-03-18 11:32:36 +00:00
const children = this.$slots
2019-07-05 07:02:14 +00:00
return (
<div class="with-load-more">
<WrappedComponent {...props} >
2019-07-05 07:02:14 +00:00
{children}
</WrappedComponent>
<div class="with-load-more-footer">
2020-11-24 12:52:01 +00:00
{this.error &&
<button onClick={this.fetchEntries} class="button-unstyled -link -fullwidth alert error">
{this.$t('general.generic_error')}
</button>
}
{!this.error && this.loading && <FAIcon spin icon="circle-notch"/>}
2019-07-05 07:02:14 +00:00
{!this.error && !this.loading && !this.bottomedOut && <a onClick={this.fetchEntries}>{this.$t('general.more')}</a>}
</div>
</div>
)
2019-02-13 12:13:48 +00:00
}
}
2019-02-13 12:13:48 +00:00
}
export default withLoadMore