refactor: uploading counters vuex state

This commit is contained in:
Ramires Viana 2020-07-07 19:41:13 +00:00
parent d0b359561f
commit b4f131be50
5 changed files with 98 additions and 65 deletions

View File

@ -101,17 +101,11 @@ export default {
components: { Item }, components: { Item },
data: function () { data: function () {
return { return {
showLimit: 50, showLimit: 50
uploading: {
id: 0,
count: 0,
size: 0,
progress: []
}
} }
}, },
computed: { computed: {
...mapState(['req', 'selected', 'user', 'show']), ...mapState(['req', 'selected', 'user', 'show', 'uploading']),
nameSorted () { nameSorted () {
return (this.req.sorting.by === 'name') return (this.req.sorting.by === 'name')
}, },
@ -311,7 +305,7 @@ export default {
dragEnd () { dragEnd () {
this.resetOpacity() this.resetOpacity()
}, },
drop: function (event) { drop: async function (event) {
event.preventDefault() event.preventDefault()
this.resetOpacity() this.resetOpacity()
@ -331,21 +325,36 @@ export default {
base = el.querySelector('.name').innerHTML + '/' base = el.querySelector('.name').innerHTML + '/'
} }
if (base === '') { let files = await this.scanFiles(dt);
this.scanFiles(dt).then((result) => { let path = this.$route.path + base;
this.checkConflict(result, this.req.items, base) let items = this.req.items
})
} else { if (base !== '') {
this.scanFiles(dt).then((result) => { try {
api.fetch(this.$route.path + base) items = (await api.fetch(path)).items
.then(req => { } catch (error) {
this.checkConflict(result, req.items, base) this.$showError(error)
})
.catch(this.$showError)
})
} }
}
let conflict = this.checkConflict(files, items)
if (conflict) {
this.$store.commit('showHover', {
prompt: 'replace',
confirm: (event) => {
event.preventDefault()
this.$store.commit('closeHovers')
this.handleFiles(files, path, true)
}
})
return
}
this.handleFiles(files, path)
}, },
checkConflict (files, items, base) { checkConflict (files, items) {
if (typeof items === 'undefined' || items === null) { if (typeof items === 'undefined' || items === null) {
items = [] items = []
} }
@ -377,19 +386,7 @@ export default {
} }
} }
if (!conflict) { return conflict
this.handleFiles(files, base)
return
}
this.$store.commit('showHover', {
prompt: 'replace',
confirm: (event) => {
event.preventDefault()
this.$store.commit('closeHovers')
this.handleFiles(files, base, true)
}
})
}, },
uploadInput (event) { uploadInput (event) {
this.$store.commit('closeHovers') this.$store.commit('closeHovers')
@ -404,7 +401,22 @@ export default {
} }
} }
this.checkConflict(files, this.req.items, '') let path = this.$route.path;
let conflict = this.checkConflict(files, this.req.items)
if (conflict) {
this.$store.commit('showHover', {
prompt: 'replace',
confirm: (event) => {
event.preventDefault()
this.$store.commit('closeHovers')
this.handleFiles(files, path, true)
}
})
return
}
this.handleFiles(files, path)
}, },
resetOpacity () { resetOpacity () {
let items = document.getElementsByClassName('item') let items = document.getElementsByClassName('item')
@ -474,15 +486,7 @@ export default {
} }
}) })
}, },
setProgress: throttle(function() { handleFiles (files, path, overwrite = false) {
if (this.uploading.count == 0) {
return
}
let sum = this.uploading.progress.reduce((acc, val) => acc + val)
this.$store.commit('setProgress', Math.ceil(sum / this.uploading.size * 100))
}, 100, {leading: false, trailing: true}),
handleFiles (files, base, overwrite = false) {
if (this.uploading.count == 0) { if (this.uploading.count == 0) {
buttons.loading('upload') buttons.loading('upload')
} }
@ -490,8 +494,7 @@ export default {
let promises = [] let promises = []
let onupload = (id) => (event) => { let onupload = (id) => (event) => {
this.uploading.progress[id] = event.loaded this.$store.commit('uploadigSetProgress', { id, loaded: event.loaded })
this.setProgress()
} }
for (let i = 0; i < files.length; i++) { for (let i = 0; i < files.length; i++) {
@ -503,17 +506,17 @@ export default {
let id = this.uploading.id let id = this.uploading.id
this.uploading.size += file.size this.$store.commit('uploadingIncrementSize', file.size)
this.uploading.id++ this.$store.commit('uploadingIncrementId')
this.uploading.count++ this.$store.commit('uploadingIncrementCount')
let promise = api.post(this.$route.path + base + filenameEncoded, file, overwrite, throttle(onupload(id), 100)).finally(() => { let promise = api.post(path + filenameEncoded, file, overwrite, throttle(onupload(id), 100)).finally(() => {
this.uploading.count-- this.$store.commit('uploadingDecreaseCount')
}) })
promises.push(promise) promises.push(promise)
} else { } else {
let uri = this.$route.path + base let uri = path
let folders = file.path.split("/") let folders = file.path.split("/")
for (let i = 0; i < folders.length; i++) { for (let i = 0; i < folders.length; i++) {
@ -533,12 +536,8 @@ export default {
buttons.success('upload') buttons.success('upload')
this.$store.commit('setProgress', 0)
this.$store.commit('setReload', true) this.$store.commit('setReload', true)
this.$store.commit('uploadingReset')
this.uploading.id = 0
this.uploading.sizes = []
this.uploading.progress = []
} }
Promise.all(promises) Promise.all(promises)

View File

@ -3,7 +3,15 @@ const getters = {
isFiles: state => !state.loading && state.route.name === 'Files', isFiles: state => !state.loading && state.route.name === 'Files',
isListing: (state, getters) => getters.isFiles && state.req.isDir, isListing: (state, getters) => getters.isFiles && state.req.isDir,
isEditor: (state, getters) => getters.isFiles && (state.req.type === 'text' || state.req.type === 'textImmutable'), isEditor: (state, getters) => getters.isFiles && (state.req.type === 'text' || state.req.type === 'textImmutable'),
selectedCount: state => state.selected.length selectedCount: state => state.selected.length,
progress : state => {
if (state.uploading.progress.length == 0) {
return 0;
}
let sum = state.uploading.progress.reduce((acc, val) => acc + val)
return Math.ceil(sum / state.uploading.size * 100);
}
} }
export default getters export default getters

View File

@ -22,7 +22,13 @@ const state = {
show: null, show: null,
showShell: false, showShell: false,
showMessage: null, showMessage: null,
showConfirm: null showConfirm: null,
uploading: {
id: 0,
count: 0,
size: 0,
progress: []
}
} }
export default new Vuex.Store({ export default new Vuex.Store({

View File

@ -1,3 +1,4 @@
import Vue from 'vue'
import * as i18n from '@/i18n' import * as i18n from '@/i18n'
import moment from 'moment' import moment from 'moment'
@ -83,8 +84,27 @@ const mutations = {
state.clipboard.key = '' state.clipboard.key = ''
state.clipboard.items = [] state.clipboard.items = []
}, },
setProgress: (state, value) => {
state.progress = value uploadingIncrementId: (state) => {
state.uploading.id = state.uploading.id + 1
},
uploadingIncrementSize: (state, value) => {
state.uploading.size = state.uploading.size + value
},
uploadingIncrementCount: (state) => {
state.uploading.count = state.uploading.count + 1
},
uploadingDecreaseCount: (state) => {
state.uploading.count = state.uploading.count - 1
},
uploadigSetProgress(state, { id, loaded }) {
Vue.set(state.uploading.progress, id, loaded)
},
uploadingReset: (state) => {
state.uploading.id = 0
state.uploading.size = 0
state.uploading.count = 0
state.uploading.progress = []
} }
} }

View File

@ -1,7 +1,7 @@
<template> <template>
<div> <div>
<div id="progress"> <div id="progress">
<div v-bind:style="{ width: $store.state.progress + '%' }"></div> <div v-bind:style="{ width: this.progress + '%' }"></div>
</div> </div>
<site-header></site-header> <site-header></site-header>
<sidebar></sidebar> <sidebar></sidebar>
@ -29,7 +29,7 @@ export default {
Shell Shell
}, },
computed: { computed: {
...mapGetters([ 'isLogged' ]), ...mapGetters([ 'isLogged', 'progress' ]),
...mapState([ 'user' ]) ...mapState([ 'user' ])
}, },
watch: { watch: {