Commit 6fe35f887da9a29742fd8735db96776f711462b6

Authored by Neeraj Sharma
1 parent c9880acade

implement bilk import all functionality

src/Services/http.js
... ... @@ -3,7 +3,7 @@ import store from '@/store/store'
3 3  
4 4 export default () => {
5 5 return axios.create({
6   - // baseURL: 'http://192.168.2.221:3002/v1',
  6 + // baseURL: 'http://192.168.0.108:3002/v1',
7 7 baseURL: 'http://13.234.251.173:8001/v1',
8 8 headers: {
9 9 Authorization: `Bearer ${store.state.token}`
... ...
src/pages/Administrator/bulkImport.vue
... ... @@ -19,7 +19,8 @@
19 19 type="file"
20 20 style="display: none"
21 21 ref="file"
22   - @change="picFileTeacher($event)"
  22 + @change="convertFile"
  23 + id="fileInput"
23 24 />
24 25 </v-flex>
25 26 <v-flex xs6 sm4 lg2 class="mt-1">
... ... @@ -28,7 +29,7 @@
28 29 </v-btn>
29 30 </v-flex>
30 31 <v-flex xs6 sm4 lg2 class="mt-1">
31   - <v-btn class="grey" dark block>import</v-btn>
  32 + <v-btn class="grey" dark block @click="importTeacher()" :loading="loading">import</v-btn>
32 33 </v-flex>
33 34 <v-flex xs12 sm4 lg2 class="mt-1">
34 35 <download-csv :data="teacherData">
... ... @@ -48,13 +49,20 @@
48 49 </v-flex>
49 50 <v-flex xs12 sm6 lg3>
50 51 <v-text-field v-model="parentFileName" placeholder="Choose file" :disabled="true"></v-text-field>
  52 + <input
  53 + type="file"
  54 + style="display: none"
  55 + ref="parentfile"
  56 + @change="parentFile"
  57 + id="parentInput"
  58 + />
51 59 </v-flex>
52 60 <v-flex xs6 sm4 lg2 class="mt-1">
53   - <v-btn class="grey" dark block @click="pickFile">
  61 + <v-btn class="grey" dark block @click="pickParentFile">
54 62 <v-icon small class="pr-1">refresh</v-icon>Upload
55 63 </v-btn>
56 64 </v-flex>
57   - <v-flex xs6 sm4 lg2 class="mt-1">
  65 + <v-flex xs6 sm4 lg2 class="mt-1" @click="importParent">
58 66 <v-btn class="grey" dark block>import</v-btn>
59 67 </v-flex>
60 68 <v-flex xs12 sm4 lg2 class="mt-1">
... ... @@ -74,15 +82,22 @@
74 82 <label class="title right">Add User:</label>
75 83 </v-flex>
76 84 <v-flex xs12 sm6 lg3>
77   - <v-text-field placeholder="Choose file" :disabled="true"></v-text-field>
  85 + <v-text-field placeholder="Choose file" v-model="userFileName" :disabled="true"></v-text-field>
  86 + <input
  87 + type="file"
  88 + style="display: none"
  89 + ref="userfile"
  90 + @change="userFile"
  91 + id="userInput"
  92 + />
78 93 </v-flex>
79 94 <v-flex xs6 sm4 lg2 class="mt-1">
80   - <v-btn class="grey" dark block>
  95 + <v-btn class="grey" dark block @click="pickUserFile">
81 96 <v-icon small class="pr-1">refresh</v-icon>Upload
82 97 </v-btn>
83 98 </v-flex>
84 99 <v-flex xs6 sm4 lg2 class="mt-1">
85   - <v-btn class="grey" dark block>import</v-btn>
  100 + <v-btn class="grey" dark block @click="importUser">import</v-btn>
86 101 </v-flex>
87 102 <v-flex xs12 sm4 lg2 class="mt-1">
88 103 <download-csv :data="userData">
... ... @@ -101,15 +116,22 @@
101 116 <label class="title right">Add Book:</label>
102 117 </v-flex>
103 118 <v-flex xs12 sm6 lg3>
104   - <v-text-field placeholder="Choose file" :disabled="true"></v-text-field>
  119 + <v-text-field placeholder="Choose file" v-model="bookFileName" :disabled="true"></v-text-field>
  120 + <input
  121 + type="file"
  122 + style="display: none"
  123 + ref="bookfile"
  124 + @change="bookFile"
  125 + id="bookInput"
  126 + />
105 127 </v-flex>
106 128 <v-flex xs6 sm4 lg2 class="mt-1">
107   - <v-btn class="grey" dark block>
  129 + <v-btn class="grey" dark block @click="pickBookFile">
108 130 <v-icon small class="pr-1">refresh</v-icon>Upload
109 131 </v-btn>
110 132 </v-flex>
111 133 <v-flex xs6 sm4 lg2 class="mt-1">
112   - <v-btn class="grey" dark block>import</v-btn>
  134 + <v-btn class="grey" dark block @click="importBook">import</v-btn>
113 135 </v-flex>
114 136 <v-flex xs12 sm4 lg2 class="mt-1">
115 137 <download-csv :data="bookData">
... ... @@ -195,58 +217,232 @@ export default {
195 217 parentData: [],
196 218 userData: [],
197 219 bookData: [],
198   - teacher: "TEACHER",
  220 + teacherFileJson: [],
  221 + parentFileJson: [],
  222 + userFileJson: [],
  223 + bookFileJson: [],
199 224 teacherFileName: "",
200   - teacherFileUrl: "",
201   - parent: "PARENT",
202 225 parentFileName: "",
203   - parentFileUrl: ""
  226 + userFileName: "",
  227 + bookFileName: "",
  228 + token: "",
  229 + loading: false
204 230 }),
205 231 methods: {
206 232 pickFile() {
207 233 this.$refs.file.click();
208 234 },
209   - picFileTeacher(e) {
210   - console.log(this.parent);
211   - if (this.teacher === "TEACHER") {
212   - var vm = this;
213   - if (window.FileReader) {
214   - var reader = new FileReader();
215   - this.teacherFileName = e.target.files[0].name;
216   - this.teacherFileUrl = e.target.files[0];
217   - console.log("this.teacherFileUrl", this.teacherFileUrl);
  235 + pickParentFile() {
  236 + this.$refs.parentfile.click();
  237 + },
  238 + pickUserFile() {
  239 + this.$refs.userfile.click();
  240 + },
  241 + pickBookFile() {
  242 + this.$refs.bookfile.click();
  243 + },
  244 + convertFile() {
  245 + const input = document.getElementById("fileInput");
  246 + const reader = new FileReader();
  247 + reader.onload = () => {
  248 + let text = reader.result;
  249 + //convert text to json here
  250 + var json = this.csvJSON(text);
  251 + // console.log("CSV: ", json);
  252 + };
  253 + reader.readAsText(input.files[0]);
  254 + // console.log("input.files[0]", input.files[0].name);
  255 + this.teacherFileName = input.files[0].name;
  256 + },
  257 + parentFile() {
  258 + const input = document.getElementById("parentInput");
  259 + const reader = new FileReader();
  260 + reader.onload = () => {
  261 + let text = reader.result;
  262 + //convert text to json here
  263 + var json = this.csvParentJSON(text);
  264 + // console.log("CSV: ", json);
  265 + };
  266 + reader.readAsText(input.files[0]);
  267 + // console.log("input.files[0]", input.files[0].name);
  268 + this.parentFileName = input.files[0].name;
  269 + },
  270 + userFile() {
  271 + const input = document.getElementById("userInput");
  272 + const reader = new FileReader();
  273 + reader.onload = () => {
  274 + let text = reader.result;
  275 + //convert text to json here
  276 + var json = this.csvUserJSON(text);
  277 + // console.log("CSV: ", json);
  278 + };
  279 + reader.readAsText(input.files[0]);
  280 + // console.log("input.files[0]", input.files[0].name);
  281 + this.userFileName = input.files[0].name;
  282 + },
  283 + bookFile() {
  284 + const input = document.getElementById("bookInput");
  285 + const reader = new FileReader();
  286 + reader.onload = () => {
  287 + let text = reader.result;
  288 + //convert text to json here
  289 + var json = this.csvBookJSON(text);
  290 + // console.log("CSV: ", json);
  291 + };
  292 + reader.readAsText(input.files[0]);
  293 + // console.log("input.files[0]", input.files[0].name);
  294 + this.bookFileName = input.files[0].name;
  295 + },
  296 + csvJSON(csv) {
  297 + var lines = csv.split("\n");
  298 +
  299 + var result = [];
  300 + var headers = lines[0].split(",");
  301 +
  302 + for (var i = 1; i < lines.length; i++) {
  303 + var obj = {};
  304 + var currentline = lines[i].split(",");
  305 +
  306 + for (var j = 0; j < headers.length; j++) {
  307 + obj[headers[j]] = currentline[j];
218 308 }
  309 + console.log("obj", obj);
  310 + this.teacherFileJson.push(obj);
219 311 }
220   - // if (this.parent === "PARENT") {
221   - // var vm = this;
222   - // if (window.FileReader) {
223   - // var reader = new FileReader();
224   - // this.parentFileName = e.target.files[0].name;
225   - // this.parentFileUrl = e.target.files[0];
226   - // console.log("this.teacherFileUrl", this.parentFileUrl);
227   - // }
228   - // }
229   - // if (this.teacher === "TEACHER") {
230   - // const files = e.target.files;
231   - // if (files[0] !== undefined) {
232   - // this.teacherFileName = files[0].name;
233   - // if (this.teacherFileName.lastIndexOf(".") <= 0) {
234   - // return;
235   - // }
236   - // const fr = new FileReader();
237   - // fr.readAsDataURL(files[0]);
238   - // fr.addEventListener("load", () => {
239   - // this.teacherFileUrl = fr.result;
240   - // // this.imageFile = files[0]; // this is an image file that can be sent to server...
241   - // });
242   - // } else {
243   - // this.teacherFileName = "";
244   - // this.teacherFileUrl = "";
245   - // }
246   - // }
  312 + },
  313 + csvParentJSON(csvParent) {
  314 + var lines = csvParent.split("\n");
  315 +
  316 + var result = [];
  317 +
  318 + var headers = lines[0].split(",");
  319 +
  320 + for (var i = 1; i < lines.length; i++) {
  321 + var obj = {};
  322 + var currentline = lines[i].split(",");
  323 +
  324 + for (var j = 0; j < headers.length; j++) {
  325 + obj[headers[j]] = currentline[j];
  326 + }
  327 +
  328 + this.parentFileJson.push(obj);
  329 + }
  330 + },
  331 + csvUserJSON(cvsUser) {
  332 + var lines = cvsUser.split("\n");
  333 +
  334 + var result = [];
  335 +
  336 + var headers = lines[0].split(",");
  337 +
  338 + for (var i = 1; i < lines.length; i++) {
  339 + var obj = {};
  340 + var currentline = lines[i].split(",");
  341 +
  342 + for (var j = 0; j < headers.length; j++) {
  343 + obj[headers[j]] = currentline[j];
  344 + }
  345 +
  346 + this.userFileJson.push(obj);
  347 + }
  348 + },
  349 + csvBookJSON(cvsBook) {
  350 + var lines = cvsBook.split("\n");
  351 +
  352 + var result = [];
  353 +
  354 + var headers = lines[0].split(",");
  355 +
  356 + for (var i = 1; i < lines.length; i++) {
  357 + var obj = {};
  358 + var currentline = lines[i].split(",");
  359 +
  360 + for (var j = 0; j < headers.length; j++) {
  361 + obj[headers[j]] = currentline[j];
  362 + }
  363 +
  364 + this.bookFileJson.push(obj);
  365 + }
  366 + },
  367 + importTeacher() {
  368 + this.loading = true;
  369 + var teacherfile = {};
  370 + teacherfile.teacherData = this.teacherFileJson;
  371 + http()
  372 + .post("/importTeacherData", teacherfile, {
  373 + headers: { Authorization: "Bearer " + this.token }
  374 + })
  375 + .then(response => {
  376 + this.snackbar = true;
  377 + this.text = response.data.message;
  378 + this.loading = false;
  379 + })
  380 + .catch(error => {
  381 + this.snackbar = true;
  382 + this.text = error.response.data.message;
  383 + this.loading = false;
  384 + });
  385 + },
  386 + importParent() {
  387 + this.loading = true;
  388 + var parentfile = {};
  389 + parentfile.parentData = this.parentFileJson;
  390 + http()
  391 + .post("/importParentData", parentfile, {
  392 + headers: { Authorization: "Bearer " + this.token }
  393 + })
  394 + .then(response => {
  395 + this.snackbar = true;
  396 + this.text = response.data.message;
  397 + this.loading = false;
  398 + })
  399 + .catch(error => {
  400 + this.snackbar = true;
  401 + this.text = error.response.data.message;
  402 + this.loading = false;
  403 + });
  404 + },
  405 + importUser() {
  406 + this.loading = true;
  407 + var userfile = {};
  408 + userfile.userData = this.userFileJson;
  409 + http()
  410 + .post("/importUserData", userfile, {
  411 + headers: { Authorization: "Bearer " + this.token }
  412 + })
  413 + .then(response => {
  414 + this.snackbar = true;
  415 + this.text = response.data.message;
  416 + this.loading = false;
  417 + })
  418 + .catch(error => {
  419 + this.snackbar = true;
  420 + this.text = error.response.data.message;
  421 + this.loading = false;
  422 + });
  423 + },
  424 + importBook() {
  425 + this.loading = true;
  426 + var bookfile = {};
  427 + bookfile.bookData = this.bookFileJson;
  428 + http()
  429 + .post("/importBookData", bookfile, {
  430 + headers: { Authorization: "Bearer " + this.token }
  431 + })
  432 + .then(response => {
  433 + this.snackbar = true;
  434 + this.text = response.data.message;
  435 + this.loading = false;
  436 + })
  437 + .catch(error => {
  438 + this.snackbar = true;
  439 + this.text = error.response.data.message;
  440 + this.loading = false;
  441 + });
247 442 }
248 443 },
249 444 mounted() {
  445 + this.token = this.$store.state.token;
250 446 /** TEACHERS SAMPLE CSV */
251 447 const getTeacherData = teacherData();
252 448 this.teacherData = getTeacherData;
... ... @@ -268,4 +464,6 @@ export default {
268 464 this.bookData = getBookData;
269 465 }
270 466 };
271   -</script>
272 467 \ No newline at end of file
  468 +</script>
  469 +
  470 +
... ...
src/script/books.js
1 1 export default () => {
2 2  
3 3 const books = [{
4   - "Book": "Math",
5   - "Subject": "002015",
6   - "Author": "R D Sharma",
7   - "Price": "600",
8   - "Quantity": "15",
9   - "Rack": "110",
  4 + "name": "Neeraj",
  5 + "subjectCode": "002015",
  6 + "author": "R D Sharma",
  7 + "price": "600",
  8 + "quantity": "15",
  9 + "rackNo": "110"
10 10 },
11 11 {
12   - "Book": "Hindi",
13   - "Subject": "059090",
14   - "Author": "V Kumar",
15   - "Price": "400",
16   - "Quantity": "5",
17   - "Rack": "10",
  12 + "name": "Rishav",
  13 + "subjectCode": "004018",
  14 + "author": "D k Kumar",
  15 + "price": "400",
  16 + "quantity": "5",
  17 + "rackNo": "10"
18 18 },
19 19 ]
20 20 return books;
... ...
src/script/parents.js
... ... @@ -3,16 +3,17 @@ export default () =&gt; {
3 3 const parents = [{
4 4 "email": "digital@theideazfactory.com",
5 5 "fatherName": "Abhishek",
6   - "fatherCellNo": "9898981234",
7 6 "motherName": "Rita",
8 7 "motherCellNo": "8989786789",
  8 + "fatherCellNo": "9898981234"
  9 +
9 10 },
10 11 {
11 12 "email": "Rampal@viithiisys.com",
12 13 "fatherName": "suresh",
13   - "fatherCellNo": "9898987867",
14 14 "motherName": "rita kumari",
15   - "motherCellNo": "9768576536",
  15 + "fatherCellNo": "9898987867",
  16 + "motherCellNo": "9768576536"
16 17 }
17 18 ]
18 19 return parents;
... ...
src/script/teachers.js
... ... @@ -6,21 +6,35 @@ export default () =&gt; {
6 6 "dob": "1994-05-01",
7 7 "city": "Chandigarh",
8 8 "country": "India",
9   - "presentAddress": "SCO 34, second floor",
  9 + "presentAddress": "SCO 34 second floor",
10 10 "mobileNo": "08558875847",
11 11 "state": "Chandigarh",
12   - "joinDate": "2019-11-18",
  12 + "pincode": "160071",
  13 + "joinDate": "2019-11-18"
13 14 },
14 15 {
15   - "name": "Neeraj Sharma",
16   - "email": "neeraj.sharma@viithiisys.com",
  16 + "name": "Kuldeep Kumar",
  17 + "email": "kuldeep.kumar@viithiisys.com",
17 18 "dob": "1995-01-14",
18 19 "city": "Mohali",
19 20 "country": "India",
20   - "presentAddress": "sector 71 ,Mohali",
  21 + "presentAddress": "sector 71 Mohali",
21 22 "mobileNo": "7009191451",
22 23 "state": "Punjab",
23   - "joinDate": "2018-04-23",
  24 + "pincode": "160072",
  25 + "joinDate": "2018-04-23"
  26 + },
  27 + {
  28 + "name": "Sunny Negi",
  29 + "email": "sunny.negi@viithiisys.com",
  30 + "dob": "1995-01-14",
  31 + "city": "Phanckula",
  32 + "country": "India",
  33 + "presentAddress": "sector 71 Mohali",
  34 + "mobileNo": "8989981451",
  35 + "state": "haryana",
  36 + "pincode": "160073",
  37 + "joinDate": "2018-04-24"
24 38 }
25 39 ]
26 40 return teachers;
... ...