import axios from 'axios' import url from '@/plugins/routes' const state = { user: null, creditList: [], loading: false, addLoading: false, error: '', days: [], message: [], status: [] } const mutations = { setStatus: (state, status) => { state.status = status }, 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 = [] state.error = '' // 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 }, setAddLoading(state, value) { state.addLoading = value }, setError(state, value) { state.error = value console.log(state) }, createDays(state, date) { let days = [] for (let i = 0; i <= 10; i++) { days.push({ date: new Date( date.getFullYear(), date.getMonth(), date.getDate() + i, 12 ), job: false, workers: [] }) } state.days = days }, updateDay(state, data) { const date = data.date for (let day in state.days) { if (state.days[day].date - date === 0) { state.days[day].job = data.job if (data.workers) state.days[day].workers = data.workers } } }, addMessage: (state, data) => { var message = '' if (data.error) { message = 'Konnte ' + (data.amount / 100).toFixed(2) + '€ zu ' + data.user.firstname + ' ' + data.user.lastname + ' hinzufügen.' } else { message = '' + (data.amount / 100).toFixed(2) + '€ wurde zu ' + data.user.firstname + ' ' + data.user.lastname + ' hinzugefügt.' } state.message.unshift({ message: message, user: data.user, error: data.error, storno: false, loading: false, visible: true, amount: data.amount, date: new Date() }) console.log(state.message) }, updateMessage: (state, data) => { var message = state.message.find(msg => { return msg.date - data.date === 0 ? true : false }) if (message) { if (data.storno !== undefined) message.storno = data.storno if (data.loading !== undefined) message.loading = data.loading } } } 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) commit('setError', '') } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout', null, { root: true }) } commit('setLoading', false) }, async addAmount({ commit, rootState, dispatch }, amount) { commit('setAddLoading', true) try { const response = await axios.post( url.userAddAmount, { amount: amount }, { headers: { Token: rootState.login.user.accessToken } } ) console.log(response.data) commit('setUser', response.data) commit('addMessage', { user: rootState.login.user, amount: amount, error: false }) commit('setError', '') } catch (e) { commit('addMessage', { user: rootState.login.user, amount: amount, error: true }) if (e.response) if (e.response.status === 401) dispatch('logout', null, { root: true }) } commit('setAddLoading', false) }, async saveConfig({ commit, rootState, dispatch }, data) { commit('setLoading', true) try { const response = await axios.post( url.user.config, { ...data }, { headers: { Token: rootState.login.user.accessToken } } ) console.log(response.data) commit('setUser', response.data) commit('setError', '') } catch (e) { if (e.response) { if (e.response.status === 401) dispatch('logout', null, { root: true }) if (e.response.data) { console.log(e.response.data) commit('setError', e.response.data.error) } } } commit('setLoading', false) }, createDays({ commit }, date) { commit('createDays', date) }, async updateDay({ commit, rootState, dispatch }, data) { commit('setLoading', true) try { console.log('hier bin ich') const response = await axios.post( url.user.job, { date: data.date.getTime() / 1000 }, { headers: { Token: rootState.login.user.accessToken } } ) console.log(response.data) commit('updateDay', { ...response.data, date: data.date }) } catch (e) { if (e.response) { if (e.response.status === 401) dispatch('logout', null, { root: true }) if (e.response.data) { console.log(e.response.data, data.date) commit('setError', e.response.data.error) } } console.log(e) } commit('setLoading', false) }, async storno({ commit, rootState, dispatch }, data) { commit('updateMessage', { date: data.date, loading: true }) try { const response = await axios.post( url.user.storno, { amount: data.amount }, { headers: { Token: rootState.login.user.accessToken } } ) console.log(response.data) commit('setUser', response.data) commit('updateMessage', { date: data.date, storno: true }) } catch (e) { if (e.response) if (e.response.status === 401) dispatch('logout', null, { root: true }) } commit('updateMessage', { date: data.date, loading: false }) }, async getStatus({commit, rootState, dispatch }) { try { const response = await axios.get(url.user.getAllStatus, { headers: { Token: rootState.login.user.accessToken } }) commit('setStatus', 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 }, addLoading: state => { return state.addLoading }, error: state => { return state.error }, days: state => { return state.days }, messages: state => { return state.message }, status: state => { return state.status } } export default { namespaced: true, state, mutations, actions, getters }