Skip to content

Instantly share code, notes, and snippets.

@enkot
Last active July 16, 2019 21:38
Show Gist options
  • Save enkot/ec6cd4f846a5c8d8fd8adc99cecab9d9 to your computer and use it in GitHub Desktop.
Save enkot/ec6cd4f846a5c8d8fd8adc99cecab9d9 to your computer and use it in GitHub Desktop.
import { fetchUserPosts } from '@/api'
const withPostsHOC = WrappedComponent => ({
props: WrappedComponent.props,
data() {
return {
postsIsLoading: false,
fetchedPosts: []
}
},
watch: {
id: {
handler: 'fetchPosts',
immediate: true
}
},
methods: {
async fetchPosts() {
this.postsIsLoading = true
this.fetchedPosts = await fetchUserPosts(this.id)
this.postsIsLoading = false
}
},
computed: {
postsCount() {
return this.fetchedPosts.length
}
},
render(h) {
return h(WrappedComponent, {
props: {
...this.$props,
isLoading: this.postsIsLoading,
posts: this.fetchedPosts,
count: this.postsCount
}
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment