From 48edc0c8fcc6c63e432c9b6d78f14af086b56900 Mon Sep 17 00:00:00 2001 From: Rinpatch Date: Thu, 13 Dec 2018 11:25:03 +0300 Subject: [PATCH] Refactor arrays to individual options --- src/components/user_settings/user_settings.js | 94 +++++++++++++------ .../user_settings/user_settings.vue | 32 +++---- 2 files changed, 83 insertions(+), 43 deletions(-) diff --git a/src/components/user_settings/user_settings.js b/src/components/user_settings/user_settings.js index 504976a5..0f3f0859 100644 --- a/src/components/user_settings/user_settings.js +++ b/src/components/user_settings/user_settings.js @@ -5,7 +5,6 @@ import fileSizeFormatService from '../../services/file_size_format/file_size_for const UserSettings = { data () { return { - uploaderror: [null, null, null], newname: this.$store.state.users.currentUser.name, newbio: this.$store.state.users.currentUser.description, newlocked: this.$store.state.users.currentUser.locked, @@ -15,8 +14,16 @@ const UserSettings = { followImportError: false, followsImported: false, enableFollowsExport: true, - uploading: [ false, false, false, false ], - previews: [ null, null, null ], + avatarUploading: false, + bannerUploading: false, + backgroundUploading: false, + followListUploading: false, + avatarPreview: null, + bannerPreview: null, + backgroundPreview: null, + avatarUploadError: null, + bannerUploadError: null, + backgroundUploadError: null, deletingAccount: false, deleteAccountConfirmPasswordInput: '', deleteAccountError: false, @@ -71,26 +78,49 @@ const UserSettings = { uploadFile (slot, e) { const file = e.target.files[0] if (!file) { return } + var error = () => {} + switch (slot) { + case 0: + error = (error) => { this.avatarUploadError = error } + break + case 1: + error = (error) => { this.bannerUploadError = error } + break + case 2: + error = (error) => { this.backgroundUploadError = error } + break + } var limit = [this.$store.state.instance.avatarlimit, this.$store.state.instance.bannerlimit, this.$store.state.instance.backgroundlimit] if (file.size > limit[slot]) { const filesize = fileSizeFormatService.fileSizeFormat(file.size) const allowedsize = fileSizeFormatService.fileSizeFormat(limit[slot]) - this.$set(this.uploaderror, slot, this.$t('upload.error.base') + ' ' + this.$t('upload.error.file_too_big', {filesize: filesize.num, filesizeunit: filesize.unit, allowedsize: allowedsize.num, allowedsizeunit: allowedsize.unit})) + error(this.$t('upload.error.base') + ' ' + this.$t('upload.error.file_too_big', {filesize: filesize.num, filesizeunit: filesize.unit, allowedsize: allowedsize.num, allowedsizeunit: allowedsize.unit})) return } // eslint-disable-next-line no-undef const reader = new FileReader() reader.onload = ({target}) => { const img = target.result - this.previews[slot] = img - this.$forceUpdate() // just changing the array with the index doesn't update the view + var preview = () => {} + switch (slot) { + case 0: + preview = (preview) => { this.avatarPreview = preview } + break + case 1: + preview = (preview) => { this.bannerPreview = preview } + break + case 2: + preview = (preview) => { this.backgroundPreview = preview } + break + } + preview(img) } reader.readAsDataURL(file) }, submitAvatar () { - if (!this.previews[0]) { return } + if (!this.avatarPreview) { return } - let img = this.previews[0] + let img = this.avatarPreview // eslint-disable-next-line no-undef let imginfo = new Image() let cropX, cropY, cropW, cropH @@ -106,25 +136,35 @@ const UserSettings = { cropX = Math.floor((imginfo.width - imginfo.height) / 2) cropW = imginfo.height } - this.uploading[0] = true + this.avatarUploading = true this.$store.state.api.backendInteractor.updateAvatar({params: {img, cropX, cropY, cropW, cropH}}).then((user) => { if (!user.error) { this.$store.commit('addNewUsers', [user]) this.$store.commit('setCurrentUser', user) - this.previews[0] = null + this.avatarPreview = null } else { - this.$set(this.uploaderror, 0, this.$t('upload.error.base') + user.error) + this.avatarUploadError = this.$t('upload.error.base') + user.error } - this.uploading[0] = false + this.avatarUploading = false }) }, clearUploadError (slot) { - this.$set(this.uploaderror, slot, null) + switch (slot) { + case 0: + this.avatarUploadError = null + break + case 1: + this.bannerUploadError = null + break + case 2: + this.backgroundUploadError = null + break + } }, submitBanner () { - if (!this.previews[1]) { return } + if (!this.bannerPreview) { return } - let banner = this.previews[1] + let banner = this.bannerPreview // eslint-disable-next-line no-undef let imginfo = new Image() /* eslint-disable camelcase */ @@ -134,24 +174,24 @@ const UserSettings = { height = imginfo.height offset_top = 0 offset_left = 0 - this.uploading[1] = true + this.bannerUploading = true this.$store.state.api.backendInteractor.updateBanner({params: {banner, offset_top, offset_left, width, height}}).then((data) => { if (!data.error) { let clone = JSON.parse(JSON.stringify(this.$store.state.users.currentUser)) clone.cover_photo = data.url this.$store.commit('addNewUsers', [clone]) this.$store.commit('setCurrentUser', clone) - this.previews[1] = null + this.bannerPreview = null } else { - this.$set(this.uploaderror, 1, this.$t('upload.error.base') + data.error) + this.bannerUploadError = this.$t('upload.error.base') + data.error } - this.uploading[1] = false + this.bannerUploading = false }) /* eslint-enable camelcase */ }, submitBg () { - if (!this.previews[2]) { return } - let img = this.previews[2] + if (!this.backgroundPreview) { return } + let img = this.backgroundPreview // eslint-disable-next-line no-undef let imginfo = new Image() let cropX, cropY, cropW, cropH @@ -160,22 +200,22 @@ const UserSettings = { cropY = 0 cropW = imginfo.width cropH = imginfo.width - this.uploading[2] = true + this.backgroundUploading = true this.$store.state.api.backendInteractor.updateBg({params: {img, cropX, cropY, cropW, cropH}}).then((data) => { if (!data.error) { let clone = JSON.parse(JSON.stringify(this.$store.state.users.currentUser)) clone.background_image = data.url this.$store.commit('addNewUsers', [clone]) this.$store.commit('setCurrentUser', clone) - this.previews[2] = null + this.backgroundPreview = null } else { - this.$set(this.uploaderror, 2, this.$t('upload.error.base') + data.error) + this.backgroundUploadError = this.$t('upload.error.base') + data.error } - this.uploading[2] = false + this.backgroundUploading = false }) }, importFollows () { - this.uploading[3] = true + this.followListUploading = true const followList = this.followList this.$store.state.api.backendInteractor.followImport({params: followList}) .then((status) => { @@ -184,7 +224,7 @@ const UserSettings = { } else { this.followImportError = true } - this.uploading[3] = false + this.followListUploading = false }) }, /* This function takes an Array of Users diff --git a/src/components/user_settings/user_settings.vue b/src/components/user_settings/user_settings.vue index 22c4fe41..5883775c 100644 --- a/src/components/user_settings/user_settings.vue +++ b/src/components/user_settings/user_settings.vue @@ -36,15 +36,15 @@

{{$t('settings.current_avatar')}}

{{$t('settings.set_new_avatar')}}

- +
- - -
- Error: {{ uploaderror[0] }} + + +
+ Error: {{ avatarUploadError }}
@@ -53,30 +53,30 @@

{{$t('settings.current_profile_banner')}}

{{$t('settings.set_new_profile_banner')}}

- +
- - -
- Error: {{ uploaderror[1] }} + + +
+ Error: {{ bannerUploadError }}

{{$t('settings.profile_background')}}

{{$t('settings.set_new_profile_background')}}

- +
- - -
- Error: {{ uploaderror[2] }} + + +
+ Error: {{ backgroundUploadError }}
@@ -125,7 +125,7 @@
- +