From ec51481b9e284669cc09bf9968e31cb3a1348983 Mon Sep 17 00:00:00 2001 From: Louis Delbosc Date: Wed, 27 Jul 2022 12:34:17 +0200 Subject: [PATCH] feat: factorise get export data logic on moreview vue --- .../spreadsheet/components/MoreActions.vue | 124 +++++++----------- 1 file changed, 44 insertions(+), 80 deletions(-) diff --git a/packages/nc-gui/components/project/spreadsheet/components/MoreActions.vue b/packages/nc-gui/components/project/spreadsheet/components/MoreActions.vue index 6e6ec7df43..5e44095ad1 100644 --- a/packages/nc-gui/components/project/spreadsheet/components/MoreActions.vue +++ b/packages/nc-gui/components/project/spreadsheet/components/MoreActions.vue @@ -235,47 +235,7 @@ export default { let c = 1; try { while (!isNaN(offset) && offset > -1) { - let res; - if (this.publicViewId) { - console.log('IF', this.publicViewId) - res = await this.$api.public.csvExport(this.publicViewId, ExportTypes.EXCEL, { - responseType: 'blob', - query: { - fields: - this.queryParams && - this.queryParams.fieldsOrder && - this.queryParams.fieldsOrder.filter(c => this.queryParams.showFields[c]), - offset, - sortArrJson: JSON.stringify( - this.reqPayload && - this.reqPayload.sorts && - this.reqPayload.sorts.map(({ fk_column_id, direction }) => ({ - direction, - fk_column_id, - })) - ), - filterArrJson: JSON.stringify(this.reqPayload && this.reqPayload.filters), - }, - headers: { - 'xc-password': this.reqPayload && this.reqPayload.password, - }, - }); - } else { - console.log('ELSE') - res = await this.$api.dbViewRow.export( - 'noco', - this.projectName, - this.meta.title, - this.selectedView.title, - ExportTypes.EXCEL, - { - responseType: 'base64', - query: { - offset, - }, - } - ); - } + const res = await this.getExportData({ offset, exportType: ExportTypes.EXCEL, responseType: 'base64' }); const workbook = XLSX.read(res.data, { type: 'base64' }); XLSX.writeFile(workbook, `${this.meta.title}_exported_${c++}.xlsx`); @@ -297,45 +257,7 @@ export default { try { while (!isNaN(offset) && offset > -1) { - let res; - if (this.publicViewId) { - res = await this.$api.public.csvExport(this.publicViewId, ExportTypes.CSV, { - responseType: 'blob', - query: { - fields: - this.queryParams && - this.queryParams.fieldsOrder && - this.queryParams.fieldsOrder.filter(c => this.queryParams.showFields[c]), - offset, - sortArrJson: JSON.stringify( - this.reqPayload && - this.reqPayload.sorts && - this.reqPayload.sorts.map(({ fk_column_id, direction }) => ({ - direction, - fk_column_id, - })) - ), - filterArrJson: JSON.stringify(this.reqPayload && this.reqPayload.filters), - }, - headers: { - 'xc-password': this.reqPayload && this.reqPayload.password, - }, - }); - } else { - res = await this.$api.dbViewRow.export( - 'noco', - this.projectName, - this.meta.title, - this.selectedView.title, - ExportTypes.CSV, - { - responseType: 'blob', - query: { - offset, - }, - } - ); - } + const res = await this.getExportData({ offset, exportType: ExportTypes.CSV, responseType: 'blob' }) const { data } = res; offset = +res.headers['nc-export-offset']; @@ -352,6 +274,48 @@ export default { this.$toast.error(e.message).goAway(3000); } }, + async getExportData({ offset, exportType, responseType }) { + let res; + if (this.publicViewId) { + res = await this.$api.public.csvExport(this.publicViewId, exportType, { + responseType, + query: { + fields: + this.queryParams && + this.queryParams.fieldsOrder && + this.queryParams.fieldsOrder.filter(c => this.queryParams.showFields[c]), + offset, + sortArrJson: JSON.stringify( + this.reqPayload && + this.reqPayload.sorts && + this.reqPayload.sorts.map(({ fk_column_id, direction }) => ({ + direction, + fk_column_id, + })) + ), + filterArrJson: JSON.stringify(this.reqPayload && this.reqPayload.filters), + }, + headers: { + 'xc-password': this.reqPayload && this.reqPayload.password, + }, + }); + } else { + res = await this.$api.dbViewRow.export( + 'noco', + this.projectName, + this.meta.title, + this.selectedView.title, + exportType, + { + responseType, + query: { + offset, + }, + } + ); + } + return res; + }, async importData(columnMappings) { try { const data = this.parsedCsv.data;