diff --git a/src/dataImport.ts b/src/dataImport.ts index 953fac6f..3504dc03 100644 --- a/src/dataImport.ts +++ b/src/dataImport.ts @@ -175,6 +175,8 @@ export class Importer { requiredMap: Map = {}; labelTemplateFieldMap: LabelTemplateFieldMap = {}; shouldSubmit: boolean = false; + labelIndex: number = -1; + csv: string[][] = []; constructor(doctype: string) { this.doctype = doctype; @@ -272,23 +274,38 @@ export class Importer { } selectFile(text: string): boolean { - const csv = parseCSV(text); - this.parsedLabels = csv[0]; - const values = csv.slice(1); - - /* - if (values.some((v) => v.length !== this.parsedLabels.length)) { - return false; - } - */ - - this.parsedValues = values; - this._setAssigned(); + this.csv = parseCSV(text); + this.initialize(0, true); return true; } - _setAssigned() { + initialize(labelIndex: number, force: boolean) { + if ( + (typeof labelIndex !== 'number' && !labelIndex) || + (labelIndex === this.labelIndex && !force) + ) { + return; + } + console.log('initing', labelIndex); + + this.labelIndex = labelIndex; + this.parsedLabels = this.csv[labelIndex]; + const values = this.csv.slice(labelIndex + 1); + + this.parsedValues = values; + this.setAssigned(); + } + + setAssigned() { const labels = [...this.parsedLabels]; + + for (const k of Object.keys(this.assignedMap)) { + const l = this.assignedMap[k] as string; + if (!labels.includes(l)) { + this.assignedMap[k] = ''; + } + } + labels.forEach((l) => { if (this.assignedMap[l] !== '') { return; @@ -343,6 +360,9 @@ export class Importer { try { await doc.insert(); + if (this.shouldSubmit) { + await doc.submit(); + } } catch (err) { const message = (err as Error).message; @@ -372,6 +392,3 @@ export class Importer { return status; } } - -// @ts-ignore -window.gtf = getTemplateFields; diff --git a/src/pages/DataImport.vue b/src/pages/DataImport.vue index d4b342a1..e7867bd5 100644 --- a/src/pages/DataImport.vue +++ b/src/pages/DataImport.vue @@ -36,35 +36,6 @@ @change="setImportType" /> -
-

Should Submit

- -

+ +

+

{{ t`Importer Settings` }}

+
+ +
+

{{ frappe.t`Submit on Import` }}

+ +
+
+

{{ t`Label Index` }}

+ +
+
+
+

{{ t`Assign Imported Labels` }}

@@ -106,7 +157,7 @@
@@ -223,7 +274,7 @@ import FeatherIcon from '@/components/FeatherIcon.vue'; import PageHeader from '@/components/PageHeader.vue'; import { importable, Importer } from '@/dataImport'; import { IPC_ACTIONS } from '@/messages'; -import { getSavePath, saveData, showToast } from '@/utils'; +import { getSavePath, saveData, showMessageDialog, showToast } from '@/utils'; import { ipcRenderer } from 'electron'; import frappe from 'frappe'; export default { @@ -244,10 +295,16 @@ export default { }; }, computed: { + labelIndex(){ + return this.importer.labelIndex + }, requiredUnassigned() { - return this.importer.assignableLabels - .filter((k) => this.importer.requiredMap[k]) - .some((k) => !this.importer.assignedMap[k]); + return this.importer.assignableLabels.filter( + (k) => this.importer.requiredMap[k] && !this.importer.assignedMap[k] + ); + }, + isRequiredUnassigned() { + return this.requiredUnassigned.length > 0; }, assignedMatrix() { return this.importer.assignedMatrix; @@ -350,6 +407,10 @@ export default { this.saveTemplate(); }, + setLabelIndex(e) { + const labelIndex = (e.target.value ?? 1) - 1; + this.importer.initialize(labelIndex); + }, async saveTemplate() { const template = this.importer.template; const templateName = this.importType + ' ' + this.t`Template`; @@ -382,10 +443,24 @@ export default { this.importer.updateValue(event.target.value, i, j); }, async importData() { - // TODO: pre import conditions - /* - if(){} - */ + if (this.isRequiredUnassigned) { + showMessageDialog({ + message: this.t`Required Fields not Assigned`, + description: this + .t`Please assign the following fields ${this.requiredUnassigned.join( + ', ' + )}`, + }); + return; + } + + if (this.importer.assignedMatrix.length === 0) { + showMessageDialog({ + message: this.t`No Data to Import`, + description: this.t`Please select a file with data to import.`, + }); + return; + } const { success, names } = await this.importer.importData(); if (!success || !names.length) {