Create separate constant in state and action for open reports count

This commit is contained in:
Angelina Filippova 2020-07-14 19:19:42 +03:00
parent 49df2deb31
commit 3df6738bc7
3 changed files with 18 additions and 7 deletions

View file

@ -2,12 +2,13 @@ import { changeState, fetchReports, createNote, deleteNote } from '@/api/reports
const reports = { const reports = {
state: { state: {
fetchedReports: [],
totalReportsCount: 0,
currentPage: 1, currentPage: 1,
fetchedReports: [],
loading: true,
openReportsCount: 0,
pageSize: 50, pageSize: 50,
stateFilter: '', stateFilter: '',
loading: true totalReportsCount: 0
}, },
mutations: { mutations: {
SET_LAST_REPORT_ID: (state, id) => { SET_LAST_REPORT_ID: (state, id) => {
@ -16,6 +17,9 @@ const reports = {
SET_LOADING: (state, status) => { SET_LOADING: (state, status) => {
state.loading = status state.loading = status
}, },
SET_OPEN_REPORTS_COUNT: (state, total) => {
state.openReportsCount = total
},
SET_PAGE: (state, page) => { SET_PAGE: (state, page) => {
state.currentPage = page state.currentPage = page
}, },
@ -30,7 +34,7 @@ const reports = {
} }
}, },
actions: { actions: {
async ChangeReportState({ commit, getters, state }, reportsData) { async ChangeReportState({ commit, dispatch, getters, state }, reportsData) {
changeState(reportsData, getters.authHost, getters.token) changeState(reportsData, getters.authHost, getters.token)
const updatedReports = state.fetchedReports.map(report => { const updatedReports = state.fetchedReports.map(report => {
@ -39,6 +43,7 @@ const reports = {
}) })
commit('SET_REPORTS', updatedReports) commit('SET_REPORTS', updatedReports)
dispatch('FetchOpenReportsCount')
}, },
ClearFetchedReports({ commit }) { ClearFetchedReports({ commit }) {
commit('SET_REPORTS', []) commit('SET_REPORTS', [])
@ -52,6 +57,13 @@ const reports = {
commit('SET_PAGE', page) commit('SET_PAGE', page)
commit('SET_LOADING', false) commit('SET_LOADING', false)
}, },
async FetchOpenReportsCount({ commit, getters, state }) {
commit('SET_LOADING', true)
const { data } = await fetchReports('open', state.currentPage, state.pageSize, getters.authHost, getters.token)
commit('SET_OPEN_REPORTS_COUNT', data.total)
commit('SET_LOADING', false)
},
SetReportsFilter({ commit }, filter) { SetReportsFilter({ commit }, filter) {
commit('SET_REPORTS_FILTER', filter) commit('SET_REPORTS_FILTER', filter)
}, },

View file

@ -85,7 +85,7 @@ export default {
return this.basePath === '/invites' ? this.$store.state.app.invitesEnabled : true return this.basePath === '/invites' ? this.$store.state.app.invitesEnabled : true
}, },
normalizedReportsCount() { normalizedReportsCount() {
return numeral(this.$store.state.reports.totalReportsCount).format('0a') return numeral(this.$store.state.reports.openReportsCount).format('0a')
} }
}, },
methods: { methods: {

View file

@ -33,8 +33,7 @@ export default {
} }
}, },
mounted() { mounted() {
this.$store.dispatch('SetReportsFilter', 'open') this.$store.dispatch('FetchOpenReportsCount')
this.$store.dispatch('FetchReports', 1)
} }
} }
</script> </script>