seepur/resources/scripts/applications/home/state.vuex.ts

49 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-03-17 22:16:34 +00:00
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,
2020-04-12 14:25:42 +00:00
notifications: []
2020-03-17 22:16:34 +00:00
},
getters: {
user(state) {
return state.user;
2020-04-12 14:25:42 +00:00
},
notifications(state) {
return state.notifications;
2020-03-17 22:16:34 +00:00
}
},
mutations: {
2020-04-12 14:25:42 +00:00
setUser(state, user) {
2020-03-17 22:16:34 +00:00
state.user = user;
2020-04-12 14:25:42 +00:00
},
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);
2020-03-17 22:16:34 +00:00
}
},
actions: {
getUser: async (ctx, userId?: number) => {
const user = await Services.ApiService.getUser(userId);
ctx.commit('setUser', user);
},
2020-04-12 14:25:42 +00:00
notify(ctx, notification: { message: string, level?: "info" | "warning" | "success" | "danger" }) {
ctx.commit("notify", notification);
},
dismissNotification(ctx, id: number) {
ctx.commit("dismissNotification", id);
}
2020-03-17 22:16:34 +00:00
}
});
export default store;