Here is the code snippet I am working with:
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteStore($event)">
<span class="fa fa-heart"></span> Favorite
</a>
</template>
<script>
export default{
props:['idStore'],
mounted(){
this.checkFavoriteStore()
},
methods:{
addFavoriteStore(event){
alert('toast');
event.target.disabled = true;
const payload= {id_store: this.idStore};
this.$store.dispatch('addFavoriteStore', payload);
setTimeout(function () {
location.reload(true);
}, 1500);
},
checkFavoriteStore(){
const payload= {id_store: this.idStore};
this.$store.dispatch('checkFavoriteStore', payload);
setTimeout(function () {
location.reload(true);
}, 1500);
// Obtain response here
}
}
}
</script>
When running the script, it will first invoke the checkFavoriteStore method.
The checkFavoriteStore method triggers an action on the Vuex store.
The results of the action will yield a response.
How can I retrieve this response?
UPDATE
My Vuex store action appears as follows:
import { set } from 'vue'
import favorite from '../../api/favorite'
import * as types from '../mutation-types'
// Actions
const actions = {
checkFavoriteStore ({ dispatch,commit,state },payload)
{
favorite.checkFavorite(payload,
data => {
commit(types.CHECK_FAVORITE_SUCCESS)
},
errors => {
commit(types.CHECK_FAVORITE_FAILURE)
console.log(errors)
}
)
}
}
// Mutations
const mutations = {
[types.CHECK_FAVORITE_SUCCESS] (state){
state.addStatus = 'success'
},
[types.CHECK_FAVORITE_FAILURE] (state){
state.addStatus = 'failure'
}
}
export default {
actions,
mutations
}
And the API structure is outlined below:
import Vue from 'vue'
import Resource from 'vue-resource'
Vue.use(Resource)
export default {
// Check if favorite exists in the API or not
checkFavorite (favorite, cb, ecb = null ) {
Vue.http.post(window.Laravel.baseUrl+'/member/store/favorite/check-favorite', favorite)
.then(
(resp) => cb(resp.data),
(resp) => ecb(resp.data)
);
}
}