import { ActionTree, GetterTree, Module, MutationTree } from 'vuex'; import RootState from '../types/RootStateModel'; import AlertStoreModel, { AlertModel, AlertModelTyped, AlertTypes } from '../types/AlertStoreModel'; import { getCmsSettings } from '../../tools/settings'; import { CmsError } from '../../api/errors'; const namespaced = true; const state: AlertStoreModel = { all: [] }; const actions: ActionTree = { addWarning({ dispatch }, message: string): void { const alert: AlertModelTyped = { type: AlertTypes.Warning, friendlyMessageHtml: message }; dispatch('add', alert); }, addSuccess({ dispatch }, message: string): void { const alert: AlertModelTyped = { type: AlertTypes.Success, friendlyMessageHtml: message }; dispatch('add', alert); }, addInfo({ dispatch }, message: string): void { const alert: AlertModelTyped = { type: AlertTypes.Info, friendlyMessageHtml: message }; dispatch('add', alert); }, /** * @deprecated This method is deprecated in favor of `addCmsError` */ addError({ dispatch }, message: string): void { const alert: AlertModelTyped = { type: AlertTypes.Danger, friendlyMessageHtml: message }; dispatch('add', alert); }, addCmsError({ dispatch }, error: CmsError): void { const alert: AlertModelTyped = { type: AlertTypes.Danger, friendlyMessageHtml: error.friendlyMessage || getCmsSettings().baseCmsErrorMessage, technicalMessage: error.message, errorCode: error.errorCode }; dispatch('add', alert); }, add({ commit }, alert: AlertModel): void { commit('add', alert); // auto-remove added alerts setTimeout(() => { commit('remove', alert); }, 10 * 1000); }, remove({ commit }, alert: AlertModelTyped): void { commit('remove', alert); } }; const getters: GetterTree = {}; const mutations: MutationTree = { add(state: AlertStoreModel, alert: AlertModelTyped) { state.all.push(alert); }, remove(state: AlertStoreModel, alert: AlertModelTyped) { const index = state.all.indexOf(alert); state.all.splice(index, 1); } }; export const alertStore: Module = { namespaced, state, getters, actions, mutations };