Skip to content

Commit 46bde58

Browse files
committed
fixup! Remake profile picture saving with Vue
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 8da0efc commit 46bde58

1 file changed

Lines changed: 51 additions & 27 deletions

File tree

apps/settings/src/components/PersonalInfo/AvatarSection.vue

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@
2626
:readable="avatar.readable"
2727
:scope.sync="avatar.scope" />
2828

29-
<div v-if="!imgSrc" class="avatar__preview">
30-
<span v-if="loading" class="icon-loading" />
31-
<img v-else
32-
class="cropped-image"
33-
:src="avatarUrl">
29+
<div v-if="!cropping" class="avatar__preview">
30+
<Avatar
31+
:user="userId"
32+
:aria-label="t('settings', 'Your profile picture')"
33+
:disabled-menu="true"
34+
:disabled-tooltip="true"
35+
:show-user-status="false"
36+
:size="180"
37+
:key="avatarKey"
38+
/>
39+
<Button
40+
@click="refreshAvatar"
41+
/>
3442

3543
<template v-if="avatarChangeSupported">
3644
<div class="avatar__buttons">
@@ -95,6 +103,7 @@ import { getFilePickerBuilder } from '@nextcloud/dialogs'
95103
import { getCurrentUser } from '@nextcloud/auth'
96104
import { generateUrl } from '@nextcloud/router'
97105
import { loadState } from '@nextcloud/initial-state'
106+
import { emit, subscribe } from '@nextcloud/event-bus'
98107
import 'cropperjs/dist/cropper.css'
99108
100109
import Upload from 'vue-material-design-icons/Upload'
@@ -131,24 +140,54 @@ export default {
131140
return {
132141
avatar: { ...avatar, readable: NAME_READABLE_ENUM[avatar.name] },
133142
avatarChangeSupported,
134-
avatarUrl: null,
135-
data: null,
136143
imgSrc: null,
137-
loading: false,
144+
cropping: false,
145+
userId: getCurrentUser().uid,
146+
displayName: getCurrentUser().displayName,
147+
avatarKey: 'key',
138148
}
139149
},
140150
141-
beforeMount() {
142-
this.updateAvatar()
151+
created() {
152+
subscribe('settings:display-name:updated', this.handleDisplayNameUpdate)
153+
subscribe('settings:avatar:updated', this.handleAvatarUpdate)
154+
// FIXME refresh all other avatars on the page when updated
143155
},
144156
157+
beforeDestroy() {
158+
unsubscribe('settings:display-name:updated', this.handleDisplayNameUpdate)
159+
unsubscribe('settings:avatar:updated', this.handleAvatarUpdate)
160+
},
161+
162+
145163
computed: {
146164
inputId() {
147165
return `account-property-${ACCOUNT_PROPERTY_ENUM.AVATAR}`
148166
},
149167
},
150168
151169
methods: {
170+
handleDisplayNameUpdate(displayName) {
171+
this.avatarKey = displayName
172+
173+
// FIXME update the avatar version only when a refresh is needed
174+
// If displayName based and displayName updated: refresh
175+
// If displayName based and image updated: refresh
176+
// If image and image updated: refresh
177+
// If image and displayName updated: do not refresh
178+
oc_userconfig.avatar.version = displayName
179+
},
180+
181+
handleAvatarUpdate(timestamp) {
182+
this.avatarKey = timestamp
183+
},
184+
185+
refreshAvatar() {
186+
this.avatarKey = Math.random().toString(36).substring(2)
187+
oc_userconfig.avatar.version = this.avatarKey
188+
console.log(`avatar key: ${this.avatarKey}`)
189+
},
190+
152191
cropImage() {
153192
this.imgSrc = null
154193
this.saveAvatar()
@@ -167,6 +206,8 @@ export default {
167206
this.$nextTick(() => this.$refs.cropper.replace(event.target.result))
168207
}
169208
reader.readAsDataURL(file)
209+
emit('settings:avatar:updated', Date.now())
210+
// FIXME emit event when avatar image has been updated and refresh all avatars on the page
170211
} else {
171212
alert('Sorry, FileReader API not supported')
172213
}
@@ -177,7 +218,6 @@ export default {
177218
},
178219
179220
saveAvatar() {
180-
this.loading = true
181221
this.$refs.cropper.getCroppedCanvas().toBlob((blob) => {
182222
const formData = new FormData()
183223
formData.append('files[]', blob)
@@ -186,7 +226,6 @@ export default {
186226
'Content-Type': 'multipart/form-data',
187227
},
188228
}).then(() => {
189-
this.updateAvatar()
190229
})
191230
})
192231
},
@@ -197,24 +236,9 @@ export default {
197236
this.imgSrc = generateUrl('/avatar/tmp') + '?requesttoken=' + encodeURIComponent(OC.requestToken) + '#' + Math.floor(Math.random() * 1000)
198237
},
199238
200-
updateAvatar() {
201-
this.loading = true
202-
const newAvatarUrl = generateUrl('/avatar/') + getCurrentUser().uid + '/256?v=' + Date.now()
203-
const img = new Image()
204-
img.onload = () => {
205-
this.loading = false
206-
this.avatarUrl = newAvatarUrl
207-
oc_userconfig.avatar.version = Date.now()
208-
}
209-
img.src = newAvatarUrl
210-
// FIXME emit an event to update all avatars on the page here
211-
},
212-
213239
async removeAvatar() {
214-
this.loading = true
215240
await axios.delete(generateUrl('/avatar/'))
216241
window.oc_userconfig.avatar.generated = true
217-
this.updateAvatar()
218242
},
219243
},
220244
}

0 commit comments

Comments
 (0)