This commit is contained in:
David Kennedy 2024-11-06 11:07:50 -05:00
parent df6649a12e
commit e948dc4311
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
2 changed files with 106 additions and 58 deletions

View file

@ -103,27 +103,40 @@ function handlePortfolioSelection() {
const orgNameFieldSet = document.querySelector(".field-organization_name").parentElement; const orgNameFieldSet = document.querySelector(".field-organization_name").parentElement;
const orgNameFieldSetDetails = orgNameFieldSet.nextElementSibling; const orgNameFieldSetDetails = orgNameFieldSet.nextElementSibling;
const portfolioOrgTypeFieldSet = document.querySelector(".field-portfolio_organization_type").parentElement; const portfolioOrgTypeFieldSet = document.querySelector(".field-portfolio_organization_type").parentElement;
const portfolioOrgNameFieldSet = document.querySelector(".field-portfolio_organization_name").parentElement; const portfolioOrgType = document.querySelector(".field-portfolio_organization_type .readonly");
const portfolioFederalTypeField = document.querySelector(".field-portfolio_federal_type");
const portfolioFederalType = portfolioFederalTypeField.querySelector(".readonly");
const portfolioOrgNameField = document.querySelector(".field-portfolio_organization_name")
const portfolioOrgName = portfolioOrgNameField.querySelector(".readonly");
const portfolioOrgNameFieldSet = portfolioOrgNameField.parentElement;
const portfolioOrgNameFieldSetDetails = portfolioOrgNameFieldSet.nextElementSibling; const portfolioOrgNameFieldSetDetails = portfolioOrgNameFieldSet.nextElementSibling;
const portfolioFederalAgencyField = document.querySelector(".field-portfolio_federal_agency");
const portfolioFederalAgency = portfolioFederalAgencyField.querySelector(".readonly");
const portfolioStateTerritory = document.querySelector(".field-portfolio_state_territory .readonly");
const portfolioAddressLine1 = document.querySelector(".field-portfolio_address_line1 .readonly");
const portfolioAddressLine2 = document.querySelector(".field-portfolio_address_line2 .readonly");
const portfolioCity = document.querySelector(".field-portfolio_city .readonly");
const portfolioZipcode = document.querySelector(".field-portfolio_zipcode .readonly");
const portfolioUrbanizationField = document.querySelector(".field-portfolio_urbanization");
const portfolioUrbanization = portfolioUrbanizationField.querySelector(".readonly");
const portfolioJsonUrl = document.getElementById("portfolio_json_url")?.value || null; const portfolioJsonUrl = document.getElementById("portfolio_json_url")?.value || null;
let isPageLoading = true; let isPageLoading = true;
function getPortfolio(portfolio_id) { function getPortfolio(portfolio_id) {
// get portfolio via ajax return fetch(`${portfolioJsonUrl}?id=${portfolio_id}`)
fetch(`${portfolioJsonUrl}?id=${portfolio_id}`) .then(response => response.json())
.then(response => { .then(data => {
return response.json().then(data => data); if (data.error) {
}) console.error("Error in AJAX call: " + data.error);
.then(data => { return null;
if (data.error) { } else {
console.error("Error in AJAX call: " + data.error); return data;
} else { }
return data; })
} .catch(error => {
}) console.error("Error retrieving portfolio", error);
.catch(error => { return null;
console.error("Error retrieving portfolio", error) });
});
} }
function updatePortfolioFieldsData(portfolio) { function updatePortfolioFieldsData(portfolio) {
@ -131,12 +144,38 @@ function handlePortfolioSelection() {
// values in portfolio.suborganizations // values in portfolio.suborganizations
suborganizationDropdown.empty(); suborganizationDropdown.empty();
// // update autocomplete url for suborganizationDropdown // update portfolio senior official
// suborganizationDropdown.attr("data-ajax--url", "/admin/api/get-suborganization-list-json/?portfolio_id=" + portfolio);
// update portfolio senior official field with portfolio.senior_official
// update portfolio organization type // update portfolio organization type
portfolioOrgType.innerText = portfolio.organization_type;
// update portfolio federal type
portfolioFederalType.innerText = portfolio.federal_type
// update portfolio organization name
portfolioOrgName.innerText = portfolio.organization_name;
// update portfolio federal agency
portfolioFederalAgency.innerText = portfolio.federal_agency;
// update portfolio state
portfolioStateTerritory.innerText = portfolio.state_territory;
// update portfolio address line 1
portfolioAddressLine1.innerText = portfolio.address_line1;
// update portfolio address line 2
portfolioAddressLine2.innerText = portfolio.address_line2;
// update portfolio city
portfolioCity.innerText = portfolio.city;
// update portfolio zip code
portfolioZipcode.innerText = portfolio.zipcode
// update portfolio urbanization
portfolioUrbanization.innerText = portfolio.urbanization;
} }
function updatePortfolioFieldsDataDynamicDisplay() { function updatePortfolioFieldsDataDynamicDisplay() {
@ -146,49 +185,42 @@ function handlePortfolioSelection() {
// This is the additional information that exists beneath the SO element. // This is the additional information that exists beneath the SO element.
var contactList = document.querySelector(".field-portfolio_senior_official .dja-address-contact-list"); var contactList = document.querySelector(".field-portfolio_senior_official .dja-address-contact-list");
const federalAgencyContainer = document.querySelector(".field-portfolio_federal_agency");
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
let federalAgencyValue = federalAgencyContainer.querySelector(".readonly").innerText; let federalAgencyValue = portfolioFederalAgency.innerText;
let readonlyOrganizationType = document.querySelector(".field-portfolio_organization_type .readonly"); let portfolioOrgTypeValue = portfolioOrgType.innerText;
let organizationTypeValue = readonlyOrganizationType.innerText;
let organizationNameContainer = document.querySelector(".field-portfolio_organization_name"); if (federalAgencyValue && portfolioOrgTypeValue) {
let federalType = document.querySelector(".field-portfolio_federal_type"); handleFederalAgencyChange(federalAgencyValue, portfolioOrgTypeValue, portfolioOrgNameField, portfolioFederalTypeField);
if (federalAgencyValue && organizationTypeValue) {
handleFederalAgencyChange(federalAgencyValue, organizationTypeValue, organizationNameContainer, federalType);
} }
// Handle dynamically hiding the urbanization field // Handle dynamically hiding the urbanization field
let urbanizationField = document.querySelector(".field-portfolio_urbanization"); let portfolioStateTerritoryValue = portfolioStateTerritory.innerText;
let stateTerritory = document.querySelector(".field-portfolio_state_territory"); if (portfolioUrbanizationField && portfolioStateTerritoryValue) {
let stateTerritoryValue = stateTerritory.innerText; handleStateTerritoryChange(portfolioStateTerritoryValue, portfolioUrbanizationField);
if (urbanizationField && stateTerritoryValue) {
handleStateTerritoryChange(stateTerritoryValue, urbanizationField);
} }
// Handle hiding the organization name field when the organization_type is federal. // Handle hiding the organization name field when the organization_type is federal.
// Run this first one page load, then secondly on a change event. // Run this first one page load, then secondly on a change event.
handleOrganizationTypeChange(organizationTypeValue, organizationNameContainer, federalType); handleOrganizationTypeChange(portfolioOrgTypeValue, portfolioOrgNameField, portfolioFederalTypeField);
}); });
function handleOrganizationTypeChange(organizationTypeValue, organizationNameContainer, federalType) { function handleOrganizationTypeChange(portfolioOrgTypeValue, portfolioOrgNameField, portfolioFederalTypeField) {
if (organizationTypeValue === "federal") { if (portfolioOrgTypeValue === "federal") {
hideElement(organizationNameContainer); hideElement(portfolioOrgNameField);
showElement(federalAgencyContainer); showElement(portfolioFederalAgencyField);
if (federalType) { if (portfolioFederalTypeField) {
showElement(federalType); showElement(portfolioFederalTypeField);
} }
} else { } else {
showElement(organizationNameContainer); showElement(portfolioOrgNameField);
hideElement(federalAgencyContainer); hideElement(portfolioFederalAgencyField);
if (federalType) { if (portfolioFederalTypeField) {
hideElement(federalType); hideElement(portfolioFederalTypeField);
} }
} }
} }
function handleFederalAgencyChange(federalAgencyValue, organizationTypeValue, organizationNameContainer, federalType) { function handleFederalAgencyChange(federalAgencyValue, organizationTypeValue, portfolioOrgNameField, portfolioFederalTypeField) {
// Don't do anything on page load // Don't do anything on page load
if (isInitialPageLoad) { if (isInitialPageLoad) {
isInitialPageLoad = false; isInitialPageLoad = false;
@ -202,15 +234,15 @@ function handlePortfolioSelection() {
if (federalAgencyValue !== "Non-Federal Agency") { if (federalAgencyValue !== "Non-Federal Agency") {
if (organizationTypeValue !== "federal") { if (organizationTypeValue !== "federal") {
readonlyOrganizationType.innerText = "Federal" portfolioOrgType.innerText = "Federal"
} }
}else { }else {
if (organizationTypeValue === "federal") { if (organizationTypeValue === "federal") {
readonlyOrganizationType.innerText = "-" portfolioOrgType.innerText = "-"
} }
} }
handleOrganizationTypeChange(organizationTypeValue, organizationNameContainer, federalType); handleOrganizationTypeChange(organizationTypeValue, portfolioOrgNameField, portfolioFederalTypeField);
// We are missing these hooks that exist on the portfolio template, so I hardcoded them for now: // We are missing these hooks that exist on the portfolio template, so I hardcoded them for now:
// <input id="senior_official_from_agency_json_url" class="display-none" value="/admin/api/get-senior-official-from-federal-agency-json/" /> // <input id="senior_official_from_agency_json_url" class="display-none" value="/admin/api/get-senior-official-from-federal-agency-json/" />
@ -271,11 +303,11 @@ function handlePortfolioSelection() {
} }
function handleStateTerritoryChange(stateTerritoryValue, urbanizationField) { function handleStateTerritoryChange(portfolioStateTerritoryValue, portfolioUrbanizationField) {
if (stateTerritoryValue === "PR") { if (portfolioStateTerritoryValue === "PR") {
showElement(urbanizationField) showElement(portfolioUrbanizationField)
} else { } else {
hideElement(urbanizationField) hideElement(portfolioUrbanizationField)
} }
} }
@ -331,13 +363,16 @@ function handlePortfolioSelection() {
} }
} }
function updatePortfolioFields() { async function updatePortfolioFields() {
if (!isPageLoading) { if (!isPageLoading) {
if (portfolioDropdown.val()) { if (portfolioDropdown.val()) {
let portfolio = getPortfolio(portfolioDropdown.val());
updatePortfolioFieldsData(portfolio); getPortfolio(portfolioDropdown.val()).then((portfolio) => {
updatePortfolioFieldsDisplay(); console.log(portfolio);
updatePortfolioFieldsDataDynamicDisplay(); updatePortfolioFieldsData(portfolio);
updatePortfolioFieldsDisplay();
updatePortfolioFieldsDataDynamicDisplay();
});
} else { } else {
updatePortfolioFieldsDisplay(); updatePortfolioFieldsDisplay();
} }

View file

@ -59,6 +59,9 @@ def get_portfolio_json(request):
# Convert the portfolio to a dictionary # Convert the portfolio to a dictionary
portfolio_dict = model_to_dict(portfolio) portfolio_dict = model_to_dict(portfolio)
# map portfolio federal type
portfolio_dict["federal_type"] = BranchChoices.get_branch_label(portfolio.federal_type) if portfolio.federal_type else "-"
# Add senior official information if it exists # Add senior official information if it exists
if portfolio.senior_official: if portfolio.senior_official:
senior_official = model_to_dict( senior_official = model_to_dict(
@ -73,6 +76,16 @@ def get_portfolio_json(request):
else: else:
portfolio_dict["senior_official"] = None portfolio_dict["senior_official"] = None
# Add federal agency information if it exists
if portfolio.federal_agency:
federal_agency = model_to_dict(
portfolio.federal_agency,
fields=["agency"]
)
portfolio_dict["federal_agency"] = federal_agency["agency"]
else:
portfolio_dict["federal_agency"] = None
return JsonResponse(portfolio_dict) return JsonResponse(portfolio_dict)