1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<style lang="scss">
#importValidate {
height: 500px;
overflow-y: scroll;
width: 650px;
.lpButton {
margin-bottom: 30px;
}
}
</style>
<template>
<div id="importCSV">
<modal id="importValidate" :shown="shown" @hide="shown = false">
<h2>Confirm your import</h2>
<div id="importData">
<ul class="lpTable lpDataTable">
<li class="lpRow lpHeader">
<span class="lpCell">Item Name</span>
<span class="lpCell">Category</span>
<span class="lpCell">Description</span>
<span class="lpCell">Qty</span>
<span class="lpCell">Weight</span>
<span class="lpCell">Unit</span>
</li>
<li v-for="row in importData.data" class="lpRow">
<span class="lpCell">{{ row.name }}</span>
<span class="lpCell">{{ row.category }}</span>
<span class="lpCell">{{ row.description }}</span>
<span class="lpCell">{{ row.qty }}</span>
<span class="lpCell">{{ row.weight }}</span>
<span class="lpCell">{{ row.unit }}</span>
</li>
</ul>
</div>
<a id="importConfirm" class="lpButton" @click="importList">Import List</a>
<a class="lpButton close" @click="shown = false">Cancel Import</a>
</modal>
<form id="csvUpload">
<input id="csv" type="file" name="csv">
</form>
</div>
</template>
<script>
import modal from './modal.vue';
export default {
name: 'ImportCsv',
components: {
modal,
},
data() {
return {
csvInput: false,
listId: false,
importData: {},
fullUnitToUnit: {
gram: 'g', grams: 'g', g: 'g', kilogram: 'kg', kilograms: 'kg', kg: 'kg', kgs: 'kg',
},
shown: false,
};
},
computed: {
library() {
return this.$store.state.library;
},
},
mounted() {
this.csvInput = document.getElementById('csv');
this.csvInput.onchange = this.importCSV;
bus.$on('importCSV', () => {
this.csvInput.click();
});
},
methods: {
importCSV(evt) {
const file = evt.target.files[0];
const name = file.name;
const size = file.size;
const type = file.type;
if (file.name.length < 1) {
return;
}
if (file.size > 1000000) {
alert('File is too big');
return;
}
if (name.substring(name.length - 4).toLowerCase() != '.csv') {
alert('Please select a CSV.');
return;
}
const reader = new FileReader();
reader.onload = ((theFile) => {
this.validateImport(theFile.target.result, file.name.substring(0, file.name.length - 4).replace(/\_/g, ' '));
});
reader.readAsText(file);
},
CSVToArray(strData) {
const strDelimiter = ',';
const arrData = [[]];
let arrMatches = null;
const objPattern = new RegExp(
(
`(\\${strDelimiter}|\\r?\\n|\\r|^)`
+ '(?:"([^"]*(?:""[^"]*)*)"|'
+ `([^"\\${strDelimiter}\\r\\n]*))`
), 'gi',
);
while (arrMatches = objPattern.exec(strData)) {
const strMatchedDelimiter = arrMatches[1];
if (strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter)) {
arrData.push([]);
}
if (arrMatches[2]) {
var strMatchedValue = arrMatches[2].replace(new RegExp('""', 'g'), '"');
} else {
var strMatchedValue = arrMatches[3];
}
arrData[arrData.length - 1].push(strMatchedValue);
}
return arrData;
},
validateImport(input, name) {
const csv = this.CSVToArray(input);
this.importData = { data: [], name };
for (const i in csv) {
const row = csv[i];
if (row.length < 6) continue;
if (row[0].toLowerCase() == 'item name') continue;
if (isNaN(parseInt(row[3]))) continue;
if (isNaN(parseInt(row[4]))) continue;
if (typeof this.fullUnitToUnit[row[5]] === 'undefined') continue;
this.importData.data.push({
name: row[0],
category: row[1],
description: row[2],
qty: parseFloat(row[3]),
weight: parseFloat(row[4]),
unit: this.fullUnitToUnit[row[5]],
});
}
if (!this.importData.data.length) {
alert('Unable to load spreadsheet - please verify the format.');
} else {
this.shown = true;
}
},
importList() {
this.$store.commit('importCSV', this.importData);
this.shown = false;
},
},
};
</script>