Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race & Ethnicities: Sorting Races & Quick Fix #190

Merged
merged 2 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 77 additions & 72 deletions publisher/src/components/MetricConfiguration/RaceEthnicitiesForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
raceEthnicityGridStates,
RaceSelection,
RadioButtonGroupWrapper,
sortRaces,
SpecifyEthnicityWrapper,
Subheader,
} from ".";
Expand Down Expand Up @@ -214,83 +215,87 @@ export const RaceEthnicitiesForm = observer(() => {
)}

{/* Races (Enable/Disable) */}
{Object.entries(ethnicitiesByRace).map(([race, ethnicities]) => {
const raceEnabled = Boolean(
Object.values(ethnicities).find((ethnicity) => ethnicity.enabled)
);
{Object.entries(ethnicitiesByRace)
.sort(sortRaces)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

L219 is the only change in this file.

.map(([race, ethnicities]) => {
const raceEnabled = Boolean(
Object.values(ethnicities).find(
(ethnicity) => ethnicity.enabled
)
);

return (
<Race key={race}>
<RaceDisplayName>{race}</RaceDisplayName>
<RaceSelection>
<RadioButtonGroupWrapper>
<BinaryRadioButton
type="radio"
id={`${race}-no`}
name={`${race}`}
label="No"
value="no"
checked={!raceEnabled}
lastOptionBlue
buttonSize="small"
onChange={() => {
if (!systemSearchParam || !metricSearchParam) return;
/**
* When Unknown Race is disabled in NO_ETHNICITY_HISPANIC_AS_RACE state, we automatically switch
* to the NO_ETHNICITY_HISPANIC_NOT_SPECIFIED state because the Unknown Race (Hispanic/Latino Ethnicity)
* dimension is the only dimension an agency can specify their numbers for Hispanic/Latino as a Race (while
* in the NO_ETHNICITY_HISPANIC_AS_RACE state).
*/
if (
race === "Unknown" &&
currentState === "NO_ETHNICITY_HISPANIC_AS_RACE"
) {
updateAllRaceEthnicitiesToDefaultState(
"NO_ETHNICITY_HISPANIC_NOT_SPECIFIED",
return (
<Race key={race}>
<RaceDisplayName>{race}</RaceDisplayName>
<RaceSelection>
<RadioButtonGroupWrapper>
<BinaryRadioButton
type="radio"
id={`${race}-no`}
name={`${race}`}
label="No"
value="no"
checked={!raceEnabled}
lastOptionBlue
buttonSize="small"
onChange={() => {
if (!systemSearchParam || !metricSearchParam) return;
/**
* When Unknown Race is disabled in NO_ETHNICITY_HISPANIC_AS_RACE state, we automatically switch
* to the NO_ETHNICITY_HISPANIC_NOT_SPECIFIED state because the Unknown Race (Hispanic/Latino Ethnicity)
* dimension is the only dimension an agency can specify their numbers for Hispanic/Latino as a Race (while
* in the NO_ETHNICITY_HISPANIC_AS_RACE state).
*/
if (
race === "Unknown" &&
currentState === "NO_ETHNICITY_HISPANIC_AS_RACE"
) {
updateAllRaceEthnicitiesToDefaultState(
"NO_ETHNICITY_HISPANIC_NOT_SPECIFIED",
raceEthnicityGridStates,
systemSearchParam,
metricSearchParam
);
}

const updatedDimensions = updateRaceDimensions(
race,
false,
currentState,
raceEthnicityGridStates,
systemSearchParam,
metricSearchParam
);
}

const updatedDimensions = updateRaceDimensions(
race,
false,
currentState,
raceEthnicityGridStates,
systemSearchParam,
metricSearchParam
);
debouncedSave(updatedDimensions);
}}
/>
<BinaryRadioButton
type="radio"
id={`${race}-yes`}
name={`${race}`}
label="Yes"
value="yes"
checked={raceEnabled}
lastOptionBlue
buttonSize="small"
onChange={() => {
if (!systemSearchParam || !metricSearchParam) return;
const updatedDimensions = updateRaceDimensions(
race,
true,
currentState,
raceEthnicityGridStates,
systemSearchParam,
metricSearchParam
);
debouncedSave(updatedDimensions);
}}
/>
</RadioButtonGroupWrapper>
</RaceSelection>
</Race>
);
})}
debouncedSave(updatedDimensions);
}}
/>
<BinaryRadioButton
type="radio"
id={`${race}-yes`}
name={`${race}`}
label="Yes"
value="yes"
checked={raceEnabled}
lastOptionBlue
buttonSize="small"
onChange={() => {
if (!systemSearchParam || !metricSearchParam) return;
const updatedDimensions = updateRaceDimensions(
race,
true,
currentState,
raceEthnicityGridStates,
systemSearchParam,
metricSearchParam
);
debouncedSave(updatedDimensions);
}}
/>
</RadioButtonGroupWrapper>
</RaceSelection>
</Race>
);
})}
</RaceContainer>
</RaceEthnicitiesDisplay>
</RaceEthnicitiesContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
RaceEthnicitiesBreakdownContainer,
RaceEthnicitiesRow,
RaceEthnicitiesTable,
sortRaces,
} from ".";

export const RaceEthnicitiesGrid: React.FC<{
Expand Down Expand Up @@ -78,19 +79,21 @@ export const RaceEthnicitiesGrid: React.FC<{
</GridHeaderContainer>

<RaceEthnicitiesTable>
{Object.entries(ethnicitiesByRace).map(([race, ethnicities]) => (
<RaceEthnicitiesRow key={race}>
<RaceCell>{race}</RaceCell>
<EthnicitiesRow>
{Object.values(ethnicities).map((ethnicity) => (
<EthnicityCell
key={ethnicity.key}
enabled={ethnicity.enabled}
/>
))}
</EthnicitiesRow>
</RaceEthnicitiesRow>
))}
{Object.entries(ethnicitiesByRace)
.sort(sortRaces)
Copy link
Contributor Author

@mxosman mxosman Nov 30, 2022

Choose a reason for hiding this comment

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

L83 is the only change in this file.

.map(([race, ethnicities]) => (
<RaceEthnicitiesRow key={race}>
<RaceCell>{race}</RaceCell>
<EthnicitiesRow>
{Object.values(ethnicities).map((ethnicity) => (
<EthnicityCell
key={ethnicity.key}
enabled={ethnicity.enabled}
/>
))}
</EthnicitiesRow>
</RaceEthnicitiesRow>
))}
</RaceEthnicitiesTable>
</RaceEthnicitiesBreakdownContainer>
);
Expand Down
1 change: 1 addition & 0 deletions publisher/src/components/MetricConfiguration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export * from "./RaceEthnicitiesForm";
export * from "./RaceEthnicitiesGrid";
export * from "./RaceEthnicitiesGridStates";
export * from "./types";
export * from "./utils";
45 changes: 45 additions & 0 deletions publisher/src/components/MetricConfiguration/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Recidiviz - a data platform for criminal justice reform
// Copyright (C) 2022 Recidiviz, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================

import { UpdatedDimension } from ".";

/**
* Sort Races from an `ethnicitiesByRace` object entries array in the following order:
* 'American Indian / Alaskan Native', 'Asian', 'Black', 'Native Hawaiian / Pacific Islander', 'White', 'More than one race', 'Other', 'Unknown'
*/
export const sortRaces = (
[a, _]: [
string,
{
[ethnicity: string]: UpdatedDimension;
}
],
[b, __]: [
string,
{
[ethnicity: string]: UpdatedDimension;
}
]
) => {
if (b === "More than one race" && a === "Other") {
return 0;
}
if (b === "More than one race" && a !== "Unknown" && a !== "Other") {
return -1;
}
return 1;
};
3 changes: 3 additions & 0 deletions publisher/src/components/Settings/Settings.styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export const MetricsListItem = styled.div<{ activeSection?: boolean }>`
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
border-bottom: 2px solid
${({ activeSection }) =>
activeSection ? palette.solid.blue : `transparent`};

&:hover {
cursor: pointer;
Expand Down