Last active
January 17, 2020 21:48
-
-
Save dperussina/4ce286aaaa3e95a50bf5cbd93c509052 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function () { | |
var _v = window.VIEW.CsvImport; | |
var _s = window.SERVICE.upload; | |
// _v.input.Upload.change(upload); | |
var DATA = ""; | |
_v.button.Upload.click(function (e) { | |
console.log('Upload Button Fired.') | |
e.preventDefault(); | |
if (DATA == "") { | |
_v.status.text('CSV Not detected.'); | |
_v.status.addClass('text-danger'); | |
return; | |
}else if(_v.input.WorkDate.val() == ""){ | |
_v.status.text('Please first select a date to import.'); | |
_v.status.addClass('text-danger'); | |
return; | |
} | |
var _data = { | |
params: { | |
username: CONFIG.username, | |
csvData: DATA, | |
workDate:_v.input.WorkDate.val() | |
}, | |
url: '/api/Upload_CSV?' | |
}; | |
_s(_data, function (err, reply) { | |
console.log('UPLOAD', _data, err, reply); | |
if (err || reply.error == true) { | |
_v.status.text(reply.errorText); | |
_v.status.addClass('text-danger'); | |
}else{ | |
_v.status.text("Upload Success! You may review data under Update Driver Records."); | |
_v.status.removeClass('text-danger'); | |
} | |
_v.input.Upload.val(''); | |
}); | |
}); | |
document.querySelector("input[type=file]").addEventListener('change', upload, false); | |
function browserSupportFileUpload() { | |
var isCompatible = false; | |
if (window.File && window.FileReader && window.FileList && window.Blob) { | |
isCompatible = true; | |
} | |
return isCompatible; | |
} | |
function upload(evt) { | |
console.log("in upload", evt); | |
if (!browserSupportFileUpload()) { | |
alert('The File APIs are not fully supported in this browser!'); | |
} else { | |
var data = null; | |
var file = evt.target.files[0]; | |
var reader = new FileReader(); | |
reader.readAsText(file); | |
reader.onload = function (event) { | |
var csvData = event.target.result; | |
DATA = csvData; | |
_v.status.text('CSV detected.. Submit using "Upload CSV" button.'); | |
_v.status.removeClass('text-danger'); | |
}; | |
reader.onerror = function () { | |
alert('Unable to read ' + file.fileName); | |
}; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment