logo

pleroma-fe

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

with_subscription.jsx (2486B)


  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_subscription.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 withSubscription = ({
  15. fetch, // function to fetch entries and return a promise
  16. select, // function to select data from store
  17. childPropName = 'content', // name of the prop to be passed into the wrapped component
  18. additionalPropNames = [] // additional prop name list of the wrapper component
  19. }) => (WrappedComponent) => {
  20. const originalProps = Object.keys(getComponentProps(WrappedComponent))
  21. const props = originalProps.filter(v => v !== childPropName).concat(additionalPropNames)
  22. return {
  23. props: [
  24. ...props,
  25. 'refresh' // boolean saying to force-fetch data whenever created
  26. ],
  27. data () {
  28. return {
  29. loading: false,
  30. error: false
  31. }
  32. },
  33. computed: {
  34. fetchedData () {
  35. return select(this.$props, this.$store)
  36. }
  37. },
  38. created () {
  39. if (this.refresh || isEmpty(this.fetchedData)) {
  40. this.fetchData()
  41. }
  42. },
  43. methods: {
  44. fetchData () {
  45. if (!this.loading) {
  46. this.loading = true
  47. this.error = false
  48. fetch(this.$props, this.$store)
  49. .then(() => {
  50. this.loading = false
  51. })
  52. .catch(() => {
  53. this.error = true
  54. this.loading = false
  55. })
  56. }
  57. }
  58. },
  59. render () {
  60. if (!this.error && !this.loading) {
  61. const props = {
  62. ...this.$props,
  63. [childPropName]: this.fetchedData
  64. }
  65. const children = this.$slots
  66. return (
  67. <div class="with-subscription">
  68. <WrappedComponent {...props}>
  69. {children}
  70. </WrappedComponent>
  71. </div>
  72. )
  73. } else {
  74. return (
  75. <div class="with-subscription-loading">
  76. {this.error
  77. ? <a onClick={this.fetchData} class="alert error">{this.$t('general.generic_error')}</a>
  78. : <FAIcon spin icon="circle-notch"/>
  79. }
  80. </div>
  81. )
  82. }
  83. }
  84. }
  85. }
  86. export default withSubscription