Skip to content

Commit

Permalink
Remove overbearing lastdbvalue
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Summers committed Nov 8, 2023
1 parent fd4436c commit 7e4c315
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 162 deletions.
149 changes: 0 additions & 149 deletions services/ui-src/src/components/fields/ChoiceListField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -725,155 +725,6 @@ describe("Test Choicelist onChangeHandler", () => {
});
});

describe("Test getNestedChildFieldsOfUncheckedParent function", () => {
const checkboxChoiceWithNoChildren = {
id: "checkboxWithNoChild",
label: "State Medicaid agency staff",
value: "State Medicaid agency staff",
name: "checkboxWithNoChild",
checked: false,
};

const checkboxChoiceWithRadioChild = {
id: "checkboxWithRadioChild",
label: "Proprietary system(s)",
children: [
{
id: "radioChild",
type: "radio",
validation: "radio",
props: {
label: "Radio Child Label",
hint: "Radio Child Hint",
choices: [
{
id: "radioChild-1",
label: "Yes",
value: "Yes",
name: "radioChild-1",
checked: false,
},
{
id: "radioChild-2",
label: "No",
value: "No",
name: "radioChild-2",
checked: false,
},
],
},
},
],
value: "Proprietary system(s)",
name: "checkboxWithRadioChild",
checked: false,
};

const checkboxChoiceWithTextFieldChild = {
id: "checkboxWithTextChild",
label: "Other, specify",
children: [
{
id: "checkboxWithTextChild-otherText",
type: "textarea",
validation: "",
props: {},
},
],
value: "Other, specify",
name: "checkboxWithTextChild",
checked: false,
};

beforeEach(() => {
jest.clearAllMocks();
});

test("Checking the checkbox choice and the user has never filled the field out the form before, it returns empty", async () => {
const initialDatabaseValue: Choice[] = [];
const choices = [{ ...checkboxChoiceWithNoChildren, checked: true }];
const returnedValue = getNestedChildFields(choices, initialDatabaseValue);

expect(returnedValue).toStrictEqual([]);
});

test("Unchecking the checkbox choice and the user HAS filled the field out the form before, it returns empty", async () => {
const initialDatabaseValue: Choice[] = [
{
key: "checkboxWithNoChild",
value: "State Medicaid agency staff",
},
];
const choices = [checkboxChoiceWithNoChildren];
const returnedValue = getNestedChildFields(choices, initialDatabaseValue);

expect(returnedValue).toStrictEqual([]);
});

test("Checking the radio choice and the user has never filled the field out the form before, it returns empty", async () => {
const initialDatabaseValue: Choice[] = [];
const choices = [{ ...checkboxChoiceWithRadioChild, checked: true }];
const returnedValue = getNestedChildFields(choices, initialDatabaseValue);

expect(returnedValue).toStrictEqual([]);
});

test("Unchecking the radio choice and the user HAS filled the field out the form before, it returns the child object", async () => {
const initialDatabaseValue: Choice[] = [
{
key: "checkboxWithRadioChild",
value: "Proprietary system(s)",
},
];
const choices = [checkboxChoiceWithRadioChild];
const returnedValue = getNestedChildFields(choices, initialDatabaseValue);

const expectedReturn = [
{
name: "radioChild",
type: "radio",
value: [],
overrideCheck: true,
defaultValue: undefined,
hydrationValue: undefined,
},
];

expect(returnedValue).toStrictEqual(expectedReturn);
});

test("Checking the choice and the user has never filled the field out the form before, it returns empty", async () => {
const initialDatabaseValue: Choice[] = [];
const choices = [{ ...checkboxChoiceWithTextFieldChild, checked: true }];
const returnedValue = getNestedChildFields(choices, initialDatabaseValue);

expect(returnedValue).toStrictEqual([]);
});

test("Unchecking the choice and the user HAS filled the field out the form before, it returns the child object", async () => {
const initialDatabaseValue: Choice[] = [
{
key: "checkboxWithTextChild",
value: "Other, specify",
},
];
const choices = [checkboxChoiceWithTextFieldChild];
const returnedValue = getNestedChildFields(choices, initialDatabaseValue);

const expectedReturn = [
{
name: "checkboxWithTextChild-otherText",
type: "textarea",
value: "",
overrideCheck: true,
defaultValue: undefined,
hydrationValue: undefined,
},
];
expect(returnedValue).toStrictEqual(expectedReturn);
});
});

describe("ChoiceListField handles triggering validation", () => {
const choiceListFieldValidateOnRenderComponent = (
<ReportContext.Provider value={mockMcparReportContext}>
Expand Down
16 changes: 3 additions & 13 deletions services/ui-src/src/components/fields/ChoiceListField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export const ChoiceListField = ({
}: Props) => {
const defaultValue: Choice[] = [];
const [displayValue, setDisplayValue] = useState<Choice[]>(defaultValue);
const [lastDatabaseValue, setLastDatabaseValue] =
useState<Choice[]>(defaultValue);

const { report, updateReport } = useContext(ReportContext);
const { entities, entityType, updateEntities, selectedEntity } =
Expand Down Expand Up @@ -71,7 +69,6 @@ export const ChoiceListField = ({
const fieldValue = form.getValues(name);
if (fieldValue) {
setDisplayValue(fieldValue);
setLastDatabaseValue(fieldValue);
}
// else if hydration value exists, set as display value
else if (hydrationValue) {
Expand All @@ -89,7 +86,6 @@ export const ChoiceListField = ({
form.setValue(name, defaultValue);
} else {
setDisplayValue(hydrationValue);
setLastDatabaseValue(hydrationValue);
if (validateOnRender)
form.setValue(name, hydrationValue, { shouldValidate: true });
else form.setValue(name, hydrationValue);
Expand Down Expand Up @@ -232,10 +228,7 @@ export const ChoiceListField = ({

const combinedFields = [
...fields,
...getNestedChildFields(
choicesWithNestedEnabledFields,
lastDatabaseValue
),
...getNestedChildFields(choicesWithNestedEnabledFields),
];
const reportArgs = {
id: report?.id,
Expand Down Expand Up @@ -314,8 +307,7 @@ const sx = {
};

export const getNestedChildFields = (
choices: FieldChoice[],
lastDatabaseValue: Choice[]
choices: FieldChoice[]
): AutosaveField[] => {
// set up nested field compilation
const nestedFields: any = [];
Expand Down Expand Up @@ -347,9 +339,7 @@ export const getNestedChildFields = (

choices.forEach((choice: FieldChoice) => {
// if choice is not selected and there are children
const isParentChoiceChecked = (id: string) =>
lastDatabaseValue?.some((autosave) => autosave.key === id);
if (choice.children && isParentChoiceChecked(choice.id)) {
if (choice.children && !choice.checked) {
compileNestedFields(choice.children);
}
});
Expand Down

0 comments on commit 7e4c315

Please sign in to comment.