import url from '@/plugins/routes' import axios from 'axios' const state = { jobkinds: [], jobkindLoading: false } const mutations = { setJobkinds: (state, jobkinds) => { state.jobkinds = jobkinds }, updateJobKind: (state, jobkind) => { console.log(jobkind) const exists = state.jobkinds.find(a => { return a.id === jobkind.id }) console.log(exists) if (exists) { exists.name = jobkind.name } else { state.jobkinds.push(jobkind) } }, deleteJobKind: (state, jobkind) => { const exists = state.jobkinds.indexOf( state.jobkinds.find(a => { return a.id === jobkind.id }) ) state.jobkinds.splice(exists, 1) }, setJobkindsLoading: (state, value) => { state.jobkindLoading = value } } const actions = { async getAllJobKinds({ commit, rootState, dispatch }) { try { commit('setJobkindsLoading', true) const response = await axios.get(url.vorstand.sm.getAllJobKindsbKinds, { headers: { Token: rootState.login.user.accessToken } }) commit('setJobkinds', response.data) commit('setJobkindsLoading', false) dispatch('getLifeTime', null, { root: true }) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout', null, { root: true }) } }, async addJobKind({ commit, rootState, dispatch }, data) { try { commit('setJobkindsLoading', true) const response = await axios.put( url.vorstand.sm.jobkind, { ...data }, { headers: { Token: rootState.login.user.accessToken } } ) commit('updateJobKind', response.data) commit('setJobkindsLoading', false) dispatch('getLifeTime', null, { root: true }) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout', null, { root: true }) } }, async updateJobKind({ commit, rootState, dispatch }, data) { try { commit('setJobkindsLoading', true) const response = await axios.post( url.vorstand.sm.jobkind, { ...data }, { headers: { Token: rootState.login.user.accessToken } } ) commit('updateJobKind', response.data) commit('setJobkindsLoading', false) dispatch('getLifeTime', null, { root: true }) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout', null, { root: true }) } }, async deleteJobKind({ commit, rootState, dispatch }, data) { try { commit('setJobkindsLoading', true) await axios.post( url.vorstand.sm.deleteJobKind, { ...data }, { headers: { Token: rootState.login.user.accessToken } } ) commit('deleteJobKind', data) commit('setJobkindsLoading', false) dispatch('getLifeTime', null, { root: true }) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout', null, { root: true }) } } } const getters = { jobkinds: state => { return state.jobkinds }, jobkindsLoading: state => { return state.jobkindLoading } } export default { namespaced: true, state, mutations, actions, getters }