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 show-size
></v-file-input ></v-file-input
><v-btn ><v-btn
v-if="uploadFile.name && ayaType != 0" v-if="uploadFile && uploadFile.name && ayaType != 0"
:loading="uploading" :loading="uploading"
color="primary" color="primary"
text text
@click="upload" @click="process"
>{{ $ay.t("Upload") }}</v-btn >{{ $ay.t("Import") }}</v-btn
> >
</v-col> </v-col>
@@ -43,7 +43,7 @@
</div> </div>
</template> </template>
<script> <script>
import papa from "papaparse"; import Papa from "papaparse";
const FORM_KEY = "adm-import"; const FORM_KEY = "adm-import";
export default { export default {
data() { data() {
@@ -70,60 +70,83 @@ export default {
window.$gz.eventBus.$off("menu-click", clickHandler); window.$gz.eventBus.$off("menu-click", clickHandler);
}, },
methods: { methods: {
async upload() { async process() {
console.log("UPLOAD: uploadFile is:", this.uploadFile); if (!this.uploadFile) {
if (this.uploadFile && this.uploadFile.name) { return;
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");
} }
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 console.log("The final data is ", dat);
// const vm = this; } catch (error) {
// const fileData = []; window.$gz.errorHandler.handleFormError(error);
// for (let i = 0; i < vm.uploadFile.length; i++) { } finally {
// const f = vm.uploadFile[i]; this.uploading = false;
// fileData.push({ name: f.name, lastModified: f.lastModified }); }
// } },
async upload() {
// const at = { if (!this.uploadFile) {
// ayaType: vm.ayaType, return;
// files: vm.uploadFile, }
// fileData: JSON.stringify(fileData) try {
// }; // console.log("Upload file is:", this.uploadFile);
// let fileType = this.uploadFile.type.toLowerCase();
// try { // if (!fileType.includes("csv") && !fileType.includes("json")) {
// vm.uploading = true; // throw new Error("Not supported file type, must be .csv or .json");
// const res = await window.$gz.api.upload("import/upload", at); // }
// if (res.error) { // const isCSV = fileType.includes("csv");
// window.$gz.errorHandler.handleFormError(res.error); // let dat = null;
// } else { // if (isCSV) {
// vm.uploadFile = []; // dat = await parse(this.uploadFile);
// //result is an array of strings // } else {
// let outText = ""; // dat = JSON.parse(this.uploadFile);
// res.data.forEach(function appendImportResultItem(value) { // }
// outText += value + "\n"; // console.log("The final data is ", dat);
// }); //similar code in wiki-control
// vm.importResult = await window.$gz.translation.translateStringWithMultipleKeysAsync( // const vm = this;
// outText // const fileData = [];
// ); // for (let i = 0; i < vm.uploadFile.length; i++) {
// } // const f = vm.uploadFile[i];
// } catch (error) { // fileData.push({ name: f.name, lastModified: f.lastModified });
// window.$gz.errorHandler.handleFormError(error); // }
// } finally { // const at = {
// vm.uploading = false; // 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() {} handleSelected() {}
} }
@@ -180,4 +203,50 @@ async function populateSelectionLists(vm) {
"importable" "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> </script>