handling isFullTankString and making post request to api
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import Layout from '@layouts/main.vue'
|
import Layout from '@layouts/main.vue'
|
||||||
import { mapState } from 'vuex'
|
import { mapState } from 'vuex'
|
||||||
// import axios from 'axios'
|
import axios from 'axios'
|
||||||
import Papa from 'papaparse'
|
import Papa from 'papaparse'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -78,11 +78,12 @@ export default {
|
|||||||
this.errorMessage = ''
|
this.errorMessage = ''
|
||||||
Papa.parse(this.file, this.papaConfig)
|
Papa.parse(this.file, this.papaConfig)
|
||||||
},
|
},
|
||||||
|
getUsedHeadings() {
|
||||||
|
return Object.keys(this.fileHeadingMap).filter((k) => this.fileHeadingMap[k] != null) // filter non-null properties
|
||||||
|
},
|
||||||
csvToJson() {
|
csvToJson() {
|
||||||
const data = []
|
const data = []
|
||||||
const headings = Object.keys(this.fileHeadingMap)
|
const headings = this.getUsedHeadings().reduce((a, k) => ({ ...a, [k]: this.fileHeadingMap[k] }), {}) // create new object from filter
|
||||||
.filter((k) => this.fileHeadingMap[k] != null) // filter non-null properties
|
|
||||||
.reduce((a, k) => ({ ...a, [k]: this.fileHeadingMap[k] }), {}) // create new object from filter
|
|
||||||
const comments = (row) => {
|
const comments = (row) => {
|
||||||
return this.fileHeadingMap.comments.reduce((a, fi) => {
|
return this.fileHeadingMap.comments.reduce((a, fi) => {
|
||||||
// TODO: sanitize to prevent XSS
|
// TODO: sanitize to prevent XSS
|
||||||
@@ -95,6 +96,17 @@ export default {
|
|||||||
: row[this.fileHeadingMap.totalAmount]
|
: row[this.fileHeadingMap.totalAmount]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const setFullTank = (row) => {
|
||||||
|
if (row[this.fileHeadingMap.isTankFull] === this.filledValueString) {
|
||||||
|
return true
|
||||||
|
} else if (row[this.fileHeadingMap.isTankFull] === this.notFilledValueString) {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
// TODO: need to handle errors better
|
||||||
|
throw Error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (let r = 1; r < this.fileData.length; r++) {
|
for (let r = 1; r < this.fileData.length; r++) {
|
||||||
const row = this.fileData[r]
|
const row = this.fileData[r]
|
||||||
const item = {}
|
const item = {}
|
||||||
@@ -103,6 +115,10 @@ export default {
|
|||||||
item[k] = comments(row)
|
item[k] = comments(row)
|
||||||
} else if (k === 'totalAmount') {
|
} else if (k === 'totalAmount') {
|
||||||
item[k] = calculateTotal(row)
|
item[k] = calculateTotal(row)
|
||||||
|
} else if (this.isFullTankString) {
|
||||||
|
item[k] = setFullTank(row)
|
||||||
|
} else if (this.invertFullTank) {
|
||||||
|
item[k] = !row[headings[k]]
|
||||||
} else {
|
} else {
|
||||||
item[k] = row[headings[k]]
|
item[k] = row[headings[k]]
|
||||||
}
|
}
|
||||||
@@ -114,10 +130,34 @@ export default {
|
|||||||
importData() {
|
importData() {
|
||||||
if (this.errors.length === 0) {
|
if (this.errors.length === 0) {
|
||||||
try {
|
try {
|
||||||
const content = this.csvToJson()
|
const content = {
|
||||||
alert(JSON.stringify(content))
|
data: this.csvToJson(),
|
||||||
|
vehicleId: this.selectedVehicle.Id,
|
||||||
|
}
|
||||||
|
axios
|
||||||
|
.post('/api/import/generic', content)
|
||||||
|
.then((data) => {
|
||||||
|
this.$buefy.toast.open({
|
||||||
|
message: this.$t('importsuccessfull'),
|
||||||
|
type: 'is-success',
|
||||||
|
duration: 3000,
|
||||||
|
})
|
||||||
|
setTimeout(() => this.$router.push({ name: 'home' }), 1000)
|
||||||
|
})
|
||||||
|
.catch((ex) => {
|
||||||
|
this.$buefy.toast.open({
|
||||||
|
duration: 5000,
|
||||||
|
message: this.$t('importerror'),
|
||||||
|
position: 'is-bottom',
|
||||||
|
type: 'is-danger',
|
||||||
|
})
|
||||||
|
if (ex.response && ex.response.data.errors) {
|
||||||
|
this.errors = ex.response.data.errors
|
||||||
|
}
|
||||||
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
alert(e)
|
// TODO: handle error
|
||||||
|
this.errors.push(e)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.errors.push('fix errors')
|
this.errors.push('fix errors')
|
||||||
|
|||||||
Reference in New Issue
Block a user