I'm new on Vuex js and everything about stores.
I've got the following store.js
import Vuex from 'vuex'
import Vue from 'vue'
import axios from 'axios';
Vue.use(Vuex)
export const store = new Vuex.Store({
state: {
pacientes: []
},
mutations: {
getPacientes() {
axios.get('http://slimapi/api/pacientes')
.then(response => {
this.state.pacientes = response.data
})
.catch(e => {
console.log(e)
})
}
}
});
Then i do a commit when i click a button on a modal (v-dialog) calling this function
agregarTurno()
{
axios.post('/api/pacientes/add', {
...
}).then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
this.$store.dispatch('fetchPacientes')
}
And then i've my turno.vue (take it as app.vue) where i retrieve the state of the store. However when the commit is made, the state it's not updating on the view!.
I'm trying to update the view when the state is changed!
turno.vue
export default {
name: 'turno',
data () {
return {
pacientes: this.$store.state
}
},
created() {
this.$store.commit('getPacientes')
},
mounted() {
this.$store.commit('getPacientes')
},
components: {
tarjetaTurno,
bottomNav,
modalTurno
}
}
I've been ready some posts, some of them talk about using computed property but i cant understand how to and if it's what i need to update the view everytime the state change.
Thank you everyone!
EDIT SOLVED
store.js
export const store = new Vuex.Store({
state: {
pacientes: []
},
mutations: {
setPacientes(state, payload) {
state.pacientes = payload.pacientes
}
},
actions: {
fetchPacientes({commit}) {
axios.get('http://slimapi/api/pacientes')
.then(response => {
commit('setPacientes', {pacientes: response.data}); //After getting patients, set the state
})
.catch(e => {
console.log(e)
})
},
addPacientes(context){
axios.post('/api/pacientes/add', {
nombre: "test",
apellido: "test",
edad: "test",
peso_inicial:"test",
patologia:"test"
}).then(function (response){
context.dispatch('fetchPacientes'); //After adding patient, dispatch the fetchPatient to get them and set state
})
.catch(function (error) {
console.log(error);
});
}
}
});
Component modal which call the function to add patient:
agregarTurno()
{
this.$store.dispatch('addPacientes');
}
Turno.vue (root) who update when state changes
export default {
name: 'turno',
data () {
return {
pacientes: this.$store.state
}
},
created() {
this.$store.dispatch('fetchPacientes')
},