update follower count on follow/unfollow

This commit is contained in:
Vasily Belolapotkov 2019-09-09 10:01:46 +03:00
parent 37f281b441
commit beb78249f6
2 changed files with 20 additions and 13 deletions

View File

@ -59,16 +59,15 @@ export default {
this.hovered = true
}
},
toggle() {
async toggle() {
const follow = !this.isFollowed
const mutation = follow ? 'follow' : 'unfollow'
this.hovered = false
this.$emit('optimistic', follow)
this.$apollo
.mutate({
try {
await this.$apollo.mutate({
mutation: gql`
mutation($id: ID!) {
${mutation}(id: $id, type: User)
@ -78,13 +77,11 @@ export default {
id: this.followId,
},
})
.then(res => {
// this.$emit('optimistic', follow ? res.data.follow : follow)
this.$emit('update', follow)
})
.catch(() => {
this.$emit('optimistic', !follow)
})
this.$emit('update', follow)
} catch {
this.$emit('optimistic', !follow)
}
},
},
}

View File

@ -79,8 +79,8 @@
<hc-follow-button
:follow-id="user.id"
:is-followed="user.followedByCurrentUser"
@optimistic="follow => (user.followedByCurrentUser = follow)"
@update="follow => (user.followedByCurrentUser = follow)"
@optimistic="optimisticFollow"
@update="updateFollow"
/>
</ds-flex-item>
<ds-flex-item :width="{ base: 1 }">
@ -135,6 +135,16 @@ export default {
return name || this.$t('profile.userAnonym')
},
},
methods: {
optimisticFollow(follow) {
const inc = follow ? 1 : -1
this.user.followedByCurrentUser = follow
this.user.followedByCount += inc
},
updateFollow(follow) {
this.user.followedByCurrentUser = follow
},
},
}
</script>