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-25 09:18:41 +00:00
|
|
|
import { getComponentProps } from '../../services/component_utils/component_utils'
|
2019-02-13 12:13:48 +00:00
|
|
|
import './with_load_more.scss'
|
|
|
|
|
2019-02-14 04:25:21 +00:00
|
|
|
const withLoadMore = ({
|
|
|
|
fetch, // function to fetch entries and return a promise
|
|
|
|
select, // function to select data from store
|
2019-02-25 09:51:23 +00:00
|
|
|
destroy, // function called at "destroyed" lifecycle
|
2019-02-25 07:10:59 +00:00
|
|
|
childPropName = 'entries', // name of the prop to be passed into the wrapped component
|
|
|
|
additionalPropNames = [] // additional prop name list of the wrapper component
|
2019-02-14 04:25:21 +00:00
|
|
|
}) => (WrappedComponent) => {
|
2019-02-25 09:18:41 +00:00
|
|
|
const originalProps = Object.keys(getComponentProps(WrappedComponent))
|
2019-02-25 07:10:59 +00:00
|
|
|
const props = filter(originalProps, v => v !== childPropName).concat(additionalPropNames)
|
2019-02-13 12:13:48 +00:00
|
|
|
|
|
|
|
return Vue.component('withLoadMore', {
|
|
|
|
render (createElement) {
|
2019-02-14 01:59:26 +00:00
|
|
|
const props = {
|
|
|
|
props: {
|
|
|
|
...this.$props,
|
2019-02-14 04:25:21 +00:00
|
|
|
[childPropName]: this.entries
|
2019-02-14 01:59:26 +00:00
|
|
|
},
|
2019-02-14 08:12:52 +00:00
|
|
|
on: this.$listeners,
|
|
|
|
scopedSlots: this.$scopedSlots
|
2019-02-14 01:59:26 +00:00
|
|
|
}
|
2019-02-21 02:50:10 +00:00
|
|
|
const children = Object.entries(this.$slots).map(([key, value]) => createElement('template', { slot: key }, value))
|
2019-02-13 12:13:48 +00:00
|
|
|
return (
|
|
|
|
<div class="with-load-more">
|
2019-02-14 08:12:52 +00:00
|
|
|
<WrappedComponent {...props}>
|
|
|
|
{children}
|
|
|
|
</WrappedComponent>
|
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)
|
2019-02-25 09:51:23 +00:00
|
|
|
destroy && destroy(this.$props, this.$store)
|
2019-02-13 12:13:48 +00:00
|
|
|
},
|
|
|
|
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
|