Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usagov-2077-usps-api-error: update code to remove specail characters … #2121

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions web/themes/custom/usagov/scripts/ceoSubmission.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,16 @@ const state_codes = {
async function addressUSPSValidation(streetAddress, city, state, zipCode) {
"use strict";

// If the zip code contains any letters or is less or more than 5 characters, it returns an error.
// If the Address contains any special characters it removes them because the USPS API
// won't process the address.
if (streetAddress.includes("#")) {
streetAddress = streetAddress.replace('#', '');
let streetAddressField = document.getElementById("input-street");
streetAddressField.value = streetAddress;
}

// If the zip code contains any letters or is less or more than 5 characters, it returns and
// doesn't call the USPS API.
if (zipCode.length !== 5 || !(/^\d+$/.test(zipCode))) {
return "Invalid Zip Code.";
}
Expand All @@ -134,17 +143,20 @@ async function addressUSPSValidation(streetAddress, city, state, zipCode) {
const response = await fetch(url);
var responseText = response.text();

if (!response.ok || (await responseText).includes("<Error>")) {
return (responseText);
if (!response.ok) {
return "USPS API not working.";
}
else {
(await responseText).includes("<Error>");
}

return await responseText;
}
catch (error) {
return "USPS API not working.";
}
}


// This function analyzes the response received by the USPS API and returns the message that the user will see.
function uspsResponseParser(responseText, userStreetAddress, userCity, userZipCode) {
"use strict";
Expand Down Expand Up @@ -225,6 +237,14 @@ async function handleFormSubmission() {
const uspsApiResponse = await addressUSPSValidation(streetAddressField.value, cityField.value, stateField.value, zipCodeField.value);
const response = uspsResponseParser(uspsApiResponse, streetAddressField.value, cityField.value, zipCodeField.value);

// This removes the number sign fromt the address field so
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this commented-out code.

// there won't be an xml syntax error when the form is submitted.
// if (streetAddressField.value.includes("#")) {
// let streetValue = streetAddressField.value;
// streetValue = streetValue.replace('#', '');
// streetAddressField.value = streetValue;
// };

formFields.forEach(field => {
let fieldID = field.previousElementSibling.id;
var errorID = "error-" + fieldID;
Expand Down Expand Up @@ -307,7 +327,6 @@ async function handleFormSubmission() {
}
});


// If all fields have an error, join the error lines on the left into one.
if (test.length === 4) {
document.getElementById("error-border").classList.add("usa-main-border-error");
Expand Down
Loading