From 87031d01fd918fce12e49c07f57ebeb558e52a2f Mon Sep 17 00:00:00 2001 From: Daniel Meegan Date: Tue, 2 Mar 2021 20:44:34 -0500 Subject: [PATCH 1/3] fix MergeYears to accept retireDate passed #264 --- src/library/user-state-manager.tsx | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/library/user-state-manager.tsx b/src/library/user-state-manager.tsx index 471b7064..f299acb1 100644 --- a/src/library/user-state-manager.tsx +++ b/src/library/user-state-manager.tsx @@ -95,18 +95,15 @@ const useYear62State = createPersistedState("Year62", global.sessionStorage); */ function mergeYears( earnings: EarningsRecord, - birthDate: Date | null - //,retireDate: Date | null + birthDate: Date | null, + retireDate: Date | null ): EarningsRecord { - /* if we don't have other info, just return what was passed in */ if (birthDate === null) return earnings; - //let cleanRetireDate = retireDate === null? dayjs(birthDate).add(70,'year').toDate(): retireDate; - let cleanRetireDate = dayjs(birthDate).add(71,'year').toDate(); + let cleanRetireDate = (retireDate === null) ? dayjs(birthDate).add(70,'year').toDate(): retireDate; const earningsRecord = earnings || {}; - /*TODO(TDK) why do we need to create a new date object here, it is a string coming in? */ - const birthYear = new Date(birthDate).getFullYear(); + const birthYear = birthDate.getFullYear(); const startEmploymentYear = (earnings && Object.keys(earnings) && Number(Object.keys(earnings)[0])) || birthYear + 18; @@ -123,7 +120,6 @@ function mergeYears( } return tempRecord; } - /** * Helper function to get a Date object equivalent to the start of the date given */ From 8a9ac2302daaad5db90dfb1410dc44fb2cbee7b4 Mon Sep 17 00:00:00 2001 From: Daniel Meegan Date: Tue, 2 Mar 2021 23:15:14 -0500 Subject: [PATCH 2/3] set up MergeYears comparisons #264 --- src/library/user-state-manager.tsx | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/library/user-state-manager.tsx b/src/library/user-state-manager.tsx index f299acb1..1c8b5618 100644 --- a/src/library/user-state-manager.tsx +++ b/src/library/user-state-manager.tsx @@ -89,25 +89,36 @@ const useYear62State = createPersistedState("Year62", global.sessionStorage); * and the birth year to specified retirement date of the user, * whichever is more complete. * If no retire date is specified, set it to their 70th birthday - * - * TODO : allow specified retire date to update length of array - * e.g. let cleanRetireDate = retireDate === null? dayjs(birthDate).add(70,'year').toDate(): retireDate; */ function mergeYears( earnings: EarningsRecord, birthDate: Date | null, - retireDate: Date | null + retireDate: Date | null, + expectedLastEarningYear: number | null ): EarningsRecord { + console.log(`expectedLastEarningYear:${expectedLastEarningYear}, birthdate: ${birthDate}, retireDate: ${retireDate}`) if (birthDate === null) return earnings; - let cleanRetireDate = (retireDate === null) ? dayjs(birthDate).add(70,'year').toDate(): retireDate; + const birthYear = dayjs(birthDate).get('year'); + const maxRetireDate = dayjs(birthDate).add(71,'year').toDate() + const retireYearFromReport = dayjs(retireDate!).get('year') || null; + + let cleanRetireDate: Date; + if ((expectedLastEarningYear && retireDate) === null) { + cleanRetireDate = dayjs(birthDate).add(71,'year').toDate(); + } else if ((expectedLastEarningYear === null) && (retireYearFromReport! - birthYear) < 71){ + cleanRetireDate = retireDate! + } else if ((retireDate === null) && ((expectedLastEarningYear! - birthYear) < 71)){ + cleanRetireDate = dayjs().year(expectedLastEarningYear!).toDate() + } else { + cleanRetireDate = dayjs(birthDate).add(71,'year').toDate(); + } const earningsRecord = earnings || {}; - const birthYear = birthDate.getFullYear(); const startEmploymentYear = (earnings && Object.keys(earnings) && Number(Object.keys(earnings)[0])) || birthYear + 18; - const retireYear = cleanRetireDate.getFullYear(); + const retireYear = dayjs(cleanRetireDate).get('year'); const endYear = retireYear; var tempRecord = {} as EarningsRecord; @@ -257,8 +268,8 @@ export default function UserStateManager( /* setEarnings & mergeYears is unlikely to fire retroactively on changes of birthdate or retire date ideally we'd have a single function for all three alterations (or wrappers around each of them) if we go this way in the end*/ - setEarnings: (earnings) => setEarnings(mergeYears(earnings, birthDate, retireDate)), - /* note the entries in the dependency array for birthDate and retireDate */ + setEarnings: (earnings) => setEarnings(mergeYears(earnings, birthDate, retireDate, expectedLastEarningYear)), + /* note the entries in the dependency array for birthDate, retireDate, and expectedLastEarningYear */ setEarningsFormat, setHaveSSAAccount, setIsEmploymentCovered, @@ -291,7 +302,8 @@ export default function UserStateManager( setAwiTrendSelection, setExpectedPercentageWageIncrease, birthDate, - retireDate + retireDate, + expectedLastEarningYear ] ); From 8288314cd2854993516adb0c25212fff722a21ee Mon Sep 17 00:00:00 2001 From: Daniel Meegan Date: Tue, 30 Mar 2021 20:05:35 -0400 Subject: [PATCH 3/3] useMaxRetireYear --- src/library/user-state-manager.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/library/user-state-manager.tsx b/src/library/user-state-manager.tsx index 1c8b5618..128b2b48 100644 --- a/src/library/user-state-manager.tsx +++ b/src/library/user-state-manager.tsx @@ -110,7 +110,7 @@ function mergeYears( } else if ((retireDate === null) && ((expectedLastEarningYear! - birthYear) < 71)){ cleanRetireDate = dayjs().year(expectedLastEarningYear!).toDate() } else { - cleanRetireDate = dayjs(birthDate).add(71,'year').toDate(); + cleanRetireDate = maxRetireDate } const earningsRecord = earnings || {};