-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
87f29c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
1-aac0edaf
discovered insrc/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.