From ce1d19136de322637695891b11117946664eacd6 Mon Sep 17 00:00:00 2001 From: taehoon Date: Wed, 24 Apr 2019 15:34:30 -0400 Subject: [PATCH] refactor pin/unpin status logic --- src/components/extra_buttons/extra_buttons.js | 8 ++---- src/modules/statuses.js | 26 ++++++++----------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/components/extra_buttons/extra_buttons.js b/src/components/extra_buttons/extra_buttons.js index 71f5946d..55a4caad 100644 --- a/src/components/extra_buttons/extra_buttons.js +++ b/src/components/extra_buttons/extra_buttons.js @@ -24,19 +24,15 @@ const ExtraButtons = { }, pinStatus () { this.refreshPopper() - this.$store.state.api.backendInteractor.pinOwnStatus(this.status.id).then((status) => { + this.$store.dispatch('pinStatus', this.status.id).then((status) => { if (status.error) { this.$emit('onError', status.error) - } else { - this.$store.dispatch('updatePinned', status) } }) }, unpinStatus () { this.refreshPopper() - this.$store.state.api.backendInteractor.unpinOwnStatus(this.status.id).then((status) => { - this.$store.dispatch('updatePinned', status) - }) + this.$store.dispatch('unpinStatus', this.status.id) }, refreshPopper () { this.showPopper = false diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 8edd2e11..9b4eb0f7 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -545,21 +545,17 @@ const statuses = { rootState.api.backendInteractor.fetchPinnedStatuses(userId) .then(statuses => dispatch('addNewStatuses', { statuses, timeline: 'user', userId })) }, - updatePinned ({ rootState, commit }, status) { - commit('setPinned', { status }) - if (status.pinned) { - const statusObj = rootState.statuses.allStatusesObject[status.id] - const user = rootState.users.currentUser - commit('addNewStatuses', { - statuses: [statusObj], - showImmediately: true, - timeline: 'pinned', - user, - userId: user.id - }) - } else { - commit('removeStatus', { timeline: 'pinned', statusId: status.id }) - } + pinStatus ({ rootState, commit }, statusId) { + return rootState.api.backendInteractor.pinOwnStatus(statusId).then((status) => { + if (!status.error) { + commit('setPinned', { status }) + } + return status + }) + }, + unpinStatus ({ rootState, commit }, statusId) { + rootState.api.backendInteractor.unpinOwnStatus(statusId) + .then((status) => commit('setPinned', { status })) }, retweet ({ rootState, commit }, status) { // Optimistic retweeting...