add status prop to store's history entries

This commit is contained in:
axolotle 2021-02-15 15:21:26 +01:00
parent 1944c7cc2f
commit 56a3b29f65
2 changed files with 15 additions and 3 deletions

View file

@ -32,7 +32,7 @@ async function _getResponseData (response) {
*/ */
export async function handleResponse (response, method) { export async function handleResponse (response, method) {
const responseData = await _getResponseData(response) const responseData = await _getResponseData(response)
store.dispatch('SERVER_RESPONDED') store.dispatch('SERVER_RESPONDED', response.ok)
return response.ok ? responseData : handleError(response, responseData, method) return response.ok ? responseData : handleError(response, responseData, method)
} }

View file

@ -28,7 +28,11 @@ export default {
}, },
'ADD_HISTORY_ENTRY' (state, [uri, method, date]) { 'ADD_HISTORY_ENTRY' (state, [uri, method, date]) {
state.history.push({ uri, method, date, messages: [] }) state.history.push({ uri, method, date, status: 'pending', messages: [] })
},
'UPDATE_LAST_HISTORY_ENTRY' (state, [key, value]) {
Vue.set(state.history[state.history.length - 1], key, value)
}, },
'ADD_MESSAGE' (state, message) { 'ADD_MESSAGE' (state, message) {
@ -109,7 +113,15 @@ export default {
commit('ADD_HISTORY_ENTRY', [uri, method, Date.now()]) commit('ADD_HISTORY_ENTRY', [uri, method, Date.now()])
}, },
'SERVER_RESPONDED' ({ commit }) { 'SERVER_RESPONDED' ({ state, commit }, success) {
const action = state.history.length ? state.history[state.history.length - 1] : null
if (action) {
let status = success ? 'success' : 'error'
if (status === 'success' && action.messages.some(msg => msg.type === 'danger' || msg.type === 'warning')) {
status = 'warning'
}
commit('UPDATE_LAST_HISTORY_ENTRY', ['status', status])
}
commit('UPDATE_WAITING', false) commit('UPDATE_WAITING', false)
}, },