Skip to content

Commit 28f0c33

Browse files
author
Bhanu Teja P
committed
Add Profile Edit Page
1 parent 188e22f commit 28f0c33

File tree

13 files changed

+279
-63
lines changed

13 files changed

+279
-63
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = {
1111
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
1212
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
1313
'import/extensions': 'off',
14-
'import/no-cycle': 'off'
14+
'import/no-cycle': 'off',
15+
'consistent-return': 'off',
1516
},
1617
parserOptions: {
1718
parser: 'babel-eslint',

src/components/CustomForm.vue

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<template>
2+
<v-row align="start" justify="center">
3+
<v-col sm="7">
4+
<card>
5+
<template v-slot:title>{{ title }}</template>
6+
<v-form>
7+
<form-alerts :alerts="alerts"></form-alerts>
8+
<form-fields :fields="fields"></form-fields>
9+
</v-form>
10+
<template v-slot:actions>
11+
<form-buttons :buttons="buttons"></form-buttons>
12+
</template>
13+
</card>
14+
</v-col>
15+
</v-row>
16+
</template>
17+
18+
<script>
19+
import {
20+
Card,
21+
FormFields,
22+
FormAlerts,
23+
FormButtons,
24+
} from '.';
25+
26+
export default {
27+
name: 'CustomForm',
28+
props: {
29+
title: String,
30+
alerts: Array,
31+
fields: Array,
32+
buttons: Array,
33+
},
34+
components: {
35+
Card,
36+
FormFields,
37+
FormAlerts,
38+
FormButtons,
39+
},
40+
};
41+
</script>
42+
43+
<style scoped>
44+
45+
</style>

src/components/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { default as Card } from './Card';
33
export { default as FormAlerts } from './FormAlerts';
44
export { default as FormFields } from './FormFields';
55
export { default as FormButtons } from './FormButtons';
6+
export { default as CustomForm } from './CustomForm';

src/components/layout/Header.vue

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@
44
<span @click="$router.push('/')">Open Rank</span>
55
</v-toolbar-title>
66
<v-spacer></v-spacer>
7-
<v-btn text to="/login">
7+
<v-btn text to="/login" v-if="!isLoggedIn">
88
<span class="mr-2">Login</span>
99
</v-btn>
10-
<v-btn text to="/register">
10+
<v-btn text to="/register" v-if="!isLoggedIn">
1111
<span class="mr-2">SignUp</span>
1212
</v-btn>
13+
<v-btn text to="/profile" v-if="isLoggedIn">
14+
<span class="mr-2">Profile</span>
15+
</v-btn>
1316
</v-app-bar>
1417
</template>
1518

1619
<script>
20+
import { createNamespacedHelpers } from 'vuex';
21+
22+
const { mapGetters } = createNamespacedHelpers('authentication/user');
23+
1724
export default {
1825
name: 'Header',
26+
computed: {
27+
...mapGetters([
28+
'isLoggedIn',
29+
]),
30+
},
1931
};
2032
</script>
2133

src/router/index.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import Vue from 'vue';
22
import Router from 'vue-router';
3-
import Home from '../views/Home';
4-
import About from '../views/About';
5-
import Register from '../views/Register';
6-
import Login from '../views/Login';
3+
import {
4+
Home,
5+
About,
6+
Register,
7+
Login,
8+
Profile,
9+
} from '../views';
710

811
Vue.use(Router);
912

@@ -31,5 +34,10 @@ export default new Router({
3134
name: 'login',
3235
component: Login,
3336
},
37+
{
38+
path: '/profile',
39+
name: 'profile',
40+
component: Profile,
41+
},
3442
],
3543
});

src/store/modules/authentication/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import register from './register';
22
import login from './login';
3+
import user from './user';
34

45
export default {
56
namespaced: true,
@@ -15,5 +16,6 @@ export default {
1516
modules: {
1617
register,
1718
login,
19+
user,
1820
},
1921
};

src/store/modules/authentication/login.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export default {
3030
await HTTP().post('/login', {
3131
email,
3232
password,
33-
}).then(({ data: { message } }) => {
33+
}).then(({ data: { data: { token }, message } }) => {
34+
commit('authentication/user/setToken', token, { root: true });
3435
commit('setSuccessMsg', message);
3536
});
3637
},

src/store/modules/authentication/register.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ export default {
44
namespaced: true,
55
strict: true,
66
state: {
7-
name: '',
8-
email: '',
9-
password: '',
10-
passwordConfirmed: '',
11-
successMsg: '',
7+
name: null,
8+
email: null,
9+
password: null,
10+
passwordConfirmed: null,
11+
successMsg: null,
1212
},
1313
mutations: {
1414
setName(state, name) {
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import HTTP from '../../../http';
2+
3+
export default {
4+
namespaced: true,
5+
strict: true,
6+
state: {
7+
token: null,
8+
id: null,
9+
name: null,
10+
email: null,
11+
successMsg: null,
12+
},
13+
mutations: {
14+
setToken(state, token) {
15+
state.token = token;
16+
},
17+
setId(state, id) {
18+
state.id = id;
19+
},
20+
setName(state, name) {
21+
state.name = name;
22+
},
23+
setEmail(state, email) {
24+
state.emeail = email;
25+
},
26+
setSuccessMsg(state, message) {
27+
state.successMsg = message;
28+
},
29+
},
30+
actions: {
31+
async fetchUser({ state: { token }, commit }) {
32+
await HTTP().get('/user', {
33+
headers: {
34+
Authorization: `Bearer ${token}`,
35+
},
36+
}).then(({ data: { data: { id, name, email }, message } }) => {
37+
commit('setId', id);
38+
commit('setName', name);
39+
commit('setEmail', email);
40+
commit('setSuccessMsg', message);
41+
});
42+
},
43+
async updateUser({
44+
state: {
45+
id,
46+
name,
47+
email,
48+
token,
49+
},
50+
commit,
51+
}) {
52+
await HTTP().post(`/users/${id}`, {
53+
id,
54+
name,
55+
email,
56+
}, {
57+
headers: {
58+
Authorization: `Bearer ${token}`,
59+
},
60+
}).then(({ data: { user, message } }) => {
61+
commit('setId', user.id);
62+
commit('setName', user.name);
63+
commit('setEmail', user.email);
64+
commit('setSuccessMSg', message);
65+
});
66+
},
67+
},
68+
getters: {
69+
isLoggedIn(state) {
70+
return !!state.token;
71+
},
72+
},
73+
};

src/views/Login.vue

+12-25
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
<template>
2-
<v-row align="start" justify="center">
3-
<v-col sm="7">
4-
<card>
5-
<template v-slot:title>Login</template>
6-
<v-form>
7-
<form-alerts :alerts="alerts"></form-alerts>
8-
<form-fields :fields="fields"></form-fields>
9-
</v-form>
10-
<template v-slot:actions>
11-
<form-buttons :buttons="buttons"></form-buttons>
12-
</template>
13-
</card>
14-
</v-col>
15-
</v-row>
2+
<custom-form title="Login" :alerts="alerts" :fields="fields" :buttons="buttons"></custom-form>
163
</template>
174

185
<script>
196
import { createNamespacedHelpers } from 'vuex';
20-
21-
import {
22-
Card,
23-
FormFields,
24-
FormAlerts,
25-
FormButtons,
26-
} from '../components';
7+
import { CustomForm } from '../components';
278
289
const { mapState, mapMutations, mapActions } = createNamespacedHelpers('authentication/login');
10+
const { mapGetters } = createNamespacedHelpers('authentication/user');
2911
3012
export default {
3113
name: 'Login',
@@ -34,6 +16,9 @@ export default {
3416
};
3517
},
3618
computed: {
19+
...mapGetters([
20+
'isLoggedIn',
21+
]),
3722
...mapState([
3823
'email',
3924
'password',
@@ -86,11 +71,13 @@ export default {
8671
'login',
8772
]),
8873
},
74+
mounted() {
75+
if (this.isLoggedIn) {
76+
return this.$router.push('/profile');
77+
}
78+
},
8979
components: {
90-
Card,
91-
FormFields,
92-
FormAlerts,
93-
FormButtons,
80+
CustomForm,
9481
},
9582
};
9683
</script>

src/views/Profile.vue

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<template>
2+
<custom-form title="Profile" :alerts="alerts" :fields="fields" :buttons="buttons"></custom-form>
3+
</template>
4+
5+
<script>
6+
import { createNamespacedHelpers } from 'vuex';
7+
import { CustomForm } from '../components';
8+
9+
const {
10+
mapState,
11+
mapMutations,
12+
mapActions,
13+
mapGetters,
14+
} = createNamespacedHelpers('authentication/user');
15+
16+
export default {
17+
name: 'Profile',
18+
data() {
19+
return {
20+
};
21+
},
22+
computed: {
23+
...mapState([
24+
'id',
25+
'name',
26+
'email',
27+
'successMsg',
28+
]),
29+
...mapGetters([
30+
'isLoggedIn',
31+
]),
32+
alerts() {
33+
return [
34+
{
35+
type: 'success',
36+
message: this.successMsg,
37+
},
38+
];
39+
},
40+
fields() {
41+
return [
42+
{
43+
label: 'Name',
44+
placeholder: 'Enter your name',
45+
type: 'text',
46+
icon: 'mdi-rename-box',
47+
value: this.name,
48+
input: this.setName,
49+
},
50+
{
51+
label: 'Email',
52+
placeholder: 'Enter Email',
53+
type: 'email',
54+
icon: 'mdi-email',
55+
value: this.email,
56+
input: this.setEmail,
57+
},
58+
];
59+
},
60+
buttons() {
61+
return [
62+
{
63+
icon: 'mdi-account',
64+
value: 'Update',
65+
click: this.updateUser,
66+
},
67+
];
68+
},
69+
},
70+
methods: {
71+
...mapMutations([
72+
'setName',
73+
'setEmail',
74+
]),
75+
...mapActions([
76+
'fetchUser',
77+
'updateUser',
78+
]),
79+
},
80+
mounted() {
81+
if (!this.isLoggedIn) {
82+
return this.$router.push('/login');
83+
}
84+
this.$store.dispatch('authentication/fetchUser');
85+
},
86+
components: {
87+
CustomForm,
88+
},
89+
};
90+
</script>
91+
92+
<style scoped>
93+
94+
</style>

0 commit comments

Comments
 (0)