Switching from false to true with Vuex and Axios

Asked

Viewed 118 times

0

Currently I have a structure with Nuxt and Vuex in the project, there is an object that comes directly from an Api and I need to make it change the state from "false" to "true" if the API returns it as true, so it will display 1 field in the menu, but in vuex he is saving in localstorage and there is that change of state.

Code of the vuex

state: {
user: {},
}
actions: {
  async betaUpdate(context, user) {        
    const usuario = (await Axios.get(`http://api-interna/me/${usuarioId}`)).data
  }

within user I receive information like beta: user.beta, betachart: user.betachart,

however in this my API I want to check if internally whenever there is an update in these two fields and change their false value to true if the API returns true,

in VUE I put a mapActions, calling the action but I can’t (I don’t know) make the change if there is a TRUE return from the Api above.

1 answer

0


The correct flow would always change the state from a Mutation, in the case from the context in the action you have access to the mutations (through the commit function), and then in your Mutation would change the state

state: {
  minhavar: false
},
mutations: {
  alteraMinhaVar(state, valor) {
    state.minhavar = valor
  }
},
actions: {
  async chamaAPI (context) {
    ...request
    context.commit('alteraMinhaVar', valorRetornadoPelaAPI)
  }
}
  • Problem still remains because, I receive in my State.user a usuer: {} because it is empty, I receive it after some time and when I make the call of the ...mapState(["betaChange"]), in the component it about writes and then keeps it empty. I would have to isolate or capture only these two Beta and betaCharts fields to modify only them, and I cannot do this intervention only in these two fields without reassembling the entire object

Browser other questions tagged

You are not signed in. Login or sign up in order to post.