81 lines
1.6 KiB
JavaScript
81 lines
1.6 KiB
JavaScript
import Vue from 'vue'
|
|
import Vuex from 'vuex'
|
|
|
|
Vue.use(Vuex);
|
|
|
|
export default new Vuex.Store({
|
|
state: {
|
|
notification: null,
|
|
notificationTimeout: null,
|
|
saving: false
|
|
},
|
|
|
|
|
|
mutations: {
|
|
saving(state, value)
|
|
{
|
|
state.saving = value;
|
|
},
|
|
|
|
|
|
_setNotification(state, payload)
|
|
{
|
|
state.notification = payload.notification;
|
|
state.notificationTimeout = payload.notificationTimeout;
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
showNotification(context, payload)
|
|
{
|
|
const self = this;
|
|
if (context.state.notificationTimeout !== null)
|
|
clearTimeout(context.state.notificationTimeout);
|
|
|
|
const notificationTimeout = setTimeout(() =>
|
|
{
|
|
context.dispatch('hideNotification');
|
|
}, 5000);
|
|
|
|
context.commit('_setNotification', {
|
|
notification: payload,
|
|
notificationTimeout
|
|
});
|
|
},
|
|
|
|
|
|
hideNotification(context)
|
|
{
|
|
if (context.state.notificationTimeout !== null)
|
|
clearTimeout(context.state.notificationTimeout);
|
|
|
|
context.commit('_setNotification', {
|
|
notification: null,
|
|
notificationTimeout: null
|
|
});
|
|
},
|
|
|
|
|
|
notifyAPIError(context, payload)
|
|
{
|
|
console.log(payload.error);
|
|
let errorMessage = '';
|
|
|
|
if (payload.error.response)
|
|
{
|
|
errorMessage = 'HTTP response code ' + payload.error.response.status;
|
|
}
|
|
else if (payload.error.request)
|
|
{
|
|
errorMessage = 'No response';
|
|
}
|
|
else
|
|
{
|
|
errorMessage = payload.error.message;
|
|
}
|
|
|
|
context.dispatch('showNotification', { message: payload.message + '\n\n' + errorMessage, isError: true });
|
|
}
|
|
},
|
|
})
|