From ee347070473030a8986b8211314061146a2c308b Mon Sep 17 00:00:00 2001 From: Angelina Filippova Date: Fri, 2 Oct 2020 00:18:10 +0300 Subject: [PATCH] Move actions that manage users from Reports to reports module --- src/store/modules/reports.js | 77 +++++++++++++++++++ .../components/ModerateUserDropdown.vue | 18 +++-- src/views/reports/components/Report.vue | 2 +- 3 files changed, 89 insertions(+), 8 deletions(-) diff --git a/src/store/modules/reports.js b/src/store/modules/reports.js index 1f6ae44f..52eb3bc6 100644 --- a/src/store/modules/reports.js +++ b/src/store/modules/reports.js @@ -1,4 +1,11 @@ import { changeState, fetchReports, createNote, deleteNote } from '@/api/reports' +import { + activateUsers, + deactivateUsers, + deleteUsers, + tagUser, + untagUser +} from '@/api/users' const reports = { state: { @@ -34,6 +41,34 @@ const reports = { } }, actions: { + async ActivateUserFromReports({ commit, dispatch, getters, state }, { user, reportId }) { + try { + await activateUsers([user.nickname], getters.authHost, getters.token) + } catch (_e) { + return + } finally { + const updatedReports = state.fetchedReports.map(report => { + const updatedAccount = { ...user, deactivated: false } + return report.id === reportId ? { ...report, account: updatedAccount } : report + }) + commit('SET_REPORTS', updatedReports) + } + dispatch('SuccessMessage') + }, + async AddTagFromReports({ commit, dispatch, getters, state }, { user, tag, reportId }) { + try { + await tagUser([user.nickname], [tag], getters.authHost, getters.token) + } catch (_e) { + return + } finally { + const updatedReports = state.fetchedReports.map(report => { + const updatedAccount = { ...user, tags: [...user.tags, tag] } + return report.id === reportId ? { ...report, account: updatedAccount } : report + }) + commit('SET_REPORTS', updatedReports) + } + dispatch('SuccessMessage') + }, async ChangeReportState({ commit, dispatch, getters, state }, reportsData) { changeState(reportsData, getters.authHost, getters.token) @@ -48,6 +83,34 @@ const reports = { ClearFetchedReports({ commit }) { commit('SET_REPORTS', []) }, + async DeactivateUserFromReports({ commit, dispatch, getters, state }, { user, reportId }) { + try { + await deactivateUsers([user.nickname], getters.authHost, getters.token) + } catch (_e) { + return + } finally { + const updatedReports = state.fetchedReports.map(report => { + const updatedAccount = { ...user, deactivated: true } + return report.id === reportId ? { ...report, account: updatedAccount } : report + }) + commit('SET_REPORTS', updatedReports) + } + dispatch('SuccessMessage') + }, + async DeleteUserFromReports({ commit, dispatch, getters, state }, { user, reportId }) { + try { + await deleteUsers([user.nickname], getters.authHost, getters.token) + } catch (_e) { + return + } finally { + const updatedReports = state.fetchedReports.map(report => { + const updatedAccount = { ...user, deactivated: true } + return report.id === reportId ? { ...report, account: updatedAccount } : report + }) + commit('SET_REPORTS', updatedReports) + } + dispatch('SuccessMessage') + }, async FetchReports({ commit, getters, state }, page) { commit('SET_LOADING', true) const { data } = await fetchReports(state.stateFilter, page, state.pageSize, getters.authHost, getters.token) @@ -64,6 +127,20 @@ const reports = { commit('SET_OPEN_REPORTS_COUNT', data.total) commit('SET_LOADING', false) }, + async RemoveTagFromReports({ commit, dispatch, getters, state }, { user, tag, reportId }) { + try { + await untagUser([user.nickname], [tag], getters.authHost, getters.token) + } catch (_e) { + return + } finally { + const updatedReports = state.fetchedReports.map(report => { + const updatedAccount = { ...user, tags: user.tags.filter(userTag => userTag !== tag) } + return report.id === reportId ? { ...report, account: updatedAccount } : report + }) + commit('SET_REPORTS', updatedReports) + } + dispatch('SuccessMessage') + }, SetReportsFilter({ commit }, filter) { commit('SET_REPORTS_FILTER', filter) }, diff --git a/src/views/reports/components/ModerateUserDropdown.vue b/src/views/reports/components/ModerateUserDropdown.vue index e2c750a6..cf3fb1ad 100644 --- a/src/views/reports/components/ModerateUserDropdown.vue +++ b/src/views/reports/components/ModerateUserDropdown.vue @@ -1,5 +1,5 @@