google_online
@google_online
My morning begins with coffee and Twitter.

Как обновить state после PATCH запроса?

Мне нужно обновить profile в state после PATCH запроса.
Как это сделать?

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
}
}

Как мне следать commit в SET_PRFILE чтобы обновить profile в state ?
  • Вопрос задан
  • 100 просмотров
Решения вопроса 1
lavezzi1
@lavezzi1
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
    },
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы