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;