diff --git a/src/pages/index.js b/src/pages/index.js index cf4257e..4829422 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -5,7 +5,10 @@ import Head from 'next/head'; import { useRouter } from 'next/router'; import { getFilterPayload, getContestResults } from 'src/data'; -import { getUniversalQueryParams, hasFiltersApplied } from 'src/parameters'; +import { + getUniversalQueryParams, + queryHasFiltersApplied, +} from 'src/parameters'; import { computeTotalVotes } from 'src/utils'; import FilterControls from 'src/components/FilterControls'; import Contest from 'src/components/Contest'; @@ -17,7 +20,7 @@ export const getServerSideProps = async ({ query }) => { const initialUnfilteredContestResults = await getContestResults( getUniversalQueryParams(query), ); - const initialFilteredContestResults = hasFiltersApplied(query) + const initialFilteredContestResults = queryHasFiltersApplied(query) ? await getContestResults(query) : null; const initialFilterPayload = await getFilterPayload(); @@ -61,7 +64,7 @@ const useFetchContestResults = ( ) => { const queryString = new URLSearchParams(query).toString(); const key = - !isFilteredQuery || hasFiltersApplied(query) + !isFilteredQuery || queryHasFiltersApplied(query) ? `/api/contest_results?${queryString}` : null; return useSWR(key, fetcher, { @@ -108,9 +111,21 @@ const groupContests = (contestResults) => { }; const filterContests = (contestResults, candidateFilter) => { + if (!candidateFilter) { + return contestResults; + } + + const contestForCandidateFilter = contestResults.find( + (c) => c.candidates.findIndex((cand) => cand.id === candidateFilter) !== -1, + ); + // Include this contest, since seeing what other votes people cast in the + // same contest can be interesting + if (contestForCandidateFilter.numVotes > 1) { + return contestResults; + } + return contestResults.filter( (contest) => - !candidateFilter || contest.candidates.findIndex( (candidate) => candidate.id === candidateFilter, ) === -1, @@ -135,6 +150,8 @@ function HomePage({ router.query?.countingGroup || null, ); + const hasFiltersApplied = candidateFilter || countingGroupFilter; + const [selectedElection, setSelectedElection] = useState( router.query?.election || initialFilterPayload.elections[0].id, ); @@ -159,11 +176,7 @@ function HomePage({ let groupedContests = []; let totalVotesForFilteredCandidate = 0; - if ( - hasFiltersApplied(router.query) && - filteredContestResults && - unfilteredContestResults - ) { + if (hasFiltersApplied && filteredContestResults && unfilteredContestResults) { groupedContests = groupContests( augmentResultsWithPercentChanges( filterContests(filteredContestResults, candidateFilter), @@ -178,10 +191,8 @@ function HomePage({ ), ).distinctVotes : 0; - } else if (!hasFiltersApplied(router.query) && unfilteredContestResults) { - groupedContests = groupContests( - filterContests(unfilteredContestResults, null), - ); + } else if (!hasFiltersApplied && unfilteredContestResults) { + groupedContests = groupContests(unfilteredContestResults, null); } const updateUrl = ( @@ -267,7 +278,7 @@ function HomePage({
diff --git a/src/parameters.js b/src/parameters.js index 7387648..5d72312 100644 --- a/src/parameters.js +++ b/src/parameters.js @@ -2,6 +2,6 @@ export const getUniversalQueryParams = (query) => { return query.election ? { election: query.election } : {}; }; -export const hasFiltersApplied = (query) => { +export const queryHasFiltersApplied = (query) => { return !!query.candidate || !!query.countingGroup; }; diff --git a/todo.txt b/todo.txt index cfea43e..d597b05 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,5 @@ functionality: - allow filtering by votes across multiple races (or single race for races with numVotes > 1). or choose multiple candidates in 1 race and compare how their voters voted in other races -- only hide contest with filtered candidate if numVotes for contest is 1! otherwise results are interesting - filter by precinct (or district?), ballot type (could be interesting for NPP vs registered dems in dem primary) ui: