Skip to content

Commit

Permalink
Cancel import on modal close
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Jan 29, 2020
1 parent 2f42183 commit 0d262fc
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/components/Settings/SettingsImportContacts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@
<span class="import-contact__button-icon icon-folder-white" />
{{ t('contacts', 'Import from files') }}
</button>
<!-- <button id="cancel" class="button import-contact__button import-contact__button--cancel" @click="toggleModal">
<span class="import-contact__button-icon icon-close" />
{{ t('contacts', 'Cancel') }}
</button> -->
</section>
</Modal>
</template>
Expand All @@ -93,6 +89,8 @@ import { generateRemoteUrl } from '@nextcloud/router'
import { getFilePickerBuilder } from '@nextcloud/dialogs'
import axios from 'axios'

const CancelToken = axios.CancelToken

const picker = getFilePickerBuilder(t('contacts', 'Choose a vcard file to import'))
.setMultiSelect(false)
.setModal(true)
Expand All @@ -106,6 +104,7 @@ export default {

data() {
return {
cancelRequest: () => {},
importDestination: false,
isOpened: false,
loading: false,
Expand Down Expand Up @@ -191,6 +190,10 @@ export default {

toggleModal() {
this.isOpened = !this.isOpened
// cancel any ongoing request if closed
if (!this.isOpened) {
this.cancelRequest()
}
},

clickImportInput() {
Expand All @@ -203,8 +206,18 @@ export default {
async openPicker() {
try {
this.loading = true
// unlikely, but let's cancel any previous request
this.cancelRequest()

// prepare cancel token for axios request
const source = CancelToken.source()
this.cancelRequest = source.cancel

// pick and retrieve file
const path = await picker.pick()
const file = await axios.get(generateRemoteUrl(`dav/files/${getCurrentUser().uid}`) + encodePath(path))
const file = await axios.get(generateRemoteUrl(`dav/files/${getCurrentUser().uid}`) + encodePath(path), {
cancelToken: source.token,
})

this.$store.dispatch('changeStage', 'parsing')
this.$store.dispatch('setAddressbook', this.selectedAddressbook.displayName)
Expand All @@ -223,6 +236,7 @@ export default {
* Reset default component state
*/
resetState() {
this.cancelRequest = () => {}
this.importDestination = false
this.isOpened = false
this.loading = false
Expand All @@ -234,9 +248,6 @@ export default {
<style lang="scss" scoped>
.import-contact {
&__modal {
h3 {
padding-bottom: 22px;
}
section {
padding: 22px;
// only one padding bewteen sections
Expand Down

0 comments on commit 0d262fc

Please sign in to comment.