Skip to content

Commit 2dd5caf

Browse files
edit profile
1 parent eeb073d commit 2dd5caf

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

src/api/account.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,25 @@ export const account = {
3535
},
3636

3737
async updateProfile (payload: UpdateProfileReq): Promise<UpdateProfileRes> {
38-
const res = await Vue.axios.patch(endpoints.account.me.updateProfile, payload)
38+
let formData: FormData | UpdateProfileReq
39+
let contentType: string
40+
41+
if (payload.avatar !== null) {
42+
formData = new FormData()
43+
Object.entries(payload).forEach(([key, value]) => {
44+
(formData as FormData).append(key, value)
45+
})
46+
contentType = 'multipart/formdata'
47+
} else {
48+
formData = payload
49+
contentType = 'application/json'
50+
}
51+
52+
const res = await Vue.axios.patch(endpoints.account.me.updateProfile, formData, {
53+
headers: {
54+
'Content-Type': contentType
55+
}
56+
})
3957
return res.data
4058
}
4159
}

src/interfaces/api/account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export declare interface UserDetailRes extends User {}
3939
export declare interface UpdateProfileReq {
4040
name?: User['name'];
4141
phone_number?: User['phone_number'];
42-
avatar?: User['avatar'];
42+
avatar?: File | null;
4343
}
4444

4545
export declare interface UpdateProfileRes extends User {}

src/views/account/MyInfo.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,20 @@
1717
v-if="user.avatar !== null"
1818
:src="user.avatar"
1919
:alt="user.name"
20-
></v-img>
20+
>
21+
<template #placeholder>
22+
<v-row
23+
class="fill-height ma-0"
24+
justify="center"
25+
align="center"
26+
>
27+
<v-progress-circular
28+
indeterminate
29+
color="primary"
30+
></v-progress-circular>
31+
</v-row>
32+
</template>
33+
</v-img>
2134
<span
2235
v-else
2336
class="white--text"

src/views/account/ProfileUpdate.vue

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
<h1>Edit profile</h1>
66
<v-divider></v-divider>
77

8-
<v-card
9-
v-if="localUser !== null"
10-
class="mt-5"
11-
>
8+
<v-card class="mt-5">
129
<v-card-text>
1310
<v-form>
1411
<v-text-field
@@ -17,12 +14,21 @@
1714
:error-messages="nameErrs"
1815
:error-count="nameErrs.length"
1916
></v-text-field>
17+
2018
<v-text-field
2119
v-model="phoneNumber"
2220
label="Phone number"
2321
:error-messages="phoneNumberErrs"
2422
:error-count="phoneNumberErrs.length"
2523
></v-text-field>
24+
25+
<v-file-input
26+
v-model="avatar"
27+
label="Avatar"
28+
prepend-icon="mdi-camera"
29+
:error-messages="avatarErrs"
30+
:error-count="avatarErrs.length"
31+
></v-file-input>
2632
</v-form>
2733
</v-card-text>
2834

@@ -66,15 +72,32 @@ export default class ProfileUpdate extends Vue {
6672
]
6773
6874
user!: User
75+
6976
name: User['name'] = ''
7077
phoneNumber: User['phone_number'] = ''
78+
avatar: File | null = null
79+
originalAvatar: File | null = null
80+
7181
nameErrs: string[] = []
7282
phoneNumberErrs: string[] = []
83+
avatarErrs: string[] = []
84+
7385
loading = false
7486
7587
created (): void {
7688
this.name = this.user.name
7789
this.phoneNumber = this.user.phone_number
90+
91+
if (this.user.avatar !== null) {
92+
Vue.axios.get(this.user.avatar)
93+
.then(res => {
94+
const parts = this.user.avatar.split('/')
95+
const avatarName = parts[parts.length - 1]
96+
const file = new File([res.data], avatarName)
97+
this.avatar = file
98+
this.originalAvatar = file
99+
})
100+
}
78101
}
79102
80103
saveProfile (): void {
@@ -86,6 +109,10 @@ export default class ProfileUpdate extends Vue {
86109
phone_number: this.phoneNumber
87110
}
88111
112+
if (this.avatar !== this.originalAvatar) {
113+
payload.avatar = this.avatar
114+
}
115+
89116
this.$store.dispatch('account/updateProfile', payload)
90117
.then(() => {
91118
this.$router.push({ name: 'MyInfo' })

0 commit comments

Comments
 (0)