Skip to content

Commit

Permalink
change contest filtering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benhiller committed Oct 25, 2020
1 parent 45f3af6 commit 4a4ee7b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
39 changes: 25 additions & 14 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
);
Expand All @@ -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),
Expand All @@ -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 = (
Expand Down Expand Up @@ -267,7 +278,7 @@ function HomePage({
<div key={contest.id} className={classes.contest}>
<Contest
contest={contest}
hasFiltersApplied={!!filteredContestResults}
hasFiltersApplied={hasFiltersApplied}
totalVotesForFilteredCandidate={totalVotesForFilteredCandidate}
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
1 change: 0 additions & 1 deletion todo.txt
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 4a4ee7b

Please sign in to comment.