So I have an action that makes a POST request to an endpoint that creates a comment for a particular artwork. On the components which renders the artwork and its comments, I dispatch an action in the onMounted() hook that makes a GET request for the artwork with that id, and then stores it in the Vuex.
Once the POST request that creates the comment goes through, I can access the artwork property in the store, and just push the response to the comments property which is an array of comments. I don't know if this is the correct way to do it though, since from what I understand any state change should be done through mutations, so directly accessing the state and pushing array elements into it seems incorrect?
This is my action that creates a comment and pushes the response to the selected artwork's comments property:
async createComment({commit, state}, payload){
try {
let response = await axios.post("/createComment", payload)
console.log(response)
state.image.comments.push(response.data.comment)
} catch (error) {
console.log(error)
}
},
I guess the alternative is to copy the artwork from the state, push the new comment in the comments property of the copy, and then commit the new object?