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

77 lines
1.6 KiB
TypeScript
Raw Normal View History

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