2019-02-13 12:13:48 +00:00
|
|
|
import Vue from 'vue'
|
|
|
|
import filter from 'lodash/filter'
|
2019-02-14 01:59:26 +00:00
|
|
|
import isEmpty from 'lodash/isEmpty'
|
2019-02-13 12:13:48 +00:00
|
|
|
import './with_load_more.scss'
|
|
|
|
|
2019-02-14 03:52:57 +00:00
|
|
|
const withLoadMore = ({ fetch, select, entriesPropName = 'entries' }) => (WrappedComponent) => {
|
|
|
|
const originalProps = WrappedComponent.props || []
|
2019-02-13 12:13:48 +00:00
|
|
|
const props = filter(originalProps, v => v !== 'entries')
|
|
|
|
|
|
|
|
return Vue.component('withLoadMore', {
|
|
|
|
render (createElement) {
|
2019-02-14 01:59:26 +00:00
|
|
|
const props = {
|
|
|
|
props: {
|
|
|
|
...this.$props,
|
|
|
|
[entriesPropName]: this.entries
|
|
|
|
},
|
|
|
|
on: this.$listeners
|
|
|
|
}
|
2019-02-13 12:13:48 +00:00
|
|
|
return (
|
|
|
|
<div class="with-load-more">
|
2019-02-14 03:52:57 +00:00
|
|
|
<WrappedComponent {...props} />
|
2019-02-13 12:13:48 +00:00
|
|
|
<div class="with-load-more-footer">
|
2019-02-14 01:59:26 +00:00
|
|
|
{this.error && <a onClick={this.fetchEntries} class="alert error">{this.$t('general.generic_error')}</a>}
|
2019-02-13 12:13:48 +00:00
|
|
|
{!this.error && this.loading && <i class="icon-spin3 animate-spin"/>}
|
2019-02-14 01:59:26 +00:00
|
|
|
{!this.error && !this.loading && !this.bottomedOut && <a onClick={this.fetchEntries}>{this.$t('general.more')}</a>}
|
2019-02-13 12:13:48 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
props,
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
loading: false,
|
|
|
|
bottomedOut: false,
|
|
|
|
error: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
entries () {
|
2019-02-14 01:59:26 +00:00
|
|
|
return select(this.$props, this.$store) || []
|
2019-02-13 12:13:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
created () {
|
|
|
|
window.addEventListener('scroll', this.scrollLoad)
|
|
|
|
if (this.entries.length === 0) {
|
2019-02-14 01:59:26 +00:00
|
|
|
this.fetchEntries()
|
2019-02-13 12:13:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
destroyed () {
|
|
|
|
window.removeEventListener('scroll', this.scrollLoad)
|
|
|
|
},
|
|
|
|
methods: {
|
2019-02-14 01:59:26 +00:00
|
|
|
fetchEntries () {
|
2019-02-13 12:13:48 +00:00
|
|
|
if (!this.loading) {
|
|
|
|
this.loading = true
|
2019-02-14 01:59:26 +00:00
|
|
|
this.error = false
|
|
|
|
fetch(this.$props, this.$store)
|
|
|
|
.then((newEntries) => {
|
|
|
|
this.loading = false
|
|
|
|
this.bottomedOut = isEmpty(newEntries)
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.loading = false
|
|
|
|
this.error = true
|
|
|
|
})
|
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)
|
|
|
|
) {
|
2019-02-14 01:59:26 +00:00
|
|
|
this.fetchEntries()
|
2019-02-13 12:13:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withLoadMore
|