From efdd155b3c4e6d9b967e5b3205dd556cf7bec30a Mon Sep 17 00:00:00 2001 From: eelkus01 <130937420+eelkus01@users.noreply.github.com> Date: Mon, 24 Jun 2024 13:38:57 -0400 Subject: [PATCH] election fixed --- client/src/common/index.ts | 7 +++++- client/src/pages/ballotInfo/districtForm.tsx | 1 - .../electionCheckBox/electionCheckbox.tsx | 18 +++++++++++---- client/src/pages/ballotInfo/index.tsx | 23 +++++++++---------- .../whatsOnTheBallot/candidateData.tsx | 18 +++++++-------- 5 files changed, 40 insertions(+), 27 deletions(-) diff --git a/client/src/common/index.ts b/client/src/common/index.ts index 4b267183b..d42db546f 100644 --- a/client/src/common/index.ts +++ b/client/src/common/index.ts @@ -21,9 +21,14 @@ export const localCandidateRoleAPI = localStrapiURL + 'candidate-roles'; export const deployedCandidateRoleAPI = deployedStrapiURL + 'candidate-roles'; -// Global variable and its setting function for district number +// Global variables and their setting functions for district number and selected election export let globalDistrictNum: string | null = null; export const setGlobalDistrictNum = (num: string | null) => { globalDistrictNum = "District " + num; +}; +export let globalCurrElection: string | null = null; + +export const setGlobalCurrElection = (name: string | null) => { + globalCurrElection = name; }; \ No newline at end of file diff --git a/client/src/pages/ballotInfo/districtForm.tsx b/client/src/pages/ballotInfo/districtForm.tsx index fcd3e2af8..0d97d214e 100644 --- a/client/src/pages/ballotInfo/districtForm.tsx +++ b/client/src/pages/ballotInfo/districtForm.tsx @@ -49,7 +49,6 @@ const DistrictForm: React.FC = ({ onFormSubmit }) => { // Set district number or error if no district number if (data) { - console.log(data); setDistrictNum(data); setGlobalDistrictNum(data); onFormSubmit(); diff --git a/client/src/pages/ballotInfo/electionCheckBox/electionCheckbox.tsx b/client/src/pages/ballotInfo/electionCheckBox/electionCheckbox.tsx index e3cbbb97e..71c25786e 100644 --- a/client/src/pages/ballotInfo/electionCheckBox/electionCheckbox.tsx +++ b/client/src/pages/ballotInfo/electionCheckBox/electionCheckbox.tsx @@ -2,7 +2,7 @@ import react from 'react'; import { useState, useEffect } from 'react'; import ElectionCheckboxCard from './electionCheckboxCard'; -import { localBostonMunicipalAPI, deployedBostonMunicipalAPI } from '@/common'; +import { localBostonMunicipalAPI, deployedBostonMunicipalAPI, setGlobalCurrElection } from '@/common'; interface ElectionDateObject { @@ -12,7 +12,11 @@ interface ElectionDateObject { } } -export default function ElectionCheckbox() { +interface ElectionCheckboxProps { + onCheck: (electionName: string) => void; +} + +const ElectionCheckbox: React.FC = ({ onCheck }) => { const [electionDates, setElectionDates] = useState([]) const [isLoading, setIsLoading] = useState(true); const [sortedElectionDates, setSortedElectionDates] = useState([]) @@ -48,6 +52,7 @@ export default function ElectionCheckbox() { fetchElectionDates(); }, []) + useEffect(() => { if (electionDates.length > 0) { const sortedDates = electionDates.sort((a: ElectionDateObject, b: ElectionDateObject) => { @@ -58,9 +63,12 @@ export default function ElectionCheckbox() { }, [electionDates]); + const handleCheckboxChange = (electionName: string) => { - setSelectedElection(electionName); console.log(electionName); + setSelectedElection(electionName); + setGlobalCurrElection(electionName); + onCheck(electionName); }; @@ -95,4 +103,6 @@ export default function ElectionCheckbox() { )} ) -} \ No newline at end of file +}; + +export default ElectionCheckbox; \ No newline at end of file diff --git a/client/src/pages/ballotInfo/index.tsx b/client/src/pages/ballotInfo/index.tsx index 4507c1b7c..56654a61c 100644 --- a/client/src/pages/ballotInfo/index.tsx +++ b/client/src/pages/ballotInfo/index.tsx @@ -14,18 +14,19 @@ import CandidateData from "./whatsOnTheBallot/candidateData"; export default function BallotInfo() { const [isFormSubmitted, setIsFormSubmitted] = React.useState(false); + const [selectedElection, setSelectedElection] = React.useState(null); const handleFormSubmit = () => { setIsFormSubmitted(true); }; - const [checked, setChecked] = React.useState(true); - - const handleChange = (event: React.ChangeEvent) => { - setChecked(event.target.checked); + const handleCheck = (electionName: string) => { + setSelectedElection(electionName); }; + + return ( -
+
{/* Header */} @@ -43,7 +44,7 @@ export default function BallotInfo() { {/* Election checkbox card */} - + {/* What's on the Ballot dropdown */} @@ -52,16 +53,14 @@ export default function BallotInfo() {

Candidates

{/*Testing*/} - {isFormSubmitted && } - {!isFormSubmitted &&
Please fill out the address form above to see your ballot information
} - - {/* */} + {isFormSubmitted && selectedElection && } + {(!isFormSubmitted || !selectedElection) &&
Please fill out the address form above and select an election to see your ballot information
}

Ballot Initiatives

{/* NOTE: REPLACE BUTTON BELOW WITH DESCRIPTION FROM YAWU */} - {isFormSubmitted && } - {!isFormSubmitted &&
Please fill out the address form above to see your ballot information
} + {isFormSubmitted && selectedElection && } + {(!isFormSubmitted || !selectedElection) &&
Please fill out the address form above and select an election to see your ballot information
}
diff --git a/client/src/pages/ballotInfo/whatsOnTheBallot/candidateData.tsx b/client/src/pages/ballotInfo/whatsOnTheBallot/candidateData.tsx index 25d7c87b3..afa59d2e7 100644 --- a/client/src/pages/ballotInfo/whatsOnTheBallot/candidateData.tsx +++ b/client/src/pages/ballotInfo/whatsOnTheBallot/candidateData.tsx @@ -1,6 +1,6 @@ 'use client' import { useEffect, useState } from 'react'; -import { localCandidateAPI, deployedCandidateAPI, localCandidateRoleAPI, deployedCandidateRoleAPI, globalDistrictNum } from '@/common'; +import { localCandidateAPI, deployedCandidateAPI, localCandidateRoleAPI, deployedCandidateRoleAPI, globalDistrictNum, globalCurrElection } from '@/common'; import Accordion from '@mui/material/Accordion'; import AccordionSummary from '@mui/material/AccordionSummary'; import AccordionDetails from '@mui/material/AccordionDetails'; @@ -36,6 +36,7 @@ export default function CandidateData() { const [allCandidateData, setAllCandidateData] = useState([]) const [filteredCandidateData, setFilteredCandidateData] = useState<{ [key: string]: Candidate[] }>({}) const [districtNum, setDistrictNum] = useState(globalDistrictNum); + const [selectedElection, setSelectedElection] = useState(globalCurrElection); const [candidateRoleDate, setCandidateRoleData] = useState<{ [key: string]: string }>({}) @@ -59,17 +60,18 @@ export default function CandidateData() { } }; - // Fetch data only if districtNum is set - if (districtNum) { + // Fetch data only if districtNum and election are set + if (districtNum && selectedElection) { getData(); } - }, [districtNum]); + }, [districtNum, selectedElection]); // Set the district number to the global number which was set in DistrictForm useEffect(() => { setDistrictNum(globalDistrictNum); - }, [globalDistrictNum]); + setSelectedElection(globalCurrElection); + }, [globalDistrictNum, globalCurrElection]); useEffect(() => { @@ -80,7 +82,6 @@ export default function CandidateData() { const sortedData: { [key: string]: Candidate[] } = {}; const roleData: { [key: string]: string } = {}; - const election = "Primary Municipal Election"; // Get candidate role from strapi const getData = async () => { @@ -109,8 +110,7 @@ export default function CandidateData() { // Add candidates to hash table if (allCandidateData.length > 0 && districtNum) { allCandidateData.forEach((candidateDataObject: CandidateDataObject) => { - - if (candidateDataObject.attributes.District.trim() === districtNum && candidateDataObject.attributes.ElectionName === election) { + if (candidateDataObject.attributes.District.trim() === districtNum && candidateDataObject.attributes.ElectionName.trim() === selectedElection?.trim()) { const candidate: Candidate = { attributes: candidateDataObject.attributes }; @@ -126,7 +126,7 @@ export default function CandidateData() { } console.log(sortedData); - }, [allCandidateData, districtNum]) + }, [allCandidateData, districtNum, selectedElection]) useEffect(() => {