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

79 lines
2.2 KiB
TypeScript

import Vue from 'vue';
import Vuex, { Store } from "vuex";
import Services from '../services';
import CallManager, { ECallEvents } from "./classes/call.manager";
import WebsocketService from './scripts/websocket.service';
import { createContext } from 'vm';
Vue.use(Vuex);
const store = new Store({
strict: true,
state: {
inCall: false,
callManager: null as CallManager,
user: { name: 'loading...', is_admin: false, id: null },
notifications: []
},
getters: {
user(state) {
return state.user;
},
notifications(state) {
return state.notifications;
},
callManager(state): CallManager {
return state.callManager;
},
inCall(state) {
return state.inCall;
}
},
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);
},
callEnded(state) {
state.callManager = null;
state.inCall = false;
},
connectToCall(state, options: { callId: number, ws: any }) {
state.callManager = new CallManager(options.ws, options.callId, state.user.id);
state.inCall = true;
}
},
actions: {
async getUser(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);
},
callEnded(ctx) {
ctx.commit('callEnded');
},
async connectToCall(ctx, callId: string) {
if (!ctx.state.inCall) {
const ws = await WebsocketService.getInstance();
ctx.commit('connectToCall', { callId, ws });
return true;
}
return false;
}
}
});
export default store;