Skip to content

Commit

Permalink
as per @beausmith, improved naming to voter card
Browse files Browse the repository at this point in the history
  • Loading branch information
benadida committed May 16, 2019
1 parent 42ae664 commit cb0e35d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
19 changes: 10 additions & 9 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
} from './lib/gamepad'

import {
ActivationCardData,
ActivationData,
BallotStyle,
CardData,
Expand All @@ -27,6 +26,7 @@ import {
PartialUserSettings,
TextSizeSetting,
UserSettings,
VoterCardData,
VotesDict,
} from './config/types'

Expand Down Expand Up @@ -67,22 +67,23 @@ const initialState = {
votes: {},
}

let checkCardInterval = 0

export class App extends React.Component<RouteComponentProps, State> {
public state: State = initialState
public checkCard: number = 0

public processCardData = (cardData: CardData) => {
if (cardData.t === 'activation') {
if (cardData.t === 'voter') {
if (!this.state.election) {
return
}

const activationCardData = cardData as ActivationCardData
const voterCardData = cardData as VoterCardData
const ballotStyle = this.state.election.ballotStyles.find(
bs => activationCardData.bs === bs.id
bs => voterCardData.bs === bs.id
)
const precinct = this.state.election.precincts.find(
pr => pr.id === activationCardData.pr
pr => pr.id === voterCardData.pr
)

if (ballotStyle && precinct) {
Expand Down Expand Up @@ -123,7 +124,7 @@ export class App extends React.Component<RouteComponentProps, State> {
document.documentElement.setAttribute('data-useragent', navigator.userAgent)
this.setDocumentFontSize()

this.checkCard = window.setInterval(() => {
checkCardInterval = window.setInterval(() => {
fetch('/card/read')
.then(result => result.json())
.then(resultJSON => {
Expand All @@ -134,15 +135,15 @@ export class App extends React.Component<RouteComponentProps, State> {
})
.catch(() => {
// if it's an error, aggressively assume there's no backend and stop hammering
window.clearInterval(this.checkCard)
window.clearInterval(checkCardInterval)
})
}, 1000)
}

public componentWillUnount = /* istanbul ignore next - triggering keystrokes issue - https://github.com/votingworks/bmd/issues/62 */ () => {
Mousetrap.unbind(removeElectionShortcuts)
document.removeEventListener('keydown', handleGamepadKeyboardEvent)
window.clearInterval(this.checkCard)
window.clearInterval(checkCardInterval)
}

public getElection = (): OptionalElection => {
Expand Down
18 changes: 9 additions & 9 deletions src/AppCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { render } from 'react-testing-library'
import electionSample from './data/electionSample.json'

import Root, { App } from './App'
import { ActivationCardData, AdminCardData, Election } from './config/types'
import { AdminCardData, Election, VoterCardData } from './config/types'

const election = electionSample as Election

Expand All @@ -25,7 +25,7 @@ it(`App fetches the card data every 1 second`, () => {
JSON.stringify({
present: true,
shortValue: JSON.stringify({
t: 'activation',
t: 'voter',
pr: election.precincts[0].id,
bs: election.ballotStyles[0].id,
}),
Expand Down Expand Up @@ -68,25 +68,25 @@ it(`CardData processing processes card data properly`, () => {
// for now just for code coverage of the else, we don't do anything useful yet
app.processCardData(adminCardData)

const activationCardData: ActivationCardData = {
const voterCardData: VoterCardData = {
bs: election.ballotStyles[0].id,
pr: election.precincts[0].id,
t: 'activation',
t: 'voter',
}

app.processCardData(activationCardData)
app.processCardData(voterCardData)
expect(app.activateBallot).not.toHaveBeenCalled()

app.state.election = election
app.processCardData(activationCardData)
app.processCardData(voterCardData)

// also bad ballot style and precinct, for coverage.
const badActivationCardData: ActivationCardData = {
const badVoterCardData: VoterCardData = {
bs: 'foobar',
pr: 'barbaz',
t: 'activation',
t: 'voter',
}
app.processCardData(badActivationCardData)
app.processCardData(badVoterCardData)

expect(app.activateBallot).toBeCalled()
})
6 changes: 3 additions & 3 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ export interface BallotContextInterface {
}

// Smart Card Content
export type CardDataTypes = 'activation' | 'admin'
export type CardDataTypes = 'voter' | 'admin'
export interface CardData {
readonly t: CardDataTypes
}
export interface ActivationCardData extends CardData {
readonly t: 'activation'
export interface VoterCardData extends CardData {
readonly t: 'voter'
readonly bs: string
readonly pr: string
}
Expand Down

0 comments on commit cb0e35d

Please sign in to comment.