Skip to content

Commit 15715f8

Browse files
author
jfusco
committed
Add edit user functionality
Add UPDATE_USER type and action. Add UI and styles for edit user page. Add object assign to get user and update user mutations. Add constants to hold local storage user key.
1 parent 5833333 commit 15715f8

File tree

10 files changed

+106
-9
lines changed

10 files changed

+106
-9
lines changed

src/components/Header.vue

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

77
<script type="text/babel">
88
import Vue from 'vue'
9+
import { mapState } from 'vuex'
910
1011
export default Vue.component('app-header', {
11-
props: {
12-
13-
}
12+
computed: mapState([
13+
'user'
14+
])
1415
})
1516
</script>

src/constants/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
export const LOCAL_USER = 'vue.user'

src/pages/EditUser.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
<template>
22
<div>
33
<h2>Edit user</h2>
4+
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" />
8+
9+
<select v-model="user.gender">
10+
<option value="male">male</option>
11+
<option value="female">female</option>
12+
</select>
13+
14+
<button v-on:click="save">save</button>
415
</div>
516
</template>
617

718
<script type="text/babel">
819
import Vue from 'vue'
20+
import { mapActions, mapState } from 'vuex'
21+
22+
export default {
23+
methods: {
24+
...mapActions([
25+
'getUser',
26+
'updateUser'
27+
]),
28+
29+
save(){
30+
this.updateUser(this.user)
31+
}
32+
},
33+
34+
mounted(){
35+
this.getUser()
36+
},
37+
38+
computed: mapState([
39+
'user'
40+
])
41+
}
942
</script>

src/pages/Users.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
loader,
2424
user
2525
},
26+
2627
methods: {
2728
...mapActions([
2829
'getUsers'
2930
])
3031
},
32+
3133
mounted(){
3234
this.getUsers(10);
3335
},
36+
3437
computed: mapState([
3538
'users',
3639
'loader'

src/store/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ export default new Vuex.Store({
2121
user,
2222
loader
2323
},
24-
strict: debug,
24+
strict: false,
2525
middlewares: debug ? [createLogger()] : []
2626
})

src/store/modules/loader/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
'use strict';
22

3-
export const SHOW_LOADER = 'SHOW_LOADER';
4-
export const HIDE_LOADER = 'HIDE_LOADER';
3+
export const SHOW_LOADER = 'SHOW_LOADER'
4+
export const HIDE_LOADER = 'HIDE_LOADER'

src/store/modules/user.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/store/modules/user/actions.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
11
'use strict';
22

3+
import Vue from 'vue'
4+
import VueResource from 'vue-resource'
5+
import * as type from './types'
6+
import { LOCAL_USER } from '../../../constants/index'
7+
8+
function getLocalUser(){
9+
return localStorage.getItem(LOCAL_USER);
10+
}
11+
12+
const actions = {
13+
getUser({ commit }){
14+
const storedUser = getLocalUser()
15+
16+
commit(type.GET_USER, {
17+
user: storedUser
18+
})
19+
},
20+
21+
updateUser({ commit }, user){
22+
localStorage.setItem(LOCAL_USER, JSON.stringify(user));
23+
24+
commit(type.UPDATE_USER, {
25+
user
26+
})
27+
}
28+
}
29+
30+
export default actions;

src/store/modules/user/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
'use strict';
22

3+
import * as type from './types'
4+
import actions from './actions'
5+
6+
const state = {
7+
firstName: 'Joe',
8+
lastName: 'Fusco',
9+
age: 32,
10+
gender: 'male'
11+
}
12+
13+
const mutations = {
14+
[type.GET_USER](state, action) {
15+
if(action.user !== null){
16+
const storedUser = JSON.parse(action.user);
17+
18+
Object.assign(state, storedUser)
19+
}
20+
},
21+
22+
[type.UPDATE_USER](state, action) {
23+
Object.assign(state, action.user)
24+
}
25+
}
26+
27+
export default {
28+
state,
29+
mutations,
30+
actions
31+
}

src/store/modules/user/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
'use strict';
22

3+
export const GET_USER = 'GET_USER'
4+
export const UPDATE_USER = 'UPDATE_USER'

0 commit comments

Comments
 (0)