From 21c55c579c4808cc0b005dbdb70a666e793eb44a Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Sat, 9 Jul 2016 15:46:06 -0400 Subject: [PATCH] Add/update group target as needed When user submits a new target, target data table is checked for an existing entry based on target email. If an existing entry is found then it is updated, otherwise a new row is added to table. Target email addresses are now converted to lower case to help prevent duplicates as well. --- static/js/app/users.js | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/static/js/app/users.js b/static/js/app/users.js index 0059a410..84cb2a5d 100644 --- a/static/js/app/users.js +++ b/static/js/app/users.js @@ -171,15 +171,34 @@ $(document).ready(function() { // Setup the event listeners // Handle manual additions $("#targetForm").submit(function() { - targets.DataTable() - .row.add([ - escapeHtml($("#firstName").val()), - escapeHtml($("#lastName").val()), - escapeHtml($("#email").val()), - escapeHtml($("#position").val()), - '' - ]) - .draw(); + // Create new data row. + var emailInput = escapeHtml($("#email").val()).toLowerCase(); + var newRow = [ + escapeHtml($("#firstName").val()), + escapeHtml($("#lastName").val()), + emailInput, + escapeHtml($("#position").val()), + '' + ]; + + // Check table to see if email already exists. + var targetsTable = targets.DataTable(); + var existingRowIndex = targetsTable + .column(2, {order: "index"}) // Email column + .data() + .indexOf(emailInput); + + // Update or add new row as necessary. + if (existingRowIndex >= 0) { + targetsTable + .row(existingRowIndex, {order: "index"}) + .data(newRow); + } else { + targetsTable.row.add(newRow); + } + targetsTable.draw(); + + // Reset user input. $("#targetForm>div>input").val(''); $("#firstName").focus(); return false;