-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccountSetting.js
More file actions
179 lines (163 loc) · 6.01 KB
/
AccountSetting.js
File metadata and controls
179 lines (163 loc) · 6.01 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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,
firstNameBTN: hiddenBTN,
lastNameBTN: 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 emailInputChange = (event) => {
let newEmail = event.currentTarget.value
if (newEmail != state.originUser.email) {
state.emailBTN = showBTN
} else {
state.emailBTN = hiddenBTN
}
state.user.email = newEmail
}
const firstNameInputChange = (event) => {
let newFirstName = event.currentTarget.value
if (newFirstName != state.originUser.first_name) {
state.firstNameBTN = showBTN
} else {
state.firstNameBTN = hiddenBTN
}
state.user.first_name = newFirstName
}
const lastNameInputChange = (event) => {
let newLastName = event.currentTarget.value
if (newLastName != state.originUser.last_name) {
state.lastNameBTN = showBTN
} else {
state.lastNameBTN = hiddenBTN
}
state.user.last_name = newLastName
}
const currentPasswordChange = (event) => {
currentPassword = event.currentTarget.value
if (currentPassword && newPassword && confirmNewPassword) {
state.updateBTN = showUpdateBTN
} else {
state.updateBTN = hiddenUpdateBTN
}
}
const newPasswordChange = (event) => {
newPassword = event.currentTarget.value
if (currentPassword && newPassword && confirmNewPassword) {
state.updateBTN = showUpdateBTN
} else {
state.updateBTN = hiddenUpdateBTN
}
}
const confirmNewPasswordChange = (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 submitFirstName = () => {
putAccount({first_name: state.user.first_name}).then(res=>{
if (res.status == 204) {
updateUser()
state.firstNameBTN = hiddenBTN
}
})
}
const submitLastName = () => {
putAccount({last_name: state.user.last_name}).then(res=>{
if (res.status == 204) {
updateUser()
state.lastNameBTN = 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 cancelFirstName = () => {
state.firstNameBTN = hiddenBTN
state.user.first_name = state.originUser.first_name
}
const cancelLastName = () => {
state.lastNameBTN = hiddenBTN
state.user.last_name = state.originUser.last_name
}
return {
...toRefs(state),
emailInputChange,
firstNameInputChange,
lastNameInputChange,
submitEmail,
cancelEmail,
submitFirstName,
cancelFirstName,
submitLastName,
cancelLastName,
currentPasswordChange,
newPasswordChange,
confirmNewPasswordChange,
submitPassowrd
}
},
}