diff --git a/services/ui-src/src/components/fields/Integer.jsx b/services/ui-src/src/components/fields/Integer.jsx index fee83e44f..fa0c4dc9a 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) => { + 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 + ) { + const lastYearFormData = useSelector((state) => state.lastYearFormData); + // 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 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) ?? 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/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";