link411 link412 link413 link414 link415 link416 link417 link418 link419 link420 link421 link422 link423 link424 link425 link426 link427 link428 link429 link430 link431 link432 link433 link434 link435 link436 link437 link438 link439 link440 link441 link442 link443 link444 link445 link446 link447 link448 link449 link450 link451 link452 link453 link454 link455 link456 link457 link458 link459 link460 link461 link462 link463 link464 link465 link466 link467 link468 link469 link470 link471 link472 link473 link474 link475 link476 link477 link478 link479 link480 link481 link482 link483 link484 link485 link486 link487 link488 link489 link490 link491 link492 link493 link494 link495 link496 link497 link498 link499 link500 link501 link502 link503 link504 link505 link506 link507 link508 link509 link510 link511 link512 link513 link514 link515 link516 link517 link518 link519 link520 link521 link522 link523 link524 link525 link526 link527 link528 link529 link530 link531 link532 link533 link534 link535 link536 link537 link538 link539 link540 link541 link542 link543 link544 link545 link546 link547

[Vue.js] DB updates but Vuex store does not

when trying to submit a form that updates a user’s account settings. The form submits correctly to my API and the database entry updates. The component updates correctly initially, but when I refresh the page the component reverts back to its initial state, telling me that the Vuex store hasn’t updated correctly (I think?)

I’m sure it’s my ignorance on Vuex in general but I also feel that I’m pretty close. I’m basing this off of Jason Watmore’s excellent posts, this one specifically. Any suggestions would be much appreciated!

ProfilePage.vue.js (parent component)

<template>

<div class=’profile-container’>

<!– {/* User Info */} –>
<div class=’user-info-container’>
<h1 class=’user-headline’>{ account.user | prettyName }</h1>
<p>{ account.user | userLocation }</p>
</div>

<!– {/* Personal Details Form */} –>
<div class=’personal-details’ v-if=’activeSelection(0)’ >
<personal-form :account=’this.account.user’ v-on:listensubmit=’handleSubmit’/>
</div>

</div>

</template>

<script>
import { mapState, mapActions } from ‘vuex’;
import PersonalForm from ‘./_partials/PersonalForm’;

export default {
data: () => ({
tile : false,
}),
computed: {
…mapState({
account: state => state.account
}),
},
methods: {
…mapActions(‘account’, [‘update’]),
handleSubmit: function(user) {
this.$validator.validateAll().then((result) => {
if (result) {
this.submitted = true;
this.update(user);
}
});
},
},
components: {
PersonalForm,
}
};
</script>

PersonalForm.vue.js (child component)

<template>
<v-form ref=”personal-details-form” lazy-validation>
<v-container fluid grid-list-lg>
<v-layout row wrap>
<!– First Name –>
<v-flex xs12 md6>
<v-text-field
v-model=”user.firstName”
label=”First Name”
color=”primary”
prepend-inner-icon=”playlist_add”
outline
/>
</v-flex>

<!– Last Name –>
<v-flex xs12 md6>
<v-text-field
v-model=”user.lastName”
label=”Last Name”
color=”primary”
prepend-inner-icon=”playlist_add”
outline
/>
</v-flex>

<!– Email –>
<v-flex xs12 md6>
<v-text-field
v-model=’user.email’
v-validate=”‘required|email’”
:error-messages=”errors.collect(‘email’)”
label=’E-mail’
data-vv-name=’email’
color=’primary’
outline
prepend-inner-icon=”mail_outline”
/>
</v-flex>
</v-layout>

<v-layout justify-end>
<v-btn @click=”handleSubmit” color=”primary” :ripple=”false”>Submit</v-btn>
</v-layout>
</v-container>
</v-form>
</template>

<script>
import { mapState, mapActions } from ‘vuex’;

export default {
data: () => ({
submitted : false,
}),
computed: {
user () {
return {…this.account}
}
},
props: {
account: {
type : Object,
required: true,
},
listensubmit: {
type: Function,
}
},
methods: {
handleSubmit(event) {
this.$emit(‘listensubmit’, this.user);
}
},
}
</script>

account.module.js

import { userService } from ‘../_services’;
import { router } from ‘../_helpers’;

const user = JSON.parse(localStorage.getItem(‘user’));
const state = user
? { status: { loggedIn: true }, user }
: { status: {}, user: null };

const actions = {
update({ dispatch, commit }, user) {
commit(‘updateRequest’, user);

userService.update(user)
.then(
u => {
commit(‘updateSuccess’, user);
dispatch(‘alert/success’, ‘Account updated’, { root: true })
},
error => {
commit(‘updateFailure’, error);
dispatch(‘alert/error’, error, { root: true });
}
);
},
}

const mutations = {
updateRequest(state, user) {
state.status = { updating: true };
},
updateSuccess(state, user) {
state.status = { updated: true };
state.user = user;
},
updateFailure(state) {
state.status = { updated: false };
}
}

export const account = {
namespaced: true,
state,
actions,
mutations,
};

user.service.js

import config from ‘config’;
import { authHeader } from ‘../_helpers’;

export const userService = {
update,
};

function update(user) {
const requestOptions = {
method: ‘PUT’,
headers: { …authHeader(), ‘Content-Type’: ‘application/json’ },
body: JSON.stringify(user)
};

return fetch(`${config.apiUrl}/users/${user.id}`, requestOptions).then(handleResponse);
}

function handleResponse(response) {
return response.text().then(text => {
const data = text && JSON.parse(text);

if (!response.ok) {
if (response.status === 401) {
// auto logout if 401 response returned from api
logout();
location.reload(true);
}

const error = (data && data.message) || response.statusText;
return Promise.reject(error);
}

return data;
});
}

Solution :

Per Phil’s help in the comments of the original question, updating the user object in localStorage seems to have solved the problem.