M
M
Mike2018-10-18 23:52:07
Vue.js
Mike, 2018-10-18 23:52:07

How to update state after PATCH request?

I need to update profile in state after PATCH request.
How to do it?

const state = {
    status: null,
    profile: {},
    token: null || ''

const actions = {
// для удобства я убрл другие actions
    SET_PROFILE: ({commit}, id, data) => {
        axios.patch('/profile/user/' + id + '/', data)
        .then(response => {
            console.log('it is OK')
            
        })
        .catch(error => {
            console.log(error, 'something went wrong')
        })
    },
// тут тоже убрал для простоты mutations. 
const mutations = {
    GET_PROFILE: (state, profile) => {
        state.profile = profile
    },
}
const getters = {
    isAuthenticated: state => !!state.token,
    authStatus: state => state.status,
    profile: state => state.profile,
    token: state => state.token
}
}

How do I follow the commit in SET_PRFILE to update the profile in state ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lavezzi1, 2018-10-19
@google_online

const state = {
    status: null,
    profile: {},
    token: null,

const actions = {
    fetchProfile: ({commit}, id, data) => {
        axios.patch('/profile/user/' + id + '/', data)
        .then(response => {
           commit('PROFILE_SET', response.profile);
        })
        .catch(error => {
            console.log(error, 'something went wrong')
        })
    },

const mutations = {
    PROFILE_SET: (state, profile) => {
        state.profile = profile
    },
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question