From 6fe35f887da9a29742fd8735db96776f711462b6 Mon Sep 17 00:00:00 2001 From: Neeraj Sharma Date: Tue, 3 Dec 2019 18:58:30 +0530 Subject: [PATCH] implement bilk import all functionality --- src/Services/http.js | 2 +- src/pages/Administrator/bulkImport.vue | 300 +++++++++++++++++++++++++++------ src/script/books.js | 24 +-- src/script/parents.js | 7 +- src/script/teachers.js | 26 ++- 5 files changed, 286 insertions(+), 73 deletions(-) diff --git a/src/Services/http.js b/src/Services/http.js index bcd38b0..2ed0575 100644 --- a/src/Services/http.js +++ b/src/Services/http.js @@ -3,7 +3,7 @@ import store from '@/store/store' export default () => { return axios.create({ - // baseURL: 'http://192.168.2.221:3002/v1', + // baseURL: 'http://192.168.0.108:3002/v1', baseURL: 'http://13.234.251.173:8001/v1', headers: { Authorization: `Bearer ${store.state.token}` diff --git a/src/pages/Administrator/bulkImport.vue b/src/pages/Administrator/bulkImport.vue index f22c992..f51eb38 100644 --- a/src/pages/Administrator/bulkImport.vue +++ b/src/pages/Administrator/bulkImport.vue @@ -19,7 +19,8 @@ type="file" style="display: none" ref="file" - @change="picFileTeacher($event)" + @change="convertFile" + id="fileInput" /> @@ -28,7 +29,7 @@ - import + import @@ -48,13 +49,20 @@ + - + refreshUpload - + import @@ -74,15 +82,22 @@ - + + - + refreshUpload - import + import @@ -101,15 +116,22 @@ - + + - + refreshUpload - import + import @@ -195,58 +217,232 @@ export default { parentData: [], userData: [], bookData: [], - teacher: "TEACHER", + teacherFileJson: [], + parentFileJson: [], + userFileJson: [], + bookFileJson: [], teacherFileName: "", - teacherFileUrl: "", - parent: "PARENT", parentFileName: "", - parentFileUrl: "" + userFileName: "", + bookFileName: "", + token: "", + loading: false }), methods: { pickFile() { this.$refs.file.click(); }, - picFileTeacher(e) { - console.log(this.parent); - if (this.teacher === "TEACHER") { - var vm = this; - if (window.FileReader) { - var reader = new FileReader(); - this.teacherFileName = e.target.files[0].name; - this.teacherFileUrl = e.target.files[0]; - console.log("this.teacherFileUrl", this.teacherFileUrl); + pickParentFile() { + this.$refs.parentfile.click(); + }, + pickUserFile() { + this.$refs.userfile.click(); + }, + pickBookFile() { + this.$refs.bookfile.click(); + }, + convertFile() { + const input = document.getElementById("fileInput"); + const reader = new FileReader(); + reader.onload = () => { + let text = reader.result; + //convert text to json here + var json = this.csvJSON(text); + // console.log("CSV: ", json); + }; + reader.readAsText(input.files[0]); + // console.log("input.files[0]", input.files[0].name); + this.teacherFileName = input.files[0].name; + }, + parentFile() { + const input = document.getElementById("parentInput"); + const reader = new FileReader(); + reader.onload = () => { + let text = reader.result; + //convert text to json here + var json = this.csvParentJSON(text); + // console.log("CSV: ", json); + }; + reader.readAsText(input.files[0]); + // console.log("input.files[0]", input.files[0].name); + this.parentFileName = input.files[0].name; + }, + userFile() { + const input = document.getElementById("userInput"); + const reader = new FileReader(); + reader.onload = () => { + let text = reader.result; + //convert text to json here + var json = this.csvUserJSON(text); + // console.log("CSV: ", json); + }; + reader.readAsText(input.files[0]); + // console.log("input.files[0]", input.files[0].name); + this.userFileName = input.files[0].name; + }, + bookFile() { + const input = document.getElementById("bookInput"); + const reader = new FileReader(); + reader.onload = () => { + let text = reader.result; + //convert text to json here + var json = this.csvBookJSON(text); + // console.log("CSV: ", json); + }; + reader.readAsText(input.files[0]); + // console.log("input.files[0]", input.files[0].name); + this.bookFileName = input.files[0].name; + }, + csvJSON(csv) { + var lines = csv.split("\n"); + + var result = []; + var headers = lines[0].split(","); + + for (var i = 1; i < lines.length; i++) { + var obj = {}; + var currentline = lines[i].split(","); + + for (var j = 0; j < headers.length; j++) { + obj[headers[j]] = currentline[j]; } + console.log("obj", obj); + this.teacherFileJson.push(obj); } - // if (this.parent === "PARENT") { - // var vm = this; - // if (window.FileReader) { - // var reader = new FileReader(); - // this.parentFileName = e.target.files[0].name; - // this.parentFileUrl = e.target.files[0]; - // console.log("this.teacherFileUrl", this.parentFileUrl); - // } - // } - // if (this.teacher === "TEACHER") { - // const files = e.target.files; - // if (files[0] !== undefined) { - // this.teacherFileName = files[0].name; - // if (this.teacherFileName.lastIndexOf(".") <= 0) { - // return; - // } - // const fr = new FileReader(); - // fr.readAsDataURL(files[0]); - // fr.addEventListener("load", () => { - // this.teacherFileUrl = fr.result; - // // this.imageFile = files[0]; // this is an image file that can be sent to server... - // }); - // } else { - // this.teacherFileName = ""; - // this.teacherFileUrl = ""; - // } - // } + }, + csvParentJSON(csvParent) { + var lines = csvParent.split("\n"); + + var result = []; + + var headers = lines[0].split(","); + + for (var i = 1; i < lines.length; i++) { + var obj = {}; + var currentline = lines[i].split(","); + + for (var j = 0; j < headers.length; j++) { + obj[headers[j]] = currentline[j]; + } + + this.parentFileJson.push(obj); + } + }, + csvUserJSON(cvsUser) { + var lines = cvsUser.split("\n"); + + var result = []; + + var headers = lines[0].split(","); + + for (var i = 1; i < lines.length; i++) { + var obj = {}; + var currentline = lines[i].split(","); + + for (var j = 0; j < headers.length; j++) { + obj[headers[j]] = currentline[j]; + } + + this.userFileJson.push(obj); + } + }, + csvBookJSON(cvsBook) { + var lines = cvsBook.split("\n"); + + var result = []; + + var headers = lines[0].split(","); + + for (var i = 1; i < lines.length; i++) { + var obj = {}; + var currentline = lines[i].split(","); + + for (var j = 0; j < headers.length; j++) { + obj[headers[j]] = currentline[j]; + } + + this.bookFileJson.push(obj); + } + }, + importTeacher() { + this.loading = true; + var teacherfile = {}; + teacherfile.teacherData = this.teacherFileJson; + http() + .post("/importTeacherData", teacherfile, { + headers: { Authorization: "Bearer " + this.token } + }) + .then(response => { + this.snackbar = true; + this.text = response.data.message; + this.loading = false; + }) + .catch(error => { + this.snackbar = true; + this.text = error.response.data.message; + this.loading = false; + }); + }, + importParent() { + this.loading = true; + var parentfile = {}; + parentfile.parentData = this.parentFileJson; + http() + .post("/importParentData", parentfile, { + headers: { Authorization: "Bearer " + this.token } + }) + .then(response => { + this.snackbar = true; + this.text = response.data.message; + this.loading = false; + }) + .catch(error => { + this.snackbar = true; + this.text = error.response.data.message; + this.loading = false; + }); + }, + importUser() { + this.loading = true; + var userfile = {}; + userfile.userData = this.userFileJson; + http() + .post("/importUserData", userfile, { + headers: { Authorization: "Bearer " + this.token } + }) + .then(response => { + this.snackbar = true; + this.text = response.data.message; + this.loading = false; + }) + .catch(error => { + this.snackbar = true; + this.text = error.response.data.message; + this.loading = false; + }); + }, + importBook() { + this.loading = true; + var bookfile = {}; + bookfile.bookData = this.bookFileJson; + http() + .post("/importBookData", bookfile, { + headers: { Authorization: "Bearer " + this.token } + }) + .then(response => { + this.snackbar = true; + this.text = response.data.message; + this.loading = false; + }) + .catch(error => { + this.snackbar = true; + this.text = error.response.data.message; + this.loading = false; + }); } }, mounted() { + this.token = this.$store.state.token; /** TEACHERS SAMPLE CSV */ const getTeacherData = teacherData(); this.teacherData = getTeacherData; @@ -268,4 +464,6 @@ export default { this.bookData = getBookData; } }; - \ No newline at end of file + + + diff --git a/src/script/books.js b/src/script/books.js index 03d23ec..9c2a85d 100644 --- a/src/script/books.js +++ b/src/script/books.js @@ -1,20 +1,20 @@ export default () => { const books = [{ - "Book": "Math", - "Subject": "002015", - "Author": "R D Sharma", - "Price": "600", - "Quantity": "15", - "Rack": "110", + "name": "Neeraj", + "subjectCode": "002015", + "author": "R D Sharma", + "price": "600", + "quantity": "15", + "rackNo": "110" }, { - "Book": "Hindi", - "Subject": "059090", - "Author": "V Kumar", - "Price": "400", - "Quantity": "5", - "Rack": "10", + "name": "Rishav", + "subjectCode": "004018", + "author": "D k Kumar", + "price": "400", + "quantity": "5", + "rackNo": "10" }, ] return books; diff --git a/src/script/parents.js b/src/script/parents.js index eb3fca2..95f5d2f 100644 --- a/src/script/parents.js +++ b/src/script/parents.js @@ -3,16 +3,17 @@ export default () => { const parents = [{ "email": "digital@theideazfactory.com", "fatherName": "Abhishek", - "fatherCellNo": "9898981234", "motherName": "Rita", "motherCellNo": "8989786789", + "fatherCellNo": "9898981234" + }, { "email": "Rampal@viithiisys.com", "fatherName": "suresh", - "fatherCellNo": "9898987867", "motherName": "rita kumari", - "motherCellNo": "9768576536", + "fatherCellNo": "9898987867", + "motherCellNo": "9768576536" } ] return parents; diff --git a/src/script/teachers.js b/src/script/teachers.js index 2b802b4..25ec41e 100644 --- a/src/script/teachers.js +++ b/src/script/teachers.js @@ -6,21 +6,35 @@ export default () => { "dob": "1994-05-01", "city": "Chandigarh", "country": "India", - "presentAddress": "SCO 34, second floor", + "presentAddress": "SCO 34 second floor", "mobileNo": "08558875847", "state": "Chandigarh", - "joinDate": "2019-11-18", + "pincode": "160071", + "joinDate": "2019-11-18" }, { - "name": "Neeraj Sharma", - "email": "neeraj.sharma@viithiisys.com", + "name": "Kuldeep Kumar", + "email": "kuldeep.kumar@viithiisys.com", "dob": "1995-01-14", "city": "Mohali", "country": "India", - "presentAddress": "sector 71 ,Mohali", + "presentAddress": "sector 71 Mohali", "mobileNo": "7009191451", "state": "Punjab", - "joinDate": "2018-04-23", + "pincode": "160072", + "joinDate": "2018-04-23" + }, + { + "name": "Sunny Negi", + "email": "sunny.negi@viithiisys.com", + "dob": "1995-01-14", + "city": "Phanckula", + "country": "India", + "presentAddress": "sector 71 Mohali", + "mobileNo": "8989981451", + "state": "haryana", + "pincode": "160073", + "joinDate": "2018-04-24" } ] return teachers; -- 2.0.0