Last active
July 16, 2019 21:38
-
-
Save enkot/ec6cd4f846a5c8d8fd8adc99cecab9d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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