diff --git a/services/ui-src/src/components/fields/Integer.jsx b/services/ui-src/src/components/fields/Integer.jsx
index fee83e44f..fb59e1fbd 100644
--- a/services/ui-src/src/components/fields/Integer.jsx
+++ b/services/ui-src/src/components/fields/Integer.jsx
@@ -4,12 +4,54 @@ import { TextField } from "@cmsgov/design-system";
import { useSelector } from "react-redux";
import { generateQuestionNumber } from "../utils/helperFunctions";
+const getPrevYearValue = (question, lastYearFormData) => {
+ let prevYearValue;
+
+ // Split and create array from id
+ const splitID = question.id.split("-");
+
+ // the subquestion id (a, b, c, etc)
+ const questionId = splitID[5];
+
+ // Custom handling for -03-c-05 and -03-c-06
+ if (
+ splitID[1] === "03" &&
+ splitID[2] === "c" &&
+ (splitID[3] === "05" || splitID[3] === "06") &&
+ questionId === "a" &&
+ parseInt(splitID[4]) > 2 &&
+ parseInt(splitID[4]) < 10
+ ) {
+ // Set year to last year
+ splitID[0] = parseInt(splitID[0]) - 1;
+ splitID.pop();
+
+ const fieldsetId = splitID.join("-");
+ const partIndex = parseInt(splitID[3]) - 1;
+
+ // Get questions from last years JSON
+ const questions =
+ lastYearFormData[3].contents.section.subsections[2].parts[partIndex]
+ .questions;
+
+ // Filter down to this question
+ const matchingQuestion = questions.filter(
+ (question) => fieldsetId === question?.fieldset_info?.id
+ );
+
+ // The first will always be correct
+ if (matchingQuestion[0]) {
+ prevYearValue = parseInt(matchingQuestion[0].questions[0].answer?.entry);
+ }
+ }
+ return prevYearValue;
+};
+
const Integer = ({ onChange, question, prevYear, ...props }) => {
const [error, setError] = useState(false);
const [answer, setAnswer] = useState(question.answer.entry);
- const lastYearTotals = useSelector((state) => state.lastYearTotals);
- const prevYearNumber =
- lastYearTotals[question.id.substring(0, question.id.length - 2)];
+ const lastYearFormData = useSelector((state) => state.lastYearFormData);
+
const change = ({ target: { name, value } }) => {
const stripped = value.replace(/[^0-9]+/g, "");
const parsed = parseFloat(stripped);
@@ -25,22 +67,14 @@ const Integer = ({ onChange, question, prevYear, ...props }) => {
}
};
- if (prevYearNumber && question.id.indexOf("-a") > -1) {
- return (
-
- );
- }
- const renderAnswer = (val) => (val || Number.isInteger(val) ? val : ""); // may attempt to rerender string on page load, so both val || isInteger
+ const renderAnswer = () => {
+ if (answer === null) {
+ return getPrevYearValue(question, lastYearFormData) ?? prevYear?.value;
+ } else {
+ // may attempt to rerender string on page load, so both answer || isInteger
+ return answer || Number.isInteger(answer) ? answer : "";
+ }
+ };
return (
{
name={question.id}
numeric
onChange={change}
- value={answer != null ? renderAnswer(answer) : prevYear && prevYear.value}
+ value={renderAnswer()}
{...props}
/>
);
@@ -61,5 +95,4 @@ Integer.propTypes = {
prevYear: PropTypes.object,
};
-export { Integer };
export default Integer;
diff --git a/services/ui-src/src/components/fields/Integer.test.jsx b/services/ui-src/src/components/fields/Integer.test.jsx
index e9bb8095e..f120eaf80 100644
--- a/services/ui-src/src/components/fields/Integer.test.jsx
+++ b/services/ui-src/src/components/fields/Integer.test.jsx
@@ -6,7 +6,40 @@ import Integer from "./Integer";
import { screen, render, fireEvent } from "@testing-library/react";
const mockStore = configureMockStore();
-const store = mockStore({ lastYearTotals: { 2022: [] } });
+const lastYearFormData = [
+ {},
+ {},
+ {},
+ {
+ contents: {
+ section: {
+ subsections: [
+ {},
+ {},
+ {
+ parts: [
+ {},
+ {},
+ {},
+ {},
+ {
+ questions: [
+ {
+ fieldset_info: {
+ id: "2022-03-c-05-03",
+ },
+ questions: [{ answer: { entry: 3000 } }],
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ },
+ },
+ },
+];
+const store = mockStore({ lastYearTotals: { 2022: [] }, lastYearFormData });
const buildInteger = (intProps) => {
return (
@@ -73,4 +106,21 @@ describe("", () => {
expect(screen.queryByDisplayValue("raw text")).not.toBeInTheDocument();
expect(screen.getByRole("alert")).toBeInTheDocument();
});
+
+ it("should render previous year value for appropriate 3c part 5 or 6 questions", () => {
+ const props = {
+ question: {
+ id: "2023-03-c-05-03-a",
+ label: "How much?",
+ answer: { entry: null },
+ },
+ };
+
+ render(buildInteger(props));
+
+ expect(screen.getByDisplayValue("3000")).toBeInTheDocument();
+ const input = screen.getByRole("textbox");
+ fireEvent.change(input, { target: { value: 234 } });
+ expect(screen.getByDisplayValue("234")).toBeInTheDocument();
+ });
});
diff --git a/services/ui-src/src/components/fields/Money.jsx b/services/ui-src/src/components/fields/Money.jsx
index d7fcce141..dba1ac251 100644
--- a/services/ui-src/src/components/fields/Money.jsx
+++ b/services/ui-src/src/components/fields/Money.jsx
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
-import { Integer } from "./Integer";
+import Integer from "./Integer";
const Money = ({ ...props }) => {
return ;
diff --git a/services/ui-src/src/components/fields/Question.jsx b/services/ui-src/src/components/fields/Question.jsx
index 271024817..db62d228a 100644
--- a/services/ui-src/src/components/fields/Question.jsx
+++ b/services/ui-src/src/components/fields/Question.jsx
@@ -9,7 +9,7 @@ import { DateRange } from "./DateRange";
import { Email } from "./Email";
import { Fieldset } from "./Fieldset";
import UploadComponent from "../layout/UploadComponent";
-import { Integer } from "./Integer";
+import Integer from "./Integer";
import { MailingAddress } from "./MailingAddress";
import { Money } from "./Money";
import { Objectives } from "./Objectives";