From df0fad60b1ff2fd004e685e8a64d2430643084a8 Mon Sep 17 00:00:00 2001 From: cecilia-donnelly Date: Tue, 26 Jul 2016 11:59:31 -0500 Subject: [PATCH] Display success/failure messages from import: #25 Show any API errors returned by POST-ing records from the import. Also let the user know when items were imported correctly. Unrelated, but also move the function that gets information about the logged-in user so that it is only run on page load, not every time we switch to the search page view. --- app/controllers/client.server.controller.js | 1 + public/js/index.js | 23 ++++++++++++++++++--- public/js/login.js | 5 ++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/controllers/client.server.controller.js b/app/controllers/client.server.controller.js index 77fcdaa..6597992 100644 --- a/app/controllers/client.server.controller.js +++ b/app/controllers/client.server.controller.js @@ -49,6 +49,7 @@ exports.addClient = function(req, res) { var data = [] res_post.on('data', function (chunk) { console.log('DEBUG: Response: ' + chunk); + data.push(chunk); }); res_post.on('end', function() { res.send(data.join('')); diff --git a/public/js/index.js b/public/js/index.js index c9f142b..382ee82 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -122,13 +122,14 @@ function getClients(token) { var ssn_array = []; // get all existing ssn's // full set of data retrieved via API + // when does this get refreshed? var dataset = $("#index").data("full-data"); for (var client in dataset){ ssn_array.push(dataset[client]['ssn']); } var duplicate_lines = ""; // assigning handler - reader.onloadend = function(evt) { + reader.onloadend = function(evt) { lines = evt.target.result.split(/\r?\n/); var line_counter = 0; // Possible values are: @@ -1116,13 +1117,13 @@ function getClients(token) { var line = line_object['data'][0]; // test whether the line is defined if (line) { + // check for duplicate SSN if (ssn_array.indexOf(line[7]) > 0) { return_array[0] = true; //also pass first and last name return_array[1] = line[2]; return_array[2] = line[4]; } - // - // if it doesn't, POST that record to the API + // if it isn't a dupe, POST the record to the API else { // get line into correct format for POSTing var new_client = {}; @@ -1159,6 +1160,22 @@ function getClients(token) { method: "POST", data: new_client, always: console.log("finished post") + }).done( function (response) { + // give user feedback about success of import + var result = JSON.parse(response); + if (result.error){ + // TBD: see issue #35's comment about making + // these API errors more human-friendly + var message = result.error.errors[0]['message']; + var problem = result.error.errors[0]['problem']; + // add message to the results display + var failure_line = "Line " + line_counter + " had an import error: " + problem + "
"; + $("#results").append(failure_line); + } + else { + var success_line = "Line " + line_counter + " (" + result.data.item.firstName + " " + result.data.item.lastName + ") imported correctly.
"; + $("#results").append(success_line); + } }); } diff --git a/public/js/login.js b/public/js/login.js index 1204193..9788236 100644 --- a/public/js/login.js +++ b/public/js/login.js @@ -10,6 +10,8 @@ $(function() { if (id_token) { getClients(id_token); switchToSearch(false); + // fill in account that was used to log in + getLoginInfo(id_token); } else { switchToLogin(false); @@ -69,9 +71,6 @@ function switchToSearch(keepResults) { $("#searchForm #addNewClient").prop("disabled", true); } $("#search").css("display", "block"); - // fill in account that was used to log in - var id_token = getIdCookie(); - getLoginInfo(id_token); $("#intake").css("display", "none"); $("#login").css("display", "none"); $("#warning").css("display", "none");