admin-fe/src/store/modules/reports.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-02-27 16:29:05 +00:00
import { changeState, fetchReports, createNote, deleteNote } from '@/api/reports'
2019-04-01 21:17:35 +00:00
2019-03-30 15:37:44 +00:00
const reports = {
state: {
currentPage: 1,
fetchedReports: [],
loading: true,
openReportsCount: 0,
pageSize: 50,
2019-05-22 22:16:41 +00:00
stateFilter: '',
totalReportsCount: 0
2019-03-30 15:37:44 +00:00
},
2019-04-01 09:03:11 +00:00
mutations: {
2019-05-22 22:16:41 +00:00
SET_LAST_REPORT_ID: (state, id) => {
state.idOfLastReport = id
2019-04-01 21:17:35 +00:00
},
SET_LOADING: (state, status) => {
state.loading = status
},
SET_OPEN_REPORTS_COUNT: (state, total) => {
state.openReportsCount = total
},
SET_PAGE: (state, page) => {
state.currentPage = page
},
2019-05-22 22:16:41 +00:00
SET_REPORTS: (state, reports) => {
state.fetchedReports = reports
},
SET_REPORTS_COUNT: (state, total) => {
state.totalReportsCount = total
},
2019-05-22 22:16:41 +00:00
SET_REPORTS_FILTER: (state, filter) => {
state.stateFilter = filter
2019-04-01 09:03:11 +00:00
}
},
actions: {
async ChangeReportState({ commit, dispatch, getters, state }, reportsData) {
changeState(reportsData, getters.authHost, getters.token)
2019-11-16 09:31:19 +00:00
const updatedReports = state.fetchedReports.map(report => {
const updatedReportsIds = reportsData.map(({ id }) => id)
return updatedReportsIds.includes(report.id) ? { ...report, state: reportsData[0].state } : report
2019-11-16 09:31:19 +00:00
})
2019-05-24 23:11:54 +00:00
commit('SET_REPORTS', updatedReports)
dispatch('FetchOpenReportsCount')
2019-05-24 23:11:54 +00:00
},
2019-05-26 19:05:40 +00:00
ClearFetchedReports({ commit }) {
commit('SET_REPORTS', [])
},
async FetchReports({ commit, getters, state }, page) {
2019-04-01 21:17:35 +00:00
commit('SET_LOADING', true)
const { data } = await fetchReports(state.stateFilter, page, state.pageSize, getters.authHost, getters.token)
2019-05-26 19:05:40 +00:00
commit('SET_REPORTS', data.reports)
commit('SET_REPORTS_COUNT', data.total)
commit('SET_PAGE', page)
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)
},
2020-07-13 23:44:16 +00:00
SetReportsFilter({ commit }, filter) {
2019-05-26 19:05:40 +00:00
commit('SET_REPORTS_FILTER', filter)
},
2019-12-08 08:26:42 +00:00
CreateReportNote({ commit, getters, state, rootState }, { content, reportID }) {
createNote(content, reportID, getters.authHost, getters.token)
const optimisticNote = {
user: {
avatar: rootState.user.avatar,
nickname: rootState.user.name,
id: rootState.user.id
2019-12-08 08:26:42 +00:00
},
content: content,
created_at: new Date().getTime()
}
const updatedReports = state.fetchedReports.map(report => {
if (report.id === reportID) {
report.notes = [...report.notes, optimisticNote]
}
return report
})
commit('SET_REPORTS', updatedReports)
},
DeleteReportNote({ commit, getters, state }, { noteID, reportID }) {
deleteNote(noteID, reportID, getters.authHost, getters.token)
const updatedReports = state.fetchedReports.map(report => {
if (report.id === reportID) {
report.notes = report.notes.filter(note => note.id !== noteID)
}
return report
})
commit('SET_REPORTS', updatedReports)
2019-04-01 09:03:11 +00:00
}
}
2019-03-30 15:37:44 +00:00
}
export default reports