import { BASE_API_URL, getAxiosConfig } from '@/constants' export const state = () => ({ personal: [], public: [], claimed: [], lastSync: 0 }) export const mutations = { setPersonal(state, wishes) { state.personal = wishes state.lastSync = Date.now() }, setPublic(state, wishes) { state.public = wishes state.lastSync = Date.now() }, setClaimed(state, wishes) { state.claimed = wishes state.lastSync = Date.now() } } export const actions = { fetchPersonal({ commit }) { return this.$axios.$get(BASE_API_URL + '/wishes/me', getAxiosConfig(localStorage.getItem("token"))).then(wishes => { commit('setPersonal', wishes) }) }, fetchPublic({ commit }) { return this.$axios.$get(BASE_API_URL + '/wishes/list', getAxiosConfig(localStorage.getItem("token"))).then(wishes => { commit('setPublic', wishes) }) }, fetchClaimed({ commit }) { return this.$axios.$get(BASE_API_URL + '/wishes/claimed', getAxiosConfig(localStorage.getItem("token"))).then(wishes => { commit('setClaimed', wishes) }) }, deleteWish({ commit, state }, wishid) { return this.$axios.$delete(BASE_API_URL + '/wishes/' + wishid, getAxiosConfig(localStorage.getItem("token"))).then(() => { commit('setPersonal', state.personal.filter(w => w.id !== wishid)) }).catch((err) => { this._vm.$swal({ text: err.response.data.toString(), type: 'error', showConfirmButton: false, timerProgressBar: true, timer: 3000, toast: true, position: 'bottom-center', iconColor: 'white' }) }) }, claimWish({ commit, state }, wishid) { return this.$axios.$post(BASE_API_URL + '/wishes/' + wishid + '/claim', {}, getAxiosConfig(localStorage.getItem("token"))).then(async () => { commit('setPublic', state.public.filter(w => w.id !== wishid)) await this.dispatch('fetchClaimed', state) }) }, }