diff --git a/Dockerfile b/Dockerfile index 4c464fdf..d250560a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - # # Read more on Dockerfile best practices at the source: # https://docs.docker.com/develop/develop-images/dockerfile_best-practices -RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client nodejs +RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client nodejs npm # Inside the container, create an app directory and switch into it RUN mkdir /app diff --git a/parserator_web/static/js/index.js b/parserator_web/static/js/index.js index 492674cc..d54c6322 100644 --- a/parserator_web/static/js/index.js +++ b/parserator_web/static/js/index.js @@ -1,2 +1,68 @@ /* TODO: Flesh this out to connect the form to the API and render results in the #address-results div. */ + +// Adding event listeners + document.addEventListener("DOMContentLoaded", function () { + var form = document.getElementById("address-form"); + + form.addEventListener("submit", async function (e) { + e.preventDefault(); + + // Declaring variables + var address = document.getElementById("address").value; + var url = "/api/parse/"; + var queryParams = new URLSearchParams({ address: address }); + + try { + var response = await fetch(`${url}?${queryParams}`, { + method: 'GET' + }); + + var contentData = await response.json(); + + if (!response.ok) { + throw new Error(contentData.detail || contentData.error); + } + + displayResults(contentData); + } catch (error) { + displayError(error.message); + } + }); + + function displayResults(data) { + document.getElementById("error-results").style.display = "none"; + var resultsDiv = document.getElementById("address-results"); + resultsDiv.style.display = "block"; + + document.getElementById("parse-type").textContent = data.address_type; + + var tableBody = document.querySelector("#address-results-table tbody"); + tableBody.innerHTML = ''; + + // Creating table + for (var [key, value] of Object.entries(data.address_components)) { + var row = document.createElement("tr"); + var partCell = document.createElement("td"); + var tagCell = document.createElement("td"); + + partCell.textContent = key; + tagCell.textContent = value; + + row.appendChild(partCell); + row.appendChild(tagCell); + + tableBody.appendChild(row); + } + } + + function displayError(errorMessage) { + document.getElementById("address-results").style.display = "none"; + + var errorDiv = document.getElementById("error-results"); + errorDiv.style.display = "block"; + + document.getElementById("parse-error").textContent = errorMessage; + } + }); + \ No newline at end of file diff --git a/parserator_web/templates/parserator_web/index.html b/parserator_web/templates/parserator_web/index.html index a72d9c80..f6e28a7d 100644 --- a/parserator_web/templates/parserator_web/index.html +++ b/parserator_web/templates/parserator_web/index.html @@ -11,17 +11,21 @@

U.S. addres

Dealing with some messy or unstructured addresses? We can parse them for you.

Try it out! Parse an address in the United States into fields like AddressNumber, StreetName and ZipCode.

-
+ {% csrf_token %}
+ +