logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

with_load_more.jsx (3483B)


  1. // eslint-disable-next-line no-unused
  2. import { h } from 'vue'
  3. import isEmpty from 'lodash/isEmpty'
  4. import { getComponentProps } from '../../services/component_utils/component_utils'
  5. import './with_load_more.scss'
  6. import { FontAwesomeIcon as FAIcon } from '@fortawesome/vue-fontawesome'
  7. import { library } from '@fortawesome/fontawesome-svg-core'
  8. import {
  9. faCircleNotch
  10. } from '@fortawesome/free-solid-svg-icons'
  11. library.add(
  12. faCircleNotch
  13. )
  14. const withLoadMore = ({
  15. fetch, // function to fetch entries and return a promise
  16. select, // function to select data from store
  17. unmounted, // function called at "destroyed" lifecycle
  18. childPropName = 'entries', // name of the prop to be passed into the wrapped component
  19. additionalPropNames = [] // additional prop name list of the wrapper component
  20. }) => (WrappedComponent) => {
  21. const originalProps = Object.keys(getComponentProps(WrappedComponent))
  22. const props = originalProps.filter(v => v !== childPropName).concat(additionalPropNames)
  23. return {
  24. props,
  25. data () {
  26. return {
  27. loading: false,
  28. bottomedOut: false,
  29. error: false,
  30. entries: []
  31. }
  32. },
  33. created () {
  34. window.addEventListener('scroll', this.scrollLoad)
  35. if (this.entries.length === 0) {
  36. this.fetchEntries()
  37. }
  38. },
  39. unmounted () {
  40. window.removeEventListener('scroll', this.scrollLoad)
  41. unmounted && unmounted(this.$props, this.$store)
  42. },
  43. methods: {
  44. // Entries is not a computed because computed can't track the dynamic
  45. // selector for changes and won't trigger after fetch.
  46. updateEntries () {
  47. this.entries = select(this.$props, this.$store) || []
  48. },
  49. fetchEntries () {
  50. if (!this.loading) {
  51. this.loading = true
  52. this.error = false
  53. fetch(this.$props, this.$store)
  54. .then((newEntries) => {
  55. this.loading = false
  56. this.bottomedOut = isEmpty(newEntries)
  57. })
  58. .catch(() => {
  59. this.loading = false
  60. this.error = true
  61. })
  62. .finally(() => {
  63. this.updateEntries()
  64. })
  65. }
  66. },
  67. scrollLoad (e) {
  68. const bodyBRect = document.body.getBoundingClientRect()
  69. const height = Math.max(bodyBRect.height, -(bodyBRect.y))
  70. if (this.loading === false &&
  71. this.bottomedOut === false &&
  72. this.$el.offsetHeight > 0 &&
  73. (window.innerHeight + window.pageYOffset) >= (height - 750)
  74. ) {
  75. this.fetchEntries()
  76. }
  77. }
  78. },
  79. render () {
  80. const props = {
  81. ...this.$props,
  82. [childPropName]: this.entries
  83. }
  84. const children = this.$slots
  85. return (
  86. <div class="with-load-more">
  87. <WrappedComponent {...props}>
  88. {children}
  89. </WrappedComponent>
  90. <div class="with-load-more-footer">
  91. {this.error &&
  92. <button onClick={this.fetchEntries} class="button-unstyled -link -fullwidth alert error">
  93. {this.$t('general.generic_error')}
  94. </button>
  95. }
  96. {!this.error && this.loading && <FAIcon spin icon="circle-notch"/>}
  97. {!this.error && !this.loading && !this.bottomedOut && <a onClick={this.fetchEntries} role="button" tabindex="0">{this.$t('general.more')}</a>}
  98. </div>
  99. </div>
  100. )
  101. }
  102. }
  103. }
  104. export default withLoadMore