Skip to content

Commit

Permalink
refactor(all): link internal apps to libs
Browse files Browse the repository at this point in the history
This also updates `bas` to use types from `@votingworks/ballot-encoder`.
  • Loading branch information
eventualbuddha committed Jan 14, 2021
1 parent c7353f7 commit 736f3b9
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 162 deletions.
3 changes: 2 additions & 1 deletion apps/bas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"husky": {
"hooks": {
"pre-commit": "tsc && lint-staged"
"pre-commit": "tsc --build && lint-staged"
}
},
"lint-staged": {
Expand Down Expand Up @@ -71,6 +71,7 @@
"@types/history": "^4.7.8",
"@typescript-eslint/eslint-plugin": "^4.11.0",
"@typescript-eslint/parser": "^4.11.0",
"@votingworks/ballot-encoder": "workspace:*",
"eslint": "^7.17.0",
"eslint-config-prettier": "^6.15.0",
"eslint-import-resolver-node": "^0.3.4",
Expand Down
17 changes: 8 additions & 9 deletions apps/bas/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import React, { useState } from 'react'

import fetchJSON from './utils/fetchJSON'

import {
ButtonEvent,
BallotStyle,
CardData,
Optional,
OptionalElection,
OptionalVoterCardData,
VoterCardData,
BallotStyle,
CardAPI,
} from './config/types'
} from '@votingworks/ballot-encoder'

import fetchJSON from './utils/fetchJSON'

import { ButtonEvent, CardAPI } from './config/types'

import useStateAndLocalStorage from './hooks/useStateWithLocalStorage'

Expand Down Expand Up @@ -51,7 +50,7 @@ const App: React.FC = () => {
)
const [ballotStyleId, setBallotStyleId] = useState<string>('')
const [partyId, setPartyId] = useStateAndLocalStorage<string>('partyId')
const [voterCardData, setVoterCardData] = useState<OptionalVoterCardData>(
const [voterCardData, setVoterCardData] = useState<Optional<VoterCardData>>(
undefined
)

Expand Down
113 changes: 0 additions & 113 deletions apps/bas/src/config/types.ts
Original file line number Diff line number Diff line change
@@ -1,121 +1,8 @@
// Generic
export interface Dictionary<T> {
[key: string]: T | undefined
}

// Events
export type InputEvent = React.FormEvent<EventTarget>
export type ButtonEvent = React.MouseEvent<HTMLButtonElement>
export type ButtonEventFunction = (event: ButtonEvent) => void

// Candidates
export interface Candidate {
readonly id: string
readonly name: string
readonly partyId?: string
isWriteIn?: boolean
}
export type OptionalCandidate = Candidate | undefined

// Contests
export type ContestTypes = 'candidate' | 'yesno'
export interface Contest {
readonly id: string
readonly districtId: string
readonly partyId?: string
readonly section: string
readonly title: string
readonly type: ContestTypes
}
export interface CandidateContest extends Contest {
readonly type: 'candidate'
readonly seats: number
readonly candidates: Candidate[]
readonly allowWriteIns: boolean
}
export interface YesNoContest extends Contest {
readonly type: 'yesno'
readonly description: string
readonly shortTitle: string
}
export type Contests = (CandidateContest | YesNoContest)[]

// Election
export interface BMDConfig {
readonly requireActivation?: boolean
readonly showHelpPage?: boolean
readonly showSettingsPage?: boolean
}
export interface ElectionDefaults {
readonly bmdConfig: BMDConfig
}
export interface BallotStyle {
readonly id: string
readonly precincts: string[]
readonly districts: string[]
readonly partyId?: string
}
export interface Party {
readonly id: string
readonly name: string
readonly abbrev: string
}
export type Parties = Party[]
export interface Precinct {
readonly id: string
readonly name: string
}
export interface District {
readonly id: string
readonly name: string
}
export interface County {
readonly id: string
readonly name: string
}
export interface Election {
readonly ballotStyles: BallotStyle[]
readonly parties: Parties
readonly precincts: Precinct[]
readonly districts: District[]
readonly contests: Contests
readonly county: County
readonly date: string
readonly seal: string
readonly state: string
readonly title: string
readonly bmdConfig?: BMDConfig
}
export type OptionalElection = Election | undefined

export type SetElection = (value: OptionalElection) => void

export interface ActivationData {
ballotStyle: BallotStyle
precinct: Precinct
}

// Smart Card Content
export type CardDataTypes = 'voter' | 'pollworker' | 'admin'
export interface CardData {
readonly t: CardDataTypes
}
export interface VoterCardData extends CardData {
readonly t: 'voter'
readonly bs: string
readonly pr: string
readonly c: number
}
export type OptionalVoterCardData = VoterCardData | undefined
export interface PollworkerCardData extends CardData {
readonly t: 'pollworker'
readonly h: string
}
export interface AdminCardData extends CardData {
readonly t: 'admin'
readonly h: string
}

export interface CardAbsentAPI {
present: false
}
Expand Down
2 changes: 1 addition & 1 deletion apps/bas/src/screens/AdminScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import pluralize from 'pluralize'

import { BallotStyle, OptionalElection } from '../config/types'
import { BallotStyle, OptionalElection } from '@votingworks/ballot-encoder'

import { compareName } from '../utils/sort'
import Button from '../components/Button'
Expand Down
5 changes: 3 additions & 2 deletions apps/bas/src/screens/PrecinctBallotStylesScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react'
import styled from 'styled-components'
import { BallotStyle } from '@votingworks/ballot-encoder'

import { ButtonEventFunction, BallotStyle } from '../config/types'
import { ButtonEventFunction } from '../config/types'

import Button from '../components/Button'
import ButtonList from '../components/ButtonList'
Expand All @@ -22,7 +23,7 @@ interface Props {
isSinglePrecinctMode: boolean
lockScreen: () => void
partyId: string
precinctBallotStyles: BallotStyle[]
precinctBallotStyles: readonly BallotStyle[]
precinctName: string
programCard: ButtonEventFunction
showPrecincts: () => void
Expand Down
11 changes: 4 additions & 7 deletions apps/bas/src/screens/PrecinctsScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React from 'react'
import { Optional, Precinct, VoterCardData } from '@votingworks/ballot-encoder'

import {
ButtonEventFunction,
Precinct,
OptionalVoterCardData,
} from '../config/types'
import { ButtonEventFunction } from '../config/types'

import Button from '../components/Button'
import ButtonList from '../components/ButtonList'
Expand All @@ -20,9 +17,9 @@ interface Props {
cardPrecinctName: string
countyName: string
lockScreen: () => void
precincts: Precinct[]
precincts: readonly Precinct[]
updatePrecinct: ButtonEventFunction
voterCardData: OptionalVoterCardData
voterCardData: Optional<VoterCardData>
}

const PrecinctsScreen: React.FC<Props> = ({
Expand Down
2 changes: 1 addition & 1 deletion apps/bmd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"@types/react-modal": "^3.10.5",
"@types/react-router-dom": "^5.1.5",
"@types/styled-components": "^5.0.1",
"@votingworks/ballot-encoder": "^5.0.0",
"@votingworks/ballot-encoder": "workspace:*",
"@votingworks/qrcode.react": "^1.0.1",
"abortcontroller-polyfill": "^1.4.0",
"base64-js": "^1.3.1",
Expand Down
4 changes: 2 additions & 2 deletions apps/bsd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@types/react-dom": "^17.0.0",
"@types/react-modal": "^3.10.6",
"@types/styled-components": "^5.0.0",
"@votingworks/hmpb-interpreter": "^5.1.2",
"@votingworks/hmpb-interpreter": "workspace:*",
"fast-text-encoding": "^1.0.2",
"http-proxy-middleware": "^1.0.0",
"js-file-download": "^0.4.6",
Expand Down Expand Up @@ -86,7 +86,7 @@
"@types/yauzl": "^2.9.1",
"@typescript-eslint/eslint-plugin": "^4.11.0",
"@typescript-eslint/parser": "^4.11.0",
"@votingworks/ballot-encoder": "^4.0.0",
"@votingworks/ballot-encoder": "workspace:*",
"eslint": "^7.17.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-prettier": "^6.15.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/election-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"build": "react-scripts build",
"eject": "react-scripts eject",
"format": "prettier '**/*.+(css|graphql|json|less|md|mdx|sass|scss|yaml|yml)' --write",
"lint": "tsc --noEmit && eslint '*/**/*.{js,jsx,ts,tsx}' --quiet && pnpm stylelint:run",
"lint:fix": "tsc --noEmit && eslint '*/**/*.{js,jsx,ts,tsx}' --fix --quiet && pnpm stylelint:run:fix",
"lint": "tsc --build && eslint '*/**/*.{js,jsx,ts,tsx}' --quiet && pnpm stylelint:run",
"lint:fix": "tsc --build && eslint '*/**/*.{js,jsx,ts,tsx}' --fix --quiet && pnpm stylelint:run:fix",
"start": "react-scripts start",
"stylelint:run": "stylelint 'src/**/*.{js,jsx,ts,tsx}' && stylelint 'src/**/*.css' --config .stylelintrc-css.js",
"stylelint:run:fix": "stylelint 'src/**/*.{js,jsx,ts,tsx}' --fix && stylelint 'src/**/*.css' --config .stylelintrc-css.js --fix",
Expand Down Expand Up @@ -82,7 +82,7 @@
"@types/react-modal": "^3.10.5",
"@types/react-router-dom": "^5.1.5",
"@types/styled-components": "^5.1.0",
"@votingworks/ballot-encoder": "^4.0.0",
"@votingworks/ballot-encoder": "workspace:*",
"@votingworks/qrcode.react": "^1.0.2",
"array-unique": "^0.3.2",
"base64-js": "^1.3.1",
Expand Down
8 changes: 4 additions & 4 deletions apps/election-manager/src/lib/votecounting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const buildVoteFromCvr = ({

if (contest.type === 'yesno') {
// the CVR is encoded the same way
vote[contest.id] = cvr[contest.id] as YesNoVote
vote[contest.id] = (cvr[contest.id] as unknown) as YesNoVote
return
}

Expand Down Expand Up @@ -270,7 +270,7 @@ export function tallyVotesByContest({
const contestTallies: ContestTally[] = []
const { contests } = election
expandEitherNeitherContests(contests).forEach((contest) => {
let options: ContestOption[] = []
let options: readonly ContestOption[] = []
if (contest.type === 'yesno') {
options = [['yes'], ['no']]
}
Expand Down Expand Up @@ -467,13 +467,13 @@ export const getContestTallyMeta = ({
if (contest.type === 'candidate') {
return ((vote as unknown) as CandidateVote).length > contest.seats
}
return (vote as YesNoVote).length > 1
return ((vote as unknown) as YesNoVote).length > 1
})
const undervotes = contestVotes.filter((vote) => {
if (contest.type === 'candidate') {
return ((vote as unknown) as CandidateVote).length < contest.seats
}
return (vote as YesNoVote).length === 0
return ((vote as unknown) as YesNoVote).length === 0
})

dictionary[contest.id] = {
Expand Down
6 changes: 3 additions & 3 deletions apps/election-manager/src/utils/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*
* @throws when no element matches and no default value is provided
*/
function find<T>(array: T[], predicate: (element: T) => boolean): T
function find<T>(array: readonly T[], predicate: (element: T) => boolean): T
function find<T>(
array: T[],
array: readonly T[],
predicate: (element: T) => boolean,
defaultValue: T
): T
function find<T>(
array: T[],
array: readonly T[],
predicate: (element: T) => boolean,
defaultValue?: T
): T {
Expand Down
2 changes: 1 addition & 1 deletion libs/hmpb-interpreter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
]
},
"dependencies": {
"@votingworks/ballot-encoder": "^5.0.0",
"@votingworks/ballot-encoder": "workspace:*",
"canvas": "^2.6.1",
"chalk": "^4.1.0",
"debug": "^4.2.0",
Expand Down
Loading

0 comments on commit 736f3b9

Please sign in to comment.