Skip to content

Commit

Permalink
Display party list
Browse files Browse the repository at this point in the history
  • Loading branch information
dtinth committed Mar 24, 2019
1 parent b31cc0d commit 87f29c4
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/components/ElectionMapContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function getMapData(summaryState, filter) {
}
return [
...zones.map((zone, i) => {
// @todo #1 This logic is now available in getSeatDisplayModel. Refactor this component to use it.
const winningCandidate = (summary.zoneWinningCandidateMap[
zone.provinceId
] || {})[zone.no]
Expand Down
104 changes: 101 additions & 3 deletions src/components/PerPartyMapContainer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,103 @@
import React from "react"
import ElectionMapContainer from "./ElectionMapContainer"
import React, { useCallback, useContext, useMemo, useState } from "react"
import {
checkFilter,
filters,
zones,
zonePath,
getZoneByProvinceIdAndZoneNo,
} from "../models/information"
import { useSummaryData } from "../models/LiveDataSubscription"
import {
isZoneFinished,
shouldDisplayZoneData,
nationwidePartyStatsFromSummaryJSON,
} from "../models/PartyStats"
import ElectionMap, { electionMapLoadingData } from "./ElectionMap"
import ElectionMapTooltip from "./ElectionMapTooltip"
import ZoneMark from "./ZoneMark"
import { ZoneFilterContext } from "./ZoneFilterPanel"
import { navigate } from "gatsby"
import { trackEvent } from "../util/analytics"
import { media, WIDE_NAV_MIN_WIDTH } from "../styles"
import { getSeatDisplayModel } from "../models/ConstituencySeat"

/**
* @param {import('../models/LiveDataSubscription').DataState<ElectionDataSource.SummaryJSON>} summaryState
*/
function getMapData(summaryState, partyId) {
if (!summaryState.completed) {
return electionMapLoadingData
} else {
/** @type {ElectionDataSource.SummaryJSON} */
const summary = summaryState.data
const row = nationwidePartyStatsFromSummaryJSON(summaryState.data).find(
row => row.party.id === +partyId
)
if (!row) return electionMapLoadingData
const partylist = []
for (let i = 0; i < row.partyListSeats; i++) {
partylist.push({
id: `pl-${partylist.length + 1}`,
partyId: row.party.id,
complete: true,
show: true,
})
}
while (partylist.length < 150) {
partylist.push({
id: `pl-${partylist.length + 1}`,
partyId: "nope",
complete: true,
show: false,
})
}
return [
...zones.map((zone, i) => {
const { candidate, zoneStats } = getSeatDisplayModel(summary, zone)
const onMap = candidate && candidate.partyId === partyId
return {
id: `${zone.provinceId}-${zone.no}`,
partyId: onMap ? candidate.partyId : "nope",
complete: onMap && isZoneFinished(zoneStats),
show: true,
}
}),
...partylist,
]
}
}

export default function PerPartyMapContainer({ partyId }) {
return <ElectionMapContainer />
const summaryState = useSummaryData()
const mapData = useMemo(
() => ({ zones: getMapData(summaryState, partyId) }),
[summaryState, partyId]
)

const onInit = useCallback(map => {}, [])
const onZoneMouseenter = useCallback((zone, mouseEvent) => {}, [])
const onZoneMousemove = useCallback((zone, mouseEvent) => {}, [])
const onZoneMouseleave = useCallback((zone, mouseEvent) => {}, [])
const onZoneClick = useCallback(zone => {}, [])

return (
<div
css={{
margin: "0 -16px",
pointerEvents: "none",
[media(WIDE_NAV_MIN_WIDTH)]: {
marginLeft: "0 0",
},
}}
>
<ElectionMap
data={mapData}
onInit={onInit}
onZoneMouseenter={onZoneMouseenter}
onZoneMousemove={onZoneMousemove}
onZoneMouseleave={onZoneMouseleave}
onZoneClick={onZoneClick}
/>
</div>
)
}
18 changes: 18 additions & 0 deletions src/models/ConstituencySeat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { shouldDisplayZoneData } from "./PartyStats"

/**
* @param {ElectionDataSource.SummaryJSON} summary
* @param {IZone} zone
*/
export function getSeatDisplayModel(summary, zone) {
const winningCandidate = (summary.zoneWinningCandidateMap[zone.provinceId] ||
{})[zone.no]
const zoneStats = (summary.zoneStatsMap[zone.provinceId] || {})[zone.no]
const candidate =
shouldDisplayZoneData(zoneStats) &&
winningCandidate &&
winningCandidate.score > zoneStats.noVotes
? winningCandidate
: null
return { candidate, zoneStats }
}

1 comment on commit 87f29c4

@0pdd
Copy link

@0pdd 0pdd commented on 87f29c4 Mar 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1-aac0edaf discovered in src/components/ElectionMapContainer.js and submitted as #177. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.