Add progress indicator and basic uplaod queue

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2020-04-04 21:24:15 +02:00
parent 6cab67cb11
commit 22fb70f957
5 changed files with 28 additions and 35 deletions

View File

@@ -102,18 +102,15 @@ export default {
}, },
}, },
methods: { methods: {
dragEnter() {
},
dragLeave() {
},
handleDropFiles(event) { handleDropFiles(event) {
this.isDraggingOver = false this.isDraggingOver = false
if (this.isReadOnly) { if (this.isReadOnly) {
return return
} }
this.onLocalAttachmentSelected(event.dataTransfer.files[0]) const files = event.dataTransfer.files
for (let file of files) {
this.onLocalAttachmentSelected(file)
}
event.dataTransfer.value = '' event.dataTransfer.value = ''
}, },
}, },

View File

@@ -28,9 +28,14 @@
<input ref="localAttachments" <input ref="localAttachments"
type="file" type="file"
style="display: none;" style="display: none;"
multiple
@change="handleUploadFile"> @change="handleUploadFile">
<div class="attachment-list"> <div class="attachment-list">
<ul> <ul>
<li v-for="attachment in uploadQueue">
{{ attachment.name }}
<progress :value="attachment.progress" max="100"></progress>
</li>
<li v-for="attachment in attachments" <li v-for="attachment in attachments"
:key="attachment.id" :key="attachment.id"
class="attachment" class="attachment"
@@ -109,7 +114,7 @@ export default {
} }
}, },
attachments() { attachments() {
return this.$store.getters.attachmentsByCard(this.card.id) return this.$store.getters.attachmentsByCard(this.card.id).sort((a, b) => b.id - a.id)
}, },
formattedFileSize() { formattedFileSize() {
return (filesize) => formatFileSize(filesize) return (filesize) => formatFileSize(filesize)
@@ -134,22 +139,11 @@ export default {
this.$store.dispatch('fetchAttachments', this.card.id) this.$store.dispatch('fetchAttachments', this.card.id)
}, },
methods: { methods: {
dragEnter() {
},
dragLeave() {
},
handleDropFiles(event) {
this.isDraggingOver = false
if (this.isReadOnly) {
return
}
this.onLocalAttachmentSelected(event.dataTransfer.files[0])
event.dataTransfer.value = ''
},
handleUploadFile(event) { handleUploadFile(event) {
this.onLocalAttachmentSelected(event.target.files[0]) const files = event.target.files ?? []
for (let file of files) {
this.onLocalAttachmentSelected(file)
}
event.target.value = '' event.target.value = ''
}, },
clickAddNewAttachmment() { clickAddNewAttachmment() {

View File

@@ -23,6 +23,11 @@ import { showError } from '@nextcloud/dialogs'
import { formatFileSize } from '@nextcloud/files' import { formatFileSize } from '@nextcloud/files'
export default { export default {
data() {
return {
uploadQueue: {},
}
},
methods: { methods: {
async onLocalAttachmentSelected(file) { async onLocalAttachmentSelected(file) {
if (this.maxUploadSize > 0 && file.size > this.maxUploadSize) { if (this.maxUploadSize > 0 && file.size > this.maxUploadSize) {
@@ -34,6 +39,7 @@ export default {
return return
} }
this.$set(this.uploadQueue, file.name, { name: file.name, progress: 0})
const bodyFormData = new FormData() const bodyFormData = new FormData()
bodyFormData.append('cardId', this.cardId) bodyFormData.append('cardId', this.cardId)
bodyFormData.append('type', 'deck_file') bodyFormData.append('type', 'deck_file')
@@ -42,8 +48,11 @@ export default {
await this.$store.dispatch('createAttachment', { cardId: this.cardId, await this.$store.dispatch('createAttachment', { cardId: this.cardId,
formData: bodyFormData, formData: bodyFormData,
onUploadProgress: (e) => { onUploadProgress: (e) => {
console.log(e) const percentCompleted = Math.round((e.loaded * 100) / e.total)
} }) console.debug(percentCompleted)
this.$set(this.uploadQueue[file.name], 'progress', percentCompleted)
}
})
} catch (err) { } catch (err) {
if (err.response.data.status === 409) { if (err.response.data.status === 409) {
this.overwriteAttachment = err.response.data.data this.overwriteAttachment = err.response.data.data
@@ -52,6 +61,7 @@ export default {
showError(err.response.data.message) showError(err.response.data.message)
} }
} }
this.$delete(this.uploadQueue, file.name)
}, },
overrideAttachment() { overrideAttachment() {

View File

@@ -38,18 +38,10 @@ export class AttachmentApi {
} }
async createAttachment({ cardId, formData, onUploadProgress }) { async createAttachment({ cardId, formData, onUploadProgress }) {
const config = {
onUploadProgress: function(progressEvent) {
const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
console.log(percentCompleted)
},
}
const response = await axios({ const response = await axios({
method: 'POST', method: 'POST',
url: this.url(`/cards/${cardId}/attachment`), url: this.url(`/cards/${cardId}/attachment`),
data: formData, data: formData,
...config,
onUploadProgress, onUploadProgress,
}) })
return response.data return response.data

View File

@@ -83,8 +83,8 @@ export default {
commit('createAttachments', { cardId, attachments }) commit('createAttachments', { cardId, attachments })
}, },
async createAttachment({ commit }, { cardId, formData }) { async createAttachment({ commit }, { cardId, formData, onUploadProgress }) {
const attachment = await apiClient.createAttachment({ cardId, formData }) const attachment = await apiClient.createAttachment({ cardId, formData, onUploadProgress })
commit('createAttachment', { cardId, attachment }) commit('createAttachment', { cardId, attachment })
commit('cardIncreaseAttachmentCount', cardId) commit('cardIncreaseAttachmentCount', cardId)
}, },