-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccountSetting.js
More file actions
237 lines (217 loc) · 7.71 KB
/
AccountSetting.js
File metadata and controls
237 lines (217 loc) · 7.71 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import NavBar from "../components/NavBar.vue";
import NavBox from "../components/NavBox.vue";
import {onMounted, reactive, toRefs} from 'vue'
import MainNav from "../components/MainNav.vue";
import MainFooter from "../components/MainFooter.vue";
import { useRouter } from 'vue-router'
import { dealUser, getUser, putAccount, putAccountPassword } from "../services/user";
import { ElMessage } from "element-plus"
import { getIdentityProviders, getLinkedIdentities, unlinkIdentity } from "../services/identities";
export default {
name: "AccountSetting",
components: {
'nav-bar': NavBar,
'nav-box': NavBox,
'main-nav': MainNav,
'main-footer': MainFooter,
},
setup() {
const showBTN = "ui-inline-actions"
const hiddenBTN = "ui-inline-actions is-hidden"
const hiddenUpdateBTN = "ui-btn ui-btn--primary is-disabled"
const showUpdateBTN = "ui-btn ui-btn--primary"
var currentPassword, newPassword, confirmNewPassword
const router = useRouter()
const state = reactive({
user: {
username: null,
email: null,
first_name: null,
last_name: null
},
originUser: {
username: null,
email: null,
first_name: null,
last_name: null
},
emailBTN: hiddenBTN,
nameBTN: hiddenBTN,
firstNameBTN: hiddenBTN,
lastNameBTN: hiddenBTN,
updateBTN: hiddenUpdateBTN,
providers: [],
identities: [],
loading: false,
})
onMounted(async () => {
const res = await getUser()
const userData = dealUser(res)
state.user = { ...userData }
state.originUser = { ...userData }
await fetchProviders()
await fetchIdentities()
})
const fetchProviders = async () => {
state.loading = true
try {
const res = await getIdentityProviders()
state.providers = res.data?.results || []
} finally {
state.loading = false
}
}
const fetchIdentities = async () => {
state.loading = true
try {
const res = await getLinkedIdentities()
state.identities = res.data?.results || []
} finally {
state.loading = false
}
}
const handleLink = (provider) => {
if (!provider?.login_url) {
ElMessage.error("Provider login URL missing.")
return
}
window.location.href = provider.login_url
}
const handleUnlink = async (identity) => {
if (!identity?.id) {
return
}
await unlinkIdentity(identity.id)
ElMessage.success("Unlinked successfully.")
await fetchIdentities()
}
const updateUser = () => {
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)) {
ElMessage.error("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){
ElMessage.error("Current Password and New Password are the same.")
} else if( newPassword.length < 8){
ElMessage.error("this New Password is not valid.")
} else if (newPassword != confirmNewPassword){
ElMessage.error("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,
handleLink,
handleUnlink
}
},
}