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

67 lines
1.6 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';
2020-05-01 05:55:26 +00:00
2020-03-17 22:16:34 +00:00
Vue.use(Vuex);
const store = new Store({
strict: true,
state: {
inCall: false,
user: { name: 'loading...', is_admin: false, id: null, books: [] },
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;
},
inCall(state) {
return state.inCall;
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);
},
callEnded(state) {
state.inCall = false;
},
2020-05-01 05:55:26 +00:00
callStarted(state) {
state.inCall = true;
2020-03-17 22:16:34 +00:00
}
},
actions: {
async getUser(ctx, userId?: number) {
2020-03-17 22:16:34 +00:00
const user = await Services.ApiService.getUser(userId);
ctx.commit('setUser', user);
2020-05-01 05:55:26 +00:00
return user;
2020-03-17 22:16:34 +00:00
},
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);
},
callEnded(ctx) {
ctx.commit('callEnded');
},
2020-05-01 05:55:26 +00:00
callStarted(ctx) {
ctx.commit('callStarted');
2020-04-12 14:25:42 +00:00
}
2020-03-17 22:16:34 +00:00
}
});
export default store;