Skip to content

Commit

Permalink
Ensure proper sorting of recidivism data (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
macfarlandian committed Nov 9, 2020
1 parent eaf0884 commit fa3efe0
Show file tree
Hide file tree
Showing 4 changed files with 741 additions and 726 deletions.
23 changes: 16 additions & 7 deletions public-dashboard-client/src/page-prison/PagePrison.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// =============================================================================

import React, { useMemo, useState } from "react";
import { ascending } from "d3-array";
import DetailPage from "../detail-page";
import { PATHS, ALL_PAGES, SECTION_TITLES, DIMENSION_KEYS } from "../constants";
import {
Expand Down Expand Up @@ -46,7 +47,7 @@ const DESCRIPTION = (
);

function getCohortOptions(data) {
const cohortsFromData = new Set(data.map((d) => d.release_cohort));
const cohortsFromData = new Set(data.map((d) => d.releaseCohort));
return [...cohortsFromData]
.map((cohort) => ({
id: cohort,
Expand All @@ -59,13 +60,25 @@ export default function PagePrison() {
const { apiData, isLoading } = useChartData("us_nd/prison");

// lifted state for the recidivism sections
const cohortOptions = useMemo(
const recidivismRates = useMemo(
() =>
isLoading
? []
: getCohortOptions(apiData.recidivism_rates_by_cohort_by_year),
: apiData.recidivism_rates_by_cohort_by_year
.map(typecastRecidivismData)
.sort((a, b) => {
// hierarchical sort: cohort, then followup
return (
ascending(a.releaseCohort, b.releaseCohort) ||
ascending(a.followupYears, b.followupYears)
);
}),
[apiData.recidivism_rates_by_cohort_by_year, isLoading]
);
const cohortOptions = useMemo(
() => (isLoading ? [] : getCohortOptions(recidivismRates)),
[isLoading, recidivismRates]
);
const [selectedCohorts, setSelectedCohorts] = useState([]);
const [highlightedCohort, setHighlightedCohort] = useState();
const [recidivismDimension, setRecidivismDimension] = useState(
Expand All @@ -92,10 +105,6 @@ export default function PagePrison() {
setRecidivismDimension(DIMENSION_KEYS.total);
}

const recidivismRates = apiData.recidivism_rates_by_cohort_by_year.map(
typecastRecidivismData
);

const SECTIONS = [
{
title: SECTION_TITLES[PATHS.prison].population,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
TOTAL_KEY,
} from "../constants";
import { THEME } from "../theme";
import { recordIsTotalByDimension } from "../utils";
import { demographicsAscending, recordIsTotalByDimension } from "../utils";

const Wrapper = styled.div``;

Expand All @@ -38,7 +38,13 @@ const Wrapper = styled.div``;
function prepareData({ recidivismRates, followupYears, dimension }) {
const dataForFollowupYears = recidivismRates
.filter((record) => record.followupYears === followupYears)
.filter(recordIsTotalByDimension(dimension));
.filter(recordIsTotalByDimension(dimension))
.sort((a, b) =>
demographicsAscending(
a[DIMENSION_DATA_KEYS[dimension]],
b[DIMENSION_DATA_KEYS[dimension]]
)
);

return Array.from(
group(
Expand Down
Loading

0 comments on commit fa3efe0

Please sign in to comment.