Skip to content

Commit

Permalink
πŸ› (#2345): fix upload state reactivity
Browse files Browse the repository at this point in the history
Signed-off-by: Vinicius Reis <vinicius.reis@nextcloud.com>
  • Loading branch information
Vinicius Reis committed May 25, 2022
1 parent a38a69a commit fbe7f11
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
12 changes: 9 additions & 3 deletions src/components/EditorDraggable.provider.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
export const IS_UPLOADING_IMAGES = Symbol('editor:is-uploading-images')
export const STATE_UPLOADING = Symbol('state:uploading-state')
export const ACTION_IMAGE_PROMPT = Symbol('editor:action:image-prompt')
export const ACTION_CHOOSE_LOCAL_IMAGE = Symbol('editor:action:upload-image')

export const useIsUploadingImagesMixin = {
export const useUploadingStateMixin = {
inject: {
$isUploadingImages: { from: IS_UPLOADING_IMAGES, default: false },
$uploadingState: {
from: STATE_UPLOADING,
default: {
x: true,
isUploadingImages: false,
},
},
},
}

Expand Down
42 changes: 27 additions & 15 deletions src/components/EditorDraggable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
import {
ACTION_IMAGE_PROMPT,
ACTION_CHOOSE_LOCAL_IMAGE,
IS_UPLOADING_IMAGES,
STATE_UPLOADING,
} from './EditorDraggable.provider.js'

const IMAGE_MIMES = [
Expand Down Expand Up @@ -81,17 +81,22 @@ export default {
[ACTION_CHOOSE_LOCAL_IMAGE]: {
get: () => this.chooseLocalImage,
},
[IS_UPLOADING_IMAGES]: {
get: () => this.isUploadingImages,
[STATE_UPLOADING]: {
get: () => this.state,
},
})

return val
},
data: () => ({
draggedOver: false,
isUploadingImages: false,
}),
data() {
return {
draggedOver: false,
// make it reactive to be used inject/provide
state: {
isUploadingImages: false,
},
}
},
computed: {
imagePath() {
return this.$file.relativePath.split('/').slice(0, -1).join('/')
Expand Down Expand Up @@ -143,12 +148,19 @@ export default {
return
}

return this.$syncService.uploadImage(file).then((response) => {
this.insertAttachmentImage(response.data?.name, response.data?.id, position, response.data?.dirname)
}).catch((error) => {
console.error(error)
showError(error?.response?.data?.error)
})
this.state.isUploadingImages = true

return this.$syncService.uploadImage(file)
.then((response) => {
this.insertAttachmentImage(response.data?.name, response.data?.id, position, response.data?.dirname)
})
.catch((error) => {
console.error(error)
showError(error?.response?.data?.error)
})
.then(() => {
this.state.isUploadingImages = false
})
},
showImagePrompt() {
const currentUser = getCurrentUser()
Expand All @@ -161,15 +173,15 @@ export default {
}, false, [], true, undefined, this.imagePath)
},
insertImagePath(imagePath) {
this.isUploadingImages = true
this.state.isUploadingImages = true

return this.$syncService.insertImageFile(imagePath).then((response) => {
this.insertAttachmentImage(response.data?.name, response.data?.id, null, response.data?.dirname)
}).catch((error) => {
console.error(error)
showError(error?.response?.data?.error || error.message)
}).then(() => {
this.isUploadingImages = false
this.state.isUploadingImages = false
})
},
insertAttachmentImage(name, fileId, position = null, dirname = '') {
Expand Down
15 changes: 10 additions & 5 deletions src/components/Menu/ActionImageUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
:aria-label="actionEntry.label"
aria-haspopup>
<template #icon>
<component :is="$isUploadingImages ? 'Loading' : actionEntry.icon"
<component :is="isUploadingImages ? 'Loading' : actionEntry.icon"
:title="actionEntry.label"
:aria-label="actionEntry.label"
aria-haspopup />
</template>
<ActionButton close-after-click
:disabled="$isUploadingImages"
:disabled="isUploadingImages"
:data-text-action-entry="`${actionEntry._key}-upload`"
@click="$callChooseLocalImage">
<template #icon>
Expand All @@ -42,7 +42,7 @@
</ActionButton>
<ActionButton v-if="!$isPublic"
close-after-click
:disabled="$isUploadingImages"
:disabled="isUploadingImages"
:data-text-action-entry="`${actionEntry._key}-insert`"
@click="$callImagePrompt">
<template #icon>
Expand All @@ -62,7 +62,7 @@ import { useIsPublicMixin } from '../EditorWrapper.provider.js'
import { BaseActionEntry } from './ActionEntry.mixin.js'
import {
useActionImagePromptMixin,
useIsUploadingImagesMixin,
useUploadingStateMixin,
useActionChooseLocalImageMixin,
} from '../EditorDraggable.provider.js'

Expand All @@ -79,8 +79,13 @@ export default {
mixins: [
useIsPublicMixin,
useActionImagePromptMixin,
useIsUploadingImagesMixin,
useUploadingStateMixin,
useActionChooseLocalImageMixin,
],
computed: {
isUploadingImages() {
return this.$uploadingState.isUploadingImages
},
},
}
</script>

0 comments on commit fbe7f11

Please sign in to comment.