import axios from 'axios' import url from '@/plugins/routes' const state = { user: null, creditList: [], loading: false } const mutations = { setUser: (state, user) => { state.user = user let list = {} for (let creditList in user['creditList']) { // eslint-disable-next-line no-console console.log(creditList) let amount = mutations.createAmount(user['creditList'][creditList]) let credit = mutations.createCredit(user['creditList'][creditList]) let sum = mutations.createSum(credit, amount) list[creditList] = [{ ...credit }, { ...amount }, { ...sum }] } state.user.creditList = list state.creditList = [] // eslint-disable-next-line no-console console.log(state.user) }, createAmount(creditList) { let amount = { type: 'Schulden', jan_amount: 0 - creditList.jan.depts, feb_amount: 0 - creditList.feb.depts, maer_amount: 0 - creditList.maer.depts, apr_amount: 0 - creditList.apr.depts, mai_amount: 0 - creditList.mai.depts, jun_amount: 0 - creditList.jun.depts, jul_amount: 0 - creditList.jul.depts, aug_amount: 0 - creditList.aug.depts, sep_amount: 0 - creditList.sep.depts, okt_amount: 0 - creditList.okt.depts, nov_amount: 0 - creditList.nov.depts, dez_amount: 0 - creditList.dez.depts, last: 0 - creditList['last'] } amount.sum = amount.jan_amount + amount.feb_amount + amount.maer_amount + amount.apr_amount + amount.mai_amount + amount.jun_amount + amount.jul_amount + amount.aug_amount + amount.sep_amount + amount.okt_amount + amount.nov_amount + amount.dez_amount return amount }, createCredit(creditList) { let credit = { type: 'Guthaben', jan_amount: creditList.jan.credit, feb_amount: creditList.feb.credit, maer_amount: creditList.maer.credit, apr_amount: creditList.apr.credit, mai_amount: creditList.mai.credit, jun_amount: creditList.jun.credit, jul_amount: creditList.jul.credit, aug_amount: creditList.aug.credit, sep_amount: creditList.sep.credit, okt_amount: creditList.okt.credit, nov_amount: creditList.nov.credit, dez_amount: creditList.dez.credit } credit.sum = credit.jan_amount + credit.feb_amount + credit.maer_amount + credit.apr_amount + credit.mai_amount + credit.jun_amount + credit.jul_amount + credit.aug_amount + credit.sep_amount + credit.okt_amount + credit.nov_amount + credit.dez_amount return credit }, createSum(credit, amount) { let sum = { type: 'Summe', jan_amount: credit.jan_amount + amount.jan_amount, feb_amount: credit.feb_amount + amount.feb_amount, maer_amount: credit.maer_amount + amount.maer_amount, apr_amount: credit.apr_amount + amount.apr_amount, mai_amount: credit.mai_amount + amount.mai_amount, jun_amount: credit.jun_amount + amount.jun_amount, jul_amount: credit.jul_amount + amount.jul_amount, aug_amount: credit.aug_amount + amount.aug_amount, sep_amount: credit.sep_amount + amount.sep_amount, okt_amount: credit.okt_amount + amount.okt_amount, nov_amount: credit.nov_amount + amount.nov_amount, dez_amount: credit.dez_amount + amount.dez_amount } sum.sum = sum.jan_amount + sum.feb_amount + sum.maer_amount + sum.apr_amount + sum.mai_amount + sum.jun_amount + sum.jul_amount + sum.aug_amount + sum.sep_amount + sum.okt_amount + sum.nov_amount + sum.dez_amount return sum }, setLoading(state, value) { state.loading = value } } const actions = { async getUser({ commit, rootState, dispatch }) { commit("setLoading", true) try { const response = await axios.get(url.userMain, { headers: { Token: rootState.login.user.accessToken } }) commit('setUser', response.data) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout', null, { root: true }) } commit("setLoading", false) }, async addAmount({ commit, rootState, dispatch }, amount) { try { const response = await axios.post( url.userAddAmount, { amount: amount }, { headers: { Token: rootState.login.user.accessToken } } ) commit('setUser', response.data) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout', null, { root: true }) } } } const getters = { user: state => { return state.user }, year: state => { let year = 0 for (let creditList in state.user.creditList) { let currentYear = parseInt(creditList) if (currentYear > year) year = currentYear } return year }, loading: state => { return state.loading } } export default { namespaced: true, state, mutations, actions, getters }