flaschengeist-frontend/src/plugins/user/store/session.ts

77 lines
1.6 KiB
TypeScript

import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
import { StateInterface } from 'src/store';
import { axios } from 'src/boot/axios';
export interface SessionInterface {
sessions: Session[];
loading: boolean;
}
const state: SessionInterface = {
sessions: [],
loading: false
};
const mutations: MutationTree<SessionInterface> = {
setSessions (state, sessions: Session[]) {
state.sessions = sessions;
},
setLoading (state, value: boolean) {
state.loading = value;
}
};
const actions: ActionTree<SessionInterface, StateInterface> = {
getSessions ({ commit, rootGetters }) {
console.log(rootGetters);
commit('setLoading', true);
axios
.get('/auth')
.then(response => {
console.log(response.data);
commit('setSessions', response.data);
})
.catch(error => {
console.exception(error);
})
.finally(() => {
commit('setLoading', false);
});
},
deleteSession ({ commit, dispatch }, token: string) {
commit('setLoading', true);
console.log('axios', axios);
axios
.delete(`/auth/${token}`)
.then(() => {
void dispatch('getSessions');
})
.catch(error => {
console.exception(error);
})
.finally(() => {
commit('setLoading', false);
});
}
};
const getters: GetterTree<SessionInterface, StateInterface> = {
sessions (state) {
return state.sessions;
},
loading (state) {
return state.loading;
}
};
const sessions: Module<SessionInterface, StateInterface> = {
namespaced: true,
state,
mutations,
actions,
getters
};
export default sessions;