Skip to content

Commit 68757fc

Browse files
author
jfusco
committed
Add reset user functionality
Refactor edit user form so the store doesn't update the synced value until you hit the save button. Add reset functionality by manually resetting the form back to it's original state. Move getUser() to the App level and call it on the created lifecycle hook.
1 parent 76fdb0f commit 68757fc

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

src/components/Header.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<template>
22
<div>
3-
Hello: {{user.firstName}} {{user.lastName}}
3+
Hello: {{fullName}}
44
</div>
55
</template>
66

77
<script type="text/babel">
88
import Vue from 'vue'
9-
import { mapState } from 'vuex'
9+
import { mapGetters } from 'vuex'
1010
1111
export default Vue.component('app-header', {
12-
computed: mapState([
13-
'user'
14-
])
12+
computed: {
13+
...mapGetters([
14+
'fullName'
15+
])
16+
}
1517
})
1618
</script>

src/components/Nav.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<nav>
2+
<nav v-once>
33
<ul role="menu">
44
<li role="presentation">
55
<router-link to="/" exact>home</router-link>

src/pages/App.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@
1616
<script type="text/babel">
1717
import appHeader from '../components/Header.vue'
1818
import appNav from '../components/Nav.vue'
19+
import { mapActions, mapState } from 'vuex'
1920
2021
export default {
2122
components: {
2223
appHeader,
2324
appNav
25+
},
26+
27+
methods: {
28+
...mapActions([
29+
'getUser'
30+
])
31+
},
32+
33+
created(){
34+
this.getUser()
2435
}
2536
}
2637
</script>

src/pages/EditUser.vue

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
<div>
33
<h2>Edit user</h2>
44

5-
<input v-model="user.firstName" placeholder="First name" />
6-
<input v-model="user.lastName" placeholder="Last name" />
7-
<input v-model="user.age" type="number" placeholder="Age" />
5+
<input v-model="form.firstName" placeholder="First name" />
6+
<input v-model="form.lastName" placeholder="Last name" />
7+
<input v-model="form.age" type="number" placeholder="Age" />
88

9-
<select v-model="user.gender">
9+
<select v-model="form.gender">
1010
<option value="male">male</option>
1111
<option value="female">female</option>
1212
</select>
1313

1414
<button v-on:click="save">save</button>
15+
<button v-on:click="reset">reset</button>
1516
</div>
1617
</template>
1718

@@ -20,19 +21,40 @@
2021
import { mapActions, mapState } from 'vuex'
2122
2223
export default {
24+
data: () => {
25+
return {
26+
form: {
27+
firstName: '',
28+
lastName: '',
29+
age: '',
30+
gender: ''
31+
}
32+
}
33+
},
34+
2335
methods: {
2436
...mapActions([
25-
'getUser',
2637
'updateUser'
2738
]),
2839
2940
save(){
30-
this.updateUser(this.user)
41+
this.updateUser(this.form)
42+
},
43+
44+
reset(){
45+
this.update()
46+
},
47+
48+
update(){
49+
this.form.firstName = this.user.firstName
50+
this.form.lastName = this.user.lastName
51+
this.form.age = this.user.age
52+
this.form.gender = this.user.gender
3153
}
3254
},
3355
3456
mounted(){
35-
this.getUser()
57+
this.update()
3658
},
3759
3860
computed: mapState([

src/store/modules/user/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ const mutations = {
2424
}
2525
}
2626

27+
const getters = {
28+
fullName: state => {
29+
return `${state.firstName} ${state.lastName}`
30+
}
31+
}
32+
2733
export default {
2834
state,
2935
mutations,
30-
actions
36+
actions,
37+
getters
3138
}

0 commit comments

Comments
 (0)