fix follow

This commit is contained in:
Shpuld Shpuldson 2020-04-22 15:06:10 +03:00
parent 576ad9750b
commit aa56147322
2 changed files with 13 additions and 13 deletions

View File

@ -37,16 +37,16 @@ export default {
}, },
follow () { follow () {
this.inProgress = true this.inProgress = true
requestFollow(this.user, this.$store).then(() => { requestFollow(this.relationship.id, this.$store).then(() => {
this.inProgress = false this.inProgress = false
}) })
}, },
unfollow () { unfollow () {
const store = this.$store const store = this.$store
this.inProgress = true this.inProgress = true
requestUnfollow(this.user, store).then(() => { requestUnfollow(this.relationship.id, store).then(() => {
this.inProgress = false this.inProgress = false
store.commit('removeStatus', { timeline: 'friends', userId: this.user.id }) store.commit('removeStatus', { timeline: 'friends', userId: this.relationship.id })
}) })
} }
} }

View File

@ -1,27 +1,27 @@
const fetchRelationship = (attempt, user, store) => new Promise((resolve, reject) => { const fetchRelationship = (attempt, userId, store) => new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
store.state.api.backendInteractor.fetchUserRelationship({ id: user.id }) store.state.api.backendInteractor.fetchUserRelationship({ id: userId })
.then((relationship) => { .then((relationship) => {
store.commit('updateUserRelationship', [relationship]) store.commit('updateUserRelationship', [relationship])
return relationship return relationship
}) })
.then((relationship) => resolve([relationship.following, relationship.requested, user.locked, attempt])) .then((relationship) => resolve([relationship.following, relationship.requested, relationship.locked, attempt]))
.catch((e) => reject(e)) .catch((e) => reject(e))
}, 500) }, 500)
}).then(([following, sent, locked, attempt]) => { }).then(([following, sent, locked, attempt]) => {
if (!following && !(locked && sent) && attempt <= 3) { if (!following && !(locked && sent) && attempt <= 3) {
// If we BE reports that we still not following that user - retry, // If we BE reports that we still not following that user - retry,
// increment attempts by one // increment attempts by one
fetchRelationship(++attempt, user, store) fetchRelationship(++attempt, userId, store)
} }
}) })
export const requestFollow = (user, store) => new Promise((resolve, reject) => { export const requestFollow = (userId, store) => new Promise((resolve, reject) => {
store.state.api.backendInteractor.followUser({ id: user.id }) store.state.api.backendInteractor.followUser({ id: userId })
.then((updated) => { .then((updated) => {
store.commit('updateUserRelationship', [updated]) store.commit('updateUserRelationship', [updated])
if (updated.following || (user.locked && user.requested)) { if (updated.following || (updated.locked && updated.requested)) {
// If we get result immediately or the account is locked, just stop. // If we get result immediately or the account is locked, just stop.
resolve() resolve()
return return
@ -34,15 +34,15 @@ export const requestFollow = (user, store) => new Promise((resolve, reject) => {
// don't know that yet. // don't know that yet.
// Recursive Promise, it will call itself up to 3 times. // Recursive Promise, it will call itself up to 3 times.
return fetchRelationship(1, user, store) return fetchRelationship(1, updated, store)
.then(() => { .then(() => {
resolve() resolve()
}) })
}) })
}) })
export const requestUnfollow = (user, store) => new Promise((resolve, reject) => { export const requestUnfollow = (userId, store) => new Promise((resolve, reject) => {
store.state.api.backendInteractor.unfollowUser({ id: user.id }) store.state.api.backendInteractor.unfollowUser({ id: userId })
.then((updated) => { .then((updated) => {
store.commit('updateUserRelationship', [updated]) store.commit('updateUserRelationship', [updated])
resolve({ resolve({