/* eslint-disable */
import axios from 'axios'

export default {
    namespaced: true,
    state: {
        items: [],
        indiv: undefined,
    },
    mutations: {
        addItem(state, item) {
            state[item.name].push(item.value)
        },
        setItem(state, item) {
            state[item.name] = item.value
        },
    },
    getters: {},
    actions: {
        async getIndividual({ commit, rootGetters }, userId) {
            let id = 0
            if (!userId || userId === 0) {
                const rootItem = rootGetters['authentication/getUser']
                id = rootItem ? rootItem.id : 0
            } else id = userId

            if (!id) {
                id = 0
            }

            await axios
                .get(`/api/individual/getIndividual/${id}`)
                .then((r) => {
                    commit('setItem', {
                        name: 'indiv',
                        value: r.data,
                    })
                })
                .catch(console.error)
        },
        getAllIndividuals({ commit }) {
            axios
                .get('/api/individual/getAllIndividuals')
                .then((x) => {
                    commit('setItem', {
                        name: 'items',
                        value: x.data,
                    })
                })
                .catch(console.error)
        },
        async updateIndividual({ commit }, item) {
            await axios
                .put('/api/individual', item)
                .then((result) => commit('setItem', item))
                .catch(console.error)
        },
    },
}