This commit is contained in:
2022-03-24 18:42:41 +00:00
parent c802633e5b
commit 0e9fc1539b

View File

@@ -21,12 +21,12 @@
show-size
></v-file-input
><v-btn
v-if="uploadFile.name && ayaType != 0"
v-if="uploadFile && uploadFile.name && ayaType != 0"
:loading="uploading"
color="primary"
text
@click="upload"
>{{ $ay.t("Upload") }}</v-btn
@click="process"
>{{ $ay.t("Import") }}</v-btn
>
</v-col>
@@ -43,7 +43,7 @@
</div>
</template>
<script>
import papa from "papaparse";
import Papa from "papaparse";
const FORM_KEY = "adm-import";
export default {
data() {
@@ -70,60 +70,83 @@ export default {
window.$gz.eventBus.$off("menu-click", clickHandler);
},
methods: {
async upload() {
console.log("UPLOAD: uploadFile is:", this.uploadFile);
if (this.uploadFile && this.uploadFile.name) {
console.log("Processing uploadfile");
papa.parse(this.uploadFile, {
header: true,
dynamicTyping: true,
worker: true,
step: function(row) {
console.log("Row:", row.data);
},
complete: function() {
console.log("All done!");
}
});
} else {
console.log("NO upload file");
async process() {
if (!this.uploadFile) {
return;
}
try {
console.log("Upload file is:", this.uploadFile);
let fileType = this.uploadFile.type.toLowerCase();
if (!fileType.includes("csv") && !fileType.includes("json")) {
throw new Error("Not supported file type, must be .csv or .json");
}
const isCSV = fileType.includes("csv");
let dat = null;
if (isCSV) {
dat = await parseCSVFile(this.uploadFile);
console.log("Done parseCSVFile");
} else {
dat = await parseJSONFile(this.uploadFile);
console.log("Done parse json");
}
//similar code in wiki-control
// const vm = this;
// const fileData = [];
// for (let i = 0; i < vm.uploadFile.length; i++) {
// const f = vm.uploadFile[i];
// fileData.push({ name: f.name, lastModified: f.lastModified });
// }
// const at = {
// ayaType: vm.ayaType,
// files: vm.uploadFile,
// fileData: JSON.stringify(fileData)
// };
// try {
// vm.uploading = true;
// const res = await window.$gz.api.upload("import/upload", at);
// if (res.error) {
// window.$gz.errorHandler.handleFormError(res.error);
// } else {
// vm.uploadFile = [];
// //result is an array of strings
// let outText = "";
// res.data.forEach(function appendImportResultItem(value) {
// outText += value + "\n";
// });
// vm.importResult = await window.$gz.translation.translateStringWithMultipleKeysAsync(
// outText
// );
// }
// } catch (error) {
// window.$gz.errorHandler.handleFormError(error);
// } finally {
// vm.uploading = false;
// }
console.log("The final data is ", dat);
} catch (error) {
window.$gz.errorHandler.handleFormError(error);
} finally {
this.uploading = false;
}
},
async upload() {
if (!this.uploadFile) {
return;
}
try {
// console.log("Upload file is:", this.uploadFile);
// let fileType = this.uploadFile.type.toLowerCase();
// if (!fileType.includes("csv") && !fileType.includes("json")) {
// throw new Error("Not supported file type, must be .csv or .json");
// }
// const isCSV = fileType.includes("csv");
// let dat = null;
// if (isCSV) {
// dat = await parse(this.uploadFile);
// } else {
// dat = JSON.parse(this.uploadFile);
// }
// console.log("The final data is ", dat);
//similar code in wiki-control
// const vm = this;
// const fileData = [];
// for (let i = 0; i < vm.uploadFile.length; i++) {
// const f = vm.uploadFile[i];
// fileData.push({ name: f.name, lastModified: f.lastModified });
// }
// const at = {
// ayaType: vm.ayaType,
// files: vm.uploadFile,
// fileData: JSON.stringify(fileData)
// };
// vm.uploading = true;
// const res = await window.$gz.api.upload("import/upload", at);
// if (res.error) {
// window.$gz.errorHandler.handleFormError(res.error);
// } else {
// vm.uploadFile = [];
// //result is an array of strings
// let outText = "";
// res.data.forEach(function appendImportResultItem(value) {
// outText += value + "\n";
// });
// vm.importResult = await window.$gz.translation.translateStringWithMultipleKeysAsync(
// outText
// );
// }
} catch (error) {
window.$gz.errorHandler.handleFormError(error);
} finally {
this.uploading = false;
}
},
handleSelected() {}
}
@@ -180,4 +203,50 @@ async function populateSelectionLists(vm) {
"importable"
);
}
//////////////////////////////////////////////////////////
//
// Parse csv and return results as JSON, handle errors if any
//
async function parseCSVFile(file) {
Papa.parsePromise = function(file) {
return new Promise(function(complete, error) {
Papa.parse(file, {
header: true,
dynamicTyping: true,
worker: true,
complete,
error
});
});
};
let results;
await Papa.parsePromise(file).then(function(parsedData) {
results = parsedData;
});
return results;
}
//////////////////////////////////////////////////////////
//
// Open local json file, read, parse and return results as JSON, handle errors if any
//
async function parseJSONFile(file) {
return new Promise(function(complete) {
const reader = new FileReader();
reader.addEventListener(
"load",
() => {
// this will then display a text file
complete(JSON.parse(reader.result));
},
false
);
if (file) {
reader.readAsText(file);
}
});
}
</script>