-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccountSetting.js
More file actions
149 lines (137 loc) · 4.98 KB
/
AccountSetting.js
File metadata and controls
149 lines (137 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import NavBar from "../components/NavBar.vue";
import NavBox from "../components/NavBox.vue";
import {onBeforeMount, reactive, toRefs} from 'vue'
import MainNav from "../components/MainNav.vue";
import { useRouter } from 'vue-router'
import { putAccount, putAccountPassword } from "../services/user";
import { Toast } from "vant"
export default {
name: "AccountSetting",
components: {
'nav-bar': NavBar,
'nav-box': NavBox,
'main-nav': MainNav,
},
setup() {
const showBTN = "flex items-center mb2"
const hiddenBTN = "flex items-center mb2 clip"
const hiddenUpdateBTN = "async-button default hk-button--disabled-primary ember-view"
const showUpdateBTN = "async-button default hk-button--primary ember-view"
var currentPassword, newPassword, confirmNewPassword
const router = useRouter()
const state = reactive({
user: Object,
originUser: Object,
emailBTN: hiddenBTN,
nameBTN: hiddenBTN,
updateBTN: hiddenUpdateBTN
})
onBeforeMount(async () => {
state.user = JSON.parse(sessionStorage.getItem('user'))
state.originUser = JSON.parse(sessionStorage.getItem('user'))
})
const updateUser = () => {
sessionStorage.setItem('user', JSON.stringify(state.user))
state.originUser = state.user
}
const emailInputChage = (event) => {
let newEmail = event.currentTarget.value
if (newEmail != state.originUser.email) {
state.emailBTN = showBTN
} else {
state.emailBTN = hiddenBTN
}
state.user.email = newEmail
}
const nameInputChage = (event) => {
let newName = event.currentTarget.value
if (newName != state.originUser.username) {
state.nameBTN = showBTN
} else {
state.nameBTN = hiddenBTN
}
state.user.username = newName
}
const currentPasswordChage = (event) => {
currentPassword = event.currentTarget.value
if (currentPassword && newPassword && confirmNewPassword) {
state.updateBTN = showUpdateBTN
} else {
state.updateBTN = hiddenUpdateBTN
}
}
const newPasswordChage = (event) => {
newPassword = event.currentTarget.value
if (currentPassword && newPassword && confirmNewPassword) {
state.updateBTN = showUpdateBTN
} else {
state.updateBTN = hiddenUpdateBTN
}
}
const confirmNewPasswordChage = (event) => {
confirmNewPassword = event.currentTarget.value
if (currentPassword && newPassword && confirmNewPassword) {
state.updateBTN = showUpdateBTN
} else {
state.updateBTN = hiddenUpdateBTN
}
}
const submitEmail = () => {
var re = /^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$/;
if(!re.test(state.user.email)) {
Toast.fail("this email is not valid.")
} else {
putAccount({email: state.user.email}).then(res=>{
if (res.status == 204) {
updateUser()
state.emailBTN = hiddenBTN
}
})
}
}
const submitName = () => {
putAccount({username: state.user.username}).then(res=>{
if (res.status == 204) {
updateUser()
state.nameBTN = hiddenBTN
}
})
}
const submitPassowrd = () => {
if (currentPassword == newPassword){
Toast.fail("Current Password and New Password are the same.")
} else if( newPassword.length >= 8){
Toast.fail("this New Password is not valid.")
} else if (newPassword != confirmNewPassword){
Toast.fail("New Password Confirm and New Password are different.")
}else {
putAccountPassword({password: currentPassword, new_password: newPassword}).then(res=>{
if (res.status == 204) {
router.push({ path: `/` })
}
})
}
}
const cancelEmail = () => {
state.emailBTN = hiddenBTN
state.user.email = state.originUser.email
}
const cancelName = () => {
state.nameBTN = hiddenBTN
state.user.username = state.originUser.username
}
return {
...toRefs(state),
emailInputChage,
nameInputChage,
submitEmail,
cancelEmail,
submitName,
cancelName,
currentPasswordChage,
newPasswordChage,
confirmNewPasswordChage,
submitPassowrd
}
},
}