Skip to content

Commit

Permalink
Continue #25: Send file via AJAX
Browse files Browse the repository at this point in the history
Instead of just submitting a form, I'm now sending the file to be
imported via AJAX so that we can interpret the data once it is returned
from the server, and send our POSTs via the API (for non-duplicate
rows).

Note that future commits will assume that we're importing a csv of a
format like "Client.csv" in the examples directory.  Does that sound
right?
  • Loading branch information
cecilia-donnelly committed Aug 4, 2015
1 parent b34aeba commit 3484230
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ html(lang="en")
script(type='text/javascript', src='js/lib/FileSaver.js/FileSaver.min.js')
script(type='text/javascript', src='js/lib/Blob.js/Blob.min.js')
script(type='text/javascript', src='js/lib/sampleData.js')
script(type='text/javascript', src='js/jquery.csv.js')
script(type='text/javascript', src='js/index.js')

body
Expand All @@ -25,9 +26,9 @@ html(lang="en")
img(src='img/OpenHMIS-logo.jpg')
div#search
h1 Client Search
form(action="/upload", method="post", enctype="multipart/form-data")
form(action="/upload", method="post", enctype="multipart/form-data" id="importForm")
input#import_file(type='file' name="import.csv")
button#importAll(type='submit')
button#importAll(type='button')
form#searchForm
div.form-group
input#searchField.form-control(name='searchField', type='text', placeholder='search by first or last name')
Expand Down
23 changes: 23 additions & 0 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ $(function() {
$("#searchForm #exportAll").click(function() {
exportAll();
});
$("#importAll").click(function() {
importAll();
});

$("#intakeForm input").keyup(function(e) {
checkForChanges(data);
});
Expand Down Expand Up @@ -516,6 +520,25 @@ $(function() {
console.log("DEBUG: done exporting enrollments");
}

function importAll() {
// get file
var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);
$.ajax("/upload", {
method: "POST",
data: formData,
processData: false,
contentType: false
}).done(function(response) {
console.log(response);
// read each line of file
// for each line, check whether ssn exists via API
// if it does, put that line in a list of duplicates
// if it doesn't, POST that line to the API
// display any duplicates to the user
});
}

function switchToIntake(personalId, data_length, dataset) {
// Reset all form fields.
$("#intakeForm input[type='input']").val("");
Expand Down

0 comments on commit 3484230

Please sign in to comment.