seepur/resources/scripts/applications/home/state.vuex.ts
2020-04-12 10:25:42 -04:00

49 lines
1.2 KiB
TypeScript

import Vue from 'vue';
import Vuex, { Store } from "vuex";
import Services from '../services';
Vue.use(Vuex);
const store = new Store({
strict: true,
state: {
user: null,
notifications: []
},
getters: {
user(state) {
return state.user;
},
notifications(state) {
return state.notifications;
}
},
mutations: {
setUser(state, user) {
state.user = user;
},
notify(state, notification) {
const id = Math.ceil(Math.random() * 1000);
state.notifications.push({ ...notification, id });
const dispatch = this.dispatch;
setTimeout(() => {
dispatch("dismissNotification", id);
}, 5000);
},
dismissNotification(state, noteId: number) {
state.notifications = state.notifications.filter(n => n.id != noteId);
}
},
actions: {
getUser: async (ctx, userId?: number) => {
const user = await Services.ApiService.getUser(userId);
ctx.commit('setUser', user);
},
notify(ctx, notification: { message: string, level?: "info" | "warning" | "success" | "danger" }) {
ctx.commit("notify", notification);
},
dismissNotification(ctx, id: number) {
ctx.commit("dismissNotification", id);
}
}
});
export default store;