diff --git a/src/components/arrows.tsx b/src/components/arrows.tsx index dbaa6f9a..8fbaf949 100644 --- a/src/components/arrows.tsx +++ b/src/components/arrows.tsx @@ -1,9 +1,9 @@ -import { animated } from "react-spring" +import { SpringValue, animated } from "react-spring" type Props = { className: string style: { - transform: string + transform: SpringValue | string } } diff --git a/src/components/inscribete/SpecialVoters/SpecialVoterCards.tsx b/src/components/inscribete/SpecialVoters/SpecialVoterCards.tsx index 5fb6d70b..4120446a 100644 --- a/src/components/inscribete/SpecialVoters/SpecialVoterCards.tsx +++ b/src/components/inscribete/SpecialVoters/SpecialVoterCards.tsx @@ -69,7 +69,7 @@ const SpecialVoterMachine = createMachine< export default function SpecialVoterCards() { const { t } = useTranslation() - const ref = useRef() + const ref = useRef(null!) const [state, send] = useMachine(SpecialVoterMachine) useEffect(() => { diff --git a/src/components/inscribete/SpecialVoters/SpecialVoterReasons.tsx b/src/components/inscribete/SpecialVoters/SpecialVoterReasons.tsx index 2e033533..706b4a02 100644 --- a/src/components/inscribete/SpecialVoters/SpecialVoterReasons.tsx +++ b/src/components/inscribete/SpecialVoters/SpecialVoterReasons.tsx @@ -23,7 +23,7 @@ export function Reason({ summary, details }: ReasonProps) { const [ref, bounds] = useMeasure({ polyfill: ResizeObserver }) const props = useSpring({ height: isOpen ? bounds.height || "auto" : 0, - visibility: isOpen ? "visible" : "hidden", + visibility: isOpen ? ("visible" as const) : ("hidden" as const), opacity: isOpen ? 1 : 0, transform: isOpen ? "rotate(180deg)" : "rotate(0deg)", }) diff --git a/src/components/inscribete/SpecialVoters/TabContent.tsx b/src/components/inscribete/SpecialVoters/TabContent.tsx index 3180d36e..31692115 100644 --- a/src/components/inscribete/SpecialVoters/TabContent.tsx +++ b/src/components/inscribete/SpecialVoters/TabContent.tsx @@ -44,9 +44,11 @@ export default function TabContent(voter: Props) { /> ))} - - {t(voter.exceptions)} - + {voter.exceptions && ( + + {t(voter.exceptions)} + + )} {voter.documents.length > 1 ? ( ) diff --git a/src/components/inscribete/VoterCard.tsx b/src/components/inscribete/VoterCard.tsx index 9fd3b68d..8a2354af 100644 --- a/src/components/inscribete/VoterCard.tsx +++ b/src/components/inscribete/VoterCard.tsx @@ -19,7 +19,7 @@ export function VoterCard(voter: Voter) { const [ref, bounds] = useMeasure({ polyfill: ResizeObserver }) const props = useSpring({ height: isOpen ? bounds.height || "auto" : 0, - visibility: isOpen ? "visible" : "hidden", + visibility: isOpen ? ("visible" as const) : ("hidden" as const), opacity: isOpen ? 1 : 0, transform: isOpen ? "rotate(180deg)" : "rotate(0deg)", }) diff --git a/src/components/inscribete/VoterCenters/CenterInfo.tsx b/src/components/inscribete/VoterCenters/CenterInfo.tsx index 764b4d6e..aa5fc8f4 100644 --- a/src/components/inscribete/VoterCenters/CenterInfo.tsx +++ b/src/components/inscribete/VoterCenters/CenterInfo.tsx @@ -60,7 +60,7 @@ const TownInfo = ({ town }: CenterInfoProps) => {
diff --git a/src/components/section.tsx b/src/components/section.tsx index abe95bdd..e144cddb 100644 --- a/src/components/section.tsx +++ b/src/components/section.tsx @@ -21,7 +21,7 @@ function Section(props: SectionProps) { const [ref, bounds] = useMeasure({ polyfill: ResizeObserver }) const springProps = useSpring({ height: isOpen ? bounds.height || "auto" : 0, - visibility: isOpen ? "visible" : "hidden", + visibility: isOpen ? ("visible" as const) : ("hidden" as const), opacity: isOpen ? 1 : 0, transform: isOpen ? "rotate(180deg)" : "rotate(0deg)", }) diff --git a/src/packages/practica/machines/practice.ts b/src/packages/practica/machines/practice.ts index da5e8280..5f5c264d 100644 --- a/src/packages/practica/machines/practice.ts +++ b/src/packages/practica/machines/practice.ts @@ -3,12 +3,9 @@ import { createMachine, assign } from "xstate" import { BallotService } from "../services/ballot-service" import { MAX_PRECINT_LENGTH } from "../services/constants" -import { - BallotConfigs, - BallotsResponse, - PracticeContext, -} from "../services/types" +import { BallotsResponse, PracticeContext } from "../services/types" import { BallotType } from "../../../ballot-validator/types" +import { BallotConfigs } from "../services/ballot-configs" type ControlEvent = | { type: "start"; userInput: string } diff --git a/src/packages/practica/resource.ts b/src/packages/practica/resource.ts index b0f66f88..ae3a32bd 100644 --- a/src/packages/practica/resource.ts +++ b/src/packages/practica/resource.ts @@ -26,7 +26,7 @@ export const BallotResource = { ballotPath: string votes: string }) { - return api.post("/createBallotTask", ballot) + return api.post<{ uuid: string }>("/createBallotTask", ballot) }, getBallotPdf(params: string) { diff --git a/src/packages/practica/services/ballot-configs/index.ts b/src/packages/practica/services/ballot-configs/index.ts index c6b5b56c..47b80d6b 100644 --- a/src/packages/practica/services/ballot-configs/index.ts +++ b/src/packages/practica/services/ballot-configs/index.ts @@ -367,3 +367,9 @@ export type BallotConfigs = | StateBallotConfig | MunicipalBallotConfig | LegislativeBallotConfig + +export type BallotConfigByType = { + estatal: StateBallotConfig + municipal: MunicipalBallotConfig + legislativa: LegislativeBallotConfig +} diff --git a/src/packages/practica/services/ballot-service.ts b/src/packages/practica/services/ballot-service.ts index 76bccd22..395ba87d 100644 --- a/src/packages/practica/services/ballot-service.ts +++ b/src/packages/practica/services/ballot-service.ts @@ -8,13 +8,14 @@ import { } from "../strategies" import { BallotResource } from "../resource" import { + BallotConfigByType, BallotConfigs, LegislativeBallotConfig, MunicipalBallotConfig, StateBallotConfig, } from "./ballot-configs" import { ElectiveField } from "./ballot-configs/base" -import { OcrResult, PracticeContext } from "./types" +import { Ballots, OcrResult, PracticeContext } from "./types" import { getExplicitlySelectedVotes, Vote } from "./vote-service" import BallotFinder, { FindByType } from "./ballot-finder-service" @@ -40,7 +41,7 @@ function findPartyVotes(votes: Vote[]) { return votes.filter((vote) => vote.position.row === PARTY_ROW) } -function getBallot(ballots, ballotType: BallotType): BallotConfigs { +function getBallot(ballots: Ballots, ballotType: BallotType): BallotConfigs { if (ballotType === BallotType.state) { return ballots.estatal } else if (ballotType === BallotType.municipality) { @@ -50,6 +51,27 @@ function getBallot(ballots, ballotType: BallotType): BallotConfigs { return ballots.legislativa } +const fetchBallotConfig = async ( + type: keyof BallotConfigByType, + path: string +) => { + const ballotJson: OcrResult[][] = await BallotResource.getBallot(path) + + if (type === "estatal") { + return { + [type]: new StateBallotConfig(ballotJson, path), + } + } else if (type === "municipal") { + return { + [type]: new MunicipalBallotConfig(ballotJson, path), + } + } + + return { + [type]: new LegislativeBallotConfig(ballotJson, path), + } +} + const BallotService = { async fetchBallots( _: PracticeContext, @@ -58,52 +80,18 @@ const BallotService = { const ballotPaths = await BallotFinder(userInput, findBy) // Prefetch ballot data - const ballotRequests: Promise<{ - estatal: StateBallotConfig - municipal: MunicipalBallotConfig - legislativa: LegislativeBallotConfig - }> = Object.entries(ballotPaths).map(async ([key, value]) => { - try { - const ballotJson: OcrResult[][] = await BallotResource.getBallot(value) - - if (key === "estatal") { - return { - [key]: new StateBallotConfig(ballotJson, ballotPaths.estatal), - } - } else if (key === "municipal") { - return { - [key]: new MunicipalBallotConfig(ballotJson, ballotPaths.municipal), - } - } - - return { - [key]: new LegislativeBallotConfig( - ballotJson, - ballotPaths.legislativa - ), - } - } catch (err) { - console.log(err) - } - }) - + const ballotRequests: Promise>[] = [ + fetchBallotConfig("estatal", ballotPaths.estatal), + fetchBallotConfig("municipal", ballotPaths.municipal), + fetchBallotConfig("legislativa", ballotPaths.legislativa), + ] const allBallotsJson = await Promise.all(ballotRequests) - const initialValue: { - estatal?: StateBallotConfig - municipal?: MunicipalBallotConfig - legislativa?: LegislativeBallotConfig - } = { - estatal: undefined, - municipal: undefined, - legislativa: undefined, - } - const ballots = allBallotsJson.reduce((prev, curr) => { return { ...prev, ...curr, } - }, initialValue) + }, {} as BallotConfigByType) return { ballots, @@ -115,7 +103,7 @@ const BallotService = { context: PracticeContext, { candidate, position, ballotType }: VoteEvent ) { - const ballots = context.ballots + const ballots = context.ballots as Ballots const prevVotes = context.votes const existingVoteAtPosition = prevVotes.find( (vote) => diff --git a/src/packages/practica/services/functions/getBallotDetails.ts b/src/packages/practica/services/functions/getBallotDetails.ts index ba356efb..70022f9a 100644 --- a/src/packages/practica/services/functions/getBallotDetails.ts +++ b/src/packages/practica/services/functions/getBallotDetails.ts @@ -1,5 +1,5 @@ import { s3Client } from "../base" -import { BallotResponse } from "../types" +import { BallotDetails } from "../types" export default (path: string) => - s3Client.get(`${path}/data.json`) + s3Client.get(`${path}/data.json`) diff --git a/src/packages/practica/services/types.ts b/src/packages/practica/services/types.ts index 19fd3ce8..e22af72f 100644 --- a/src/packages/practica/services/types.ts +++ b/src/packages/practica/services/types.ts @@ -28,7 +28,7 @@ export type VoterDetailsResponse = { } } -export type BallotResponse = { ocrResult: string; logoImg?: string }[][] +export type BallotDetails = { ocrResult: string; logoImg?: string }[][] export type OcrResult = { ocrResult: string @@ -78,8 +78,8 @@ export type VoterInfo = { unidad: string } -export type BallotsResponse = { +export interface BallotsResponse { estatal: string municipal: string - legislative: string + legislativa: string } diff --git a/src/services/api.ts b/src/services/api.ts index 66a0758d..1239c1b7 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -67,8 +67,8 @@ class ApiService { return res } - async post(endpoint: string, params: any, options?: RequestOptions) { - const res = await this.adapter.post(endpoint, params, options) + async post(endpoint: string, params: any, options?: RequestOptions) { + const res = await this.adapter.post(endpoint, params, options) return res }