Skip to content

Commit

Permalink
fix: Demographics Race Field Render Issue (#4102)
Browse files Browse the repository at this point in the history
* fix: use current values for race fields

* fix: remove unneeded dependencies

* fix: decoupling and simplifying
  • Loading branch information
mcgarrye authored May 31, 2024
1 parent 34794db commit 76b77ad
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
19 changes: 19 additions & 0 deletions shared-helpers/src/utilities/formKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,25 @@ export const raceKeys: subCheckboxes = {
declineToRespond: [],
}

export const isKeyIncluded = (
searchKey: string,
originalValues: Array<string> | undefined
): boolean => {
let keyExists = false
originalValues?.forEach((key) => {
if (key.includes(searchKey)) {
keyExists = true
}
})
return keyExists
}

// Get the value of a field that is storing a custom value, i.e. "otherAsian: Custom Race Input"
export const getCustomValue = (subKey: string, formValues: Array<string> | undefined): string => {
const customValues = formValues?.find((value: string) => value.split(":")[0] === subKey)
return customValues?.length ? customValues.split(":")[1]?.substring(1) : ""
}

export const howDidYouHear = [
{
id: "jurisdictionWebsite",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import React, { useMemo } from "react"
import { useFormContext } from "react-hook-form"
import { t, Select, FieldGroup } from "@bloom-housing/ui-components"
import { Grid } from "@bloom-housing/ui-seeds"
import { ethnicityKeys, raceKeys, howDidYouHear } from "@bloom-housing/shared-helpers"
import {
ethnicityKeys,
raceKeys,
isKeyIncluded,
getCustomValue,
howDidYouHear,
} from "@bloom-housing/shared-helpers"
import { Demographic } from "@bloom-housing/shared-helpers/src/types/backend-swagger"
import SectionWithGrid from "../../../shared/SectionWithGrid"

Expand All @@ -24,41 +30,24 @@ const FormDemographics = ({ formValues }: FormDemographicsProps) => {
}))
}, [register])

// Does a key exist in a root field or a sub array
const isKeyIncluded = (raceKey: string) => {
let keyExists = false
formValues?.race?.forEach((value) => {
if (value.includes(raceKey)) {
keyExists = true
}
})
return keyExists
}

// Get the value of a field that is storing a custom value, i.e. "otherAsian: Custom Race Input"
const getCustomValue = (subKey: string) => {
const customValues = formValues?.race?.filter((value) => value.split(":")[0] === subKey)
return customValues?.length ? customValues[0].split(":")[1]?.substring(1) : ""
}

const raceOptions = useMemo(() => {
return Object.keys(raceKeys).map((rootKey) => ({
id: rootKey,
label: t(`application.review.demographics.raceOptions.${rootKey}`),
value: rootKey,
additionalText: rootKey.indexOf("other") >= 0,
defaultChecked: isKeyIncluded(rootKey),
defaultText: getCustomValue(rootKey),
defaultChecked: isKeyIncluded(rootKey, formValues?.race),
defaultText: getCustomValue(rootKey, formValues?.race),
subFields: raceKeys[rootKey].map((subKey) => ({
id: subKey,
label: t(`application.review.demographics.raceOptions.${subKey}`),
value: subKey,
defaultChecked: isKeyIncluded(subKey),
defaultChecked: isKeyIncluded(subKey, formValues?.race),
additionalText: subKey.indexOf("other") >= 0,
defaultText: getCustomValue(subKey),
defaultText: getCustomValue(subKey, formValues?.race),
})),
}))
}, [register, isKeyIncluded, getCustomValue])
}, [register])

return (
<>
Expand All @@ -72,9 +61,11 @@ const FormDemographics = ({ formValues }: FormDemographicsProps) => {
type="checkbox"
register={register}
groupLabel={t("application.add.race")}
strings={{
description: "",
}}
/>
</Grid.Cell>

<Grid.Cell>
<Select
id="application.demographics.ethnicity"
Expand Down
42 changes: 25 additions & 17 deletions sites/public/src/pages/applications/review/demographics.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect } from "react"
import React, { useContext, useEffect, useMemo } from "react"
import { useForm } from "react-hook-form"

import { FieldGroup, Form, Select, t } from "@bloom-housing/ui-components"
Expand All @@ -7,6 +7,8 @@ import { MultiselectQuestionsApplicationSectionEnum } from "@bloom-housing/share
import {
ethnicityKeys,
raceKeys,
isKeyIncluded,
getCustomValue,
howDidYouHear,
fieldGroupObjectToArray,
OnClientSide,
Expand Down Expand Up @@ -61,6 +63,25 @@ const ApplicationDemographics = () => {
}))
}

const raceOptions = useMemo(() => {
return Object.keys(raceKeys).map((rootKey) => ({
id: rootKey,
label: t(`application.review.demographics.raceOptions.${rootKey}`),
value: rootKey,
additionalText: rootKey.indexOf("other") >= 0,
defaultChecked: isKeyIncluded(rootKey, application.demographics?.race),
defaultText: getCustomValue(rootKey, application.demographics?.race),
subFields: raceKeys[rootKey].map((subKey) => ({
id: subKey,
label: t(`application.review.demographics.raceOptions.${subKey}`),
value: subKey,
defaultChecked: isKeyIncluded(subKey, application.demographics?.race),
additionalText: subKey.indexOf("other") >= 0,
defaultText: getCustomValue(subKey, application.demographics?.race),
})),
}))
}, [register])

useEffect(() => {
pushGtmEvent<PageView>({
event: "pageView",
Expand Down Expand Up @@ -94,26 +115,13 @@ const ApplicationDemographics = () => {
</legend>
<FieldGroup
name="race"
fields={Object.keys(raceKeys).map((rootKey) => ({
id: rootKey,
label: t(`application.review.demographics.raceOptions.${rootKey}`),
value: rootKey,
additionalText: rootKey.indexOf("other") >= 0,
defaultChecked: application[`race-${rootKey}`],
subFields: raceKeys[rootKey].map((subKey) => ({
id: subKey,
label: t(`application.review.demographics.raceOptions.${subKey}`),
value: subKey,
defaultChecked: application[`race-${subKey}`],
additionalText: subKey.indexOf("other") >= 0,
})),
}))}
fields={raceOptions}
type="checkbox"
register={register}
strings={{
description: "",
}}
type="checkbox"
dataTestId={"app-demographics-race"}
register={register}
/>
</fieldset>
<div className={"pt-4"}>
Expand Down

0 comments on commit 76b77ad

Please sign in to comment.