Skip to content

Commit

Permalink
Update other textbox logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nasaownsky committed Dec 10, 2024
1 parent 3094f81 commit 15664f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,3 @@ export const ContextLabel = styled.div`
color: ${palette.highlight.grey8};
margin-bottom: 16px;
`;

export const InputWrapper = styled.div`
margin-bottom: 16px;
textarea {
height: 100px;
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
CheckboxOption,
CheckboxOptions,
} from "@justice-counts/common/components/CheckboxOptions";
import { NewInput } from "@justice-counts/common/components/Input";
import {
RadioButton,
RadioButtonsWrapper,
Expand Down Expand Up @@ -119,18 +118,46 @@ function RaceEthnicitiesModalForm({
}
: undefined;

const currentOtherDescription =
Object.values(
Object.entries(ethnicitiesByRace).find(
([race]) => race === "Other"
)?.[1] || {}
).find((ethnicity) => {
if (!canSpecifyEthnicity && !specifiesHispanicAsRace)
return ethnicity.key === "Other / Unknown Ethnicity";

if (!canSpecifyEthnicity && specifiesHispanicAsRace)
return ethnicity.key === "Other / Not Hispanic or Latino";

return ethnicity.key === "Other / Hispanic or Latino";
})?.other_description || "";

const [otherDescription, setOtherDescription] = useState(
currentOtherDescription
);

const raceEthnicityOptions: CheckboxOption[] = [
...(hispanicOrLatinoOption ? [hispanicOrLatinoOption] : []),
...Object.entries(racesStatusObject).map(([race, enabled]) => {
const disabledUnknownRace =
race === "Unknown" && specifiesHispanicAsRace && !canSpecifyEthnicity;

const otherDescriptionParams = {
isEnabled: race === "Other" && Boolean(enabled),
placeholder:
"Please describe additional definition/clarification of the 'Other' selection.",
value: currentOtherDescription,
onChange: (value: string) => setOtherDescription(value),
};

return {
key: race,
label: race,
checked: Boolean(enabled),
disabled: disabledUnknownRace,
icon: disabledUnknownRace ? <InfoIcon id="unknown-race" /> : undefined,
otherDescription: otherDescriptionParams,
};
}),
];
Expand All @@ -145,9 +172,6 @@ function RaceEthnicitiesModalForm({
return "NO_ETHNICITY_HISPANIC_NOT_SPECIFIED";
};

const [isOtherChecked, setOtherChecked] = useState(false);
const [otherDescription, setOtherDescription] = useState("");

const handleUpdateRacesDimensions = () => {
if (!systemSearchParam || !metricSearchParam) return;
const currentState = determineCurrentState();
Expand Down Expand Up @@ -212,25 +236,9 @@ function RaceEthnicitiesModalForm({
capture for race?
</Styled.ToggleSwitchesListHeader>
<Styled.CheckboxWrapper>
{isOtherChecked && (
<Styled.InputWrapper>
<NewInput
type="text"
label=""
placeholder={`If the listed categories do not adequately describe this breakdown, please describe additional definition/clarification of the "Other" selection.`}
value={otherDescription}
multiline
onChange={(e) => setOtherDescription(e.target.value)}
fullWidth
/>
</Styled.InputWrapper>
)}
<CheckboxOptions
options={raceEthnicityOptions}
onChange={({ key, checked }) => {
if (key === "Other") {
setOtherChecked(!checked);
}
setRacesStatusObject({
...racesStatusObject,
[key]: !checked,
Expand Down
16 changes: 9 additions & 7 deletions publisher/src/stores/MetricConfigStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1193,13 +1193,15 @@ class MetricConfigStore {
raceEthnicitiesDimensions &&
(Object.values(raceEthnicitiesDimensions) as UpdatedDimension[]);

if (otherDescription) {
const otherDimension = dimensions.find(
(dimension) => dimension.race === "Other" && dimension.enabled
);
if (otherDimension) {
otherDimension.other_description = otherDescription;
}
if (otherDescription !== undefined) {
dimensions
.filter((dimension) => dimension.race === "Other")
.forEach((otherDimension) => {
const updatedDimension = otherDimension;
updatedDimension.other_description = otherDimension.enabled
? otherDescription
: "";
});
}

/** Return an object w/ all dimensions in the desired backend data structure for saving purposes */
Expand Down

0 comments on commit 15664f4

Please sign in to comment.