From 4f44e1a7a2dbd25e526c44c63f69a03a6b6adf87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0j=C3=B3n=20Gu=C3=B0j=C3=B3nsson?= Date: Tue, 2 Jul 2024 15:04:43 +0000 Subject: [PATCH] fix(j-s): Court Arrangements (#15406) * Fixes court arrangement * Removes redundant null check * Fixes initialization when requested court date should be used to autofill arraignment date --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../CourtArrangements/CourtArrangements.tsx | 72 +++++++++---------- .../Indictments/Conclusion/Conclusion.tsx | 2 +- .../Court/Indictments/Subpoena/Subpoena.tsx | 8 +-- .../HearingArrangements.tsx | 16 ++--- .../CourtRecord/CourtRecord.tsx | 25 ++++--- .../HearingArrangements.tsx | 16 ++--- .../judicial-system/web/src/utils/validate.ts | 30 +++++--- 7 files changed, 86 insertions(+), 83 deletions(-) diff --git a/apps/judicial-system/web/src/components/CourtArrangements/CourtArrangements.tsx b/apps/judicial-system/web/src/components/CourtArrangements/CourtArrangements.tsx index 13717ed01a05..713b7f0f548f 100644 --- a/apps/judicial-system/web/src/components/CourtArrangements/CourtArrangements.tsx +++ b/apps/judicial-system/web/src/components/CourtArrangements/CourtArrangements.tsx @@ -41,15 +41,14 @@ export const useCourtArrangements = ( const { setAndSendCaseToServer } = useCase() const [original, setOriginal] = useState<[DateLogKey, DateLog | undefined | null]>() - const [courtDate, setCourtDate] = useState() + const [courtDate, setCourtDate] = useState({}) const [courtDateHasChanged, setCourtDateHasChanged] = useState(false) useEffect(() => { if ( - original && - original[0] === dateKey && - original[1]?.date === workingCase[dateKey]?.date && - original[1]?.location === workingCase[dateKey]?.location + original?.[0] === dateKey && + original?.[1]?.date === workingCase[dateKey]?.date && + original?.[1]?.location === workingCase[dateKey]?.location ) { // Do not reset the court date if it has not changed return @@ -57,48 +56,40 @@ export const useCourtArrangements = ( setOriginal([dateKey, workingCase[dateKey]]) - if (workingCase[dateKey]) { - setCourtDate(workingCase[dateKey]) - } + setCourtDate({ + date: workingCase[dateKey]?.date, + location: workingCase[dateKey]?.location, + }) }, [dateKey, original, workingCase]) const handleCourtDateChange = ( date: Date | undefined | null, valid = true, ) => { - if (!date) { - setCourtDate(null) - } else if (date && valid) { - const oldDate = workingCase[dateKey] - if ( - oldDate?.date && - compareAsc(date, new Date(oldDate.date)) !== 0 && - hasSentNotification( - NotificationType.COURT_DATE, - workingCase.notifications, - ).hasSent - ) { - setCourtDateHasChanged(true) - } - - setCourtDate((previous) => - previous - ? { ...previous, date: formatDateForServer(date) } - : { date: formatDateForServer(date) }, + if (date && valid) { + const courtDateHasChanged = Boolean( + original?.[1]?.date && + compareAsc(date, new Date(original[1].date)) !== 0 && + hasSentNotification( + NotificationType.COURT_DATE, + workingCase.notifications, + ).hasSent, ) + + setCourtDateHasChanged(courtDateHasChanged) } + + setCourtDate((previous) => ({ + ...previous, + date: date ? formatDateForServer(date) : null, + })) } const handleCourtRoomChange = (courtRoom?: string | null) => { - if (!courtRoom) { - setCourtDate((prev) => ({ ...prev, location: null })) - } else { - setCourtDate((previous) => - previous - ? { ...previous, location: courtRoom } - : { location: courtRoom }, - ) - } + setCourtDate((previous) => ({ + ...previous, + location: courtRoom ? courtRoom : null, + })) } const sendCourtDateToServer = (otherUpdates: UpdateCase[] = []) => { @@ -141,6 +132,12 @@ export const CourtArrangements: FC = (props) => { } = props const { formatMessage } = useIntl() + const [courtRoom, setCourtRoom] = useState(courtDate?.location ?? '') + + useEffect(() => { + setCourtRoom(courtDate?.location ?? '') + }, [courtDate?.location]) + const renderCourtArrangements = () => ( <> @@ -159,9 +156,10 @@ export const CourtArrangements: FC = (props) => { name="courtroom" label={formatMessage(strings.courtRoomLabel)} autoComplete="off" - value={courtDate?.location || ''} + value={courtRoom} placeholder="Skráðu inn dómsal" onChange={(evt) => { + setCourtRoom(evt.target.value) handleCourtRoomChange(evt.target.value) }} disabled={courtRoomDisabled} diff --git a/apps/judicial-system/web/src/routes/Court/Indictments/Conclusion/Conclusion.tsx b/apps/judicial-system/web/src/routes/Court/Indictments/Conclusion/Conclusion.tsx index a11b33af4780..657689a9345c 100644 --- a/apps/judicial-system/web/src/routes/Court/Indictments/Conclusion/Conclusion.tsx +++ b/apps/judicial-system/web/src/routes/Court/Indictments/Conclusion/Conclusion.tsx @@ -379,7 +379,7 @@ const Conclusion: FC = () => { diff --git a/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx b/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx index ff90d0cc0e45..61ad34b01190 100644 --- a/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx +++ b/apps/judicial-system/web/src/routes/Court/Indictments/Subpoena/Subpoena.tsx @@ -100,11 +100,7 @@ const Subpoena: FC = () => { ], ) - const stepIsValid = isSubpoenaStepValid( - workingCase, - courtDate?.date, - courtDate?.location, - ) + const stepIsValid = isSubpoenaStepValid(workingCase, courtDate) return ( { { const initialize = useCallback(() => { if (!workingCase.arraignmentDate && workingCase.requestedCourtDate) { - handleCourtDateChange(new Date(workingCase.requestedCourtDate)) + setWorkingCase((theCase) => ({ + ...theCase, + arraignmentDate: { date: theCase.requestedCourtDate }, + })) } setAndSendCaseToServer( @@ -70,12 +73,7 @@ const HearingArrangements = () => { workingCase, setWorkingCase, ) - }, [ - handleCourtDateChange, - setAndSendCaseToServer, - setWorkingCase, - workingCase, - ]) + }, [setAndSendCaseToServer, setWorkingCase, workingCase]) useOnceOn(isCaseUpToDate, initialize) @@ -110,7 +108,7 @@ const HearingArrangements = () => { const stepIsValid = isCourtHearingArrangementsStepValidIC( workingCase, - courtDate?.date, + courtDate, ) const isCorrectingRuling = workingCase.notifications?.some( @@ -304,7 +302,7 @@ const HearingArrangements = () => { diff --git a/apps/judicial-system/web/src/routes/Court/RestrictionCase/CourtRecord/CourtRecord.tsx b/apps/judicial-system/web/src/routes/Court/RestrictionCase/CourtRecord/CourtRecord.tsx index e784bfaf65f6..319d59117d77 100644 --- a/apps/judicial-system/web/src/routes/Court/RestrictionCase/CourtRecord/CourtRecord.tsx +++ b/apps/judicial-system/web/src/routes/Court/RestrictionCase/CourtRecord/CourtRecord.tsx @@ -490,19 +490,18 @@ export const CourtRecord: FC = () => { maxDate={new Date()} selectedDate={workingCase.courtEndTime} onChange={(date: Date | undefined, valid: boolean) => { - setAndSendCaseToServer( - [ - { - courtEndTime: - date && valid - ? formatDateForServer(date) - : undefined, - force: true, - }, - ], - workingCase, - setWorkingCase, - ) + if (date && valid) { + setAndSendCaseToServer( + [ + { + courtEndTime: formatDateForServer(date), + force: true, + }, + ], + workingCase, + setWorkingCase, + ) + } }} blueBox={false} required diff --git a/apps/judicial-system/web/src/routes/Court/RestrictionCase/HearingArrangements/HearingArrangements.tsx b/apps/judicial-system/web/src/routes/Court/RestrictionCase/HearingArrangements/HearingArrangements.tsx index 2b5466d5263e..f76f4d5002e6 100644 --- a/apps/judicial-system/web/src/routes/Court/RestrictionCase/HearingArrangements/HearingArrangements.tsx +++ b/apps/judicial-system/web/src/routes/Court/RestrictionCase/HearingArrangements/HearingArrangements.tsx @@ -60,7 +60,10 @@ export const HearingArrangements = () => { const initialize = useCallback(() => { if (!workingCase.arraignmentDate?.date && workingCase.requestedCourtDate) { - handleCourtDateChange(new Date(workingCase.requestedCourtDate)) + setWorkingCase((theCase) => ({ + ...theCase, + arraignmentDate: { date: theCase.requestedCourtDate }, + })) } setAndSendCaseToServer( @@ -89,12 +92,7 @@ export const HearingArrangements = () => { workingCase, setWorkingCase, ) - }, [ - handleCourtDateChange, - setAndSendCaseToServer, - setWorkingCase, - workingCase, - ]) + }, [setAndSendCaseToServer, setWorkingCase, workingCase]) useOnceOn(isCaseUpToDate, initialize) @@ -129,7 +127,7 @@ export const HearingArrangements = () => { const stepIsValid = isCourtHearingArrangemenstStepValidRC( workingCase, - courtDate?.date, + courtDate, ) const isCorrectingRuling = workingCase.notifications?.some( @@ -173,7 +171,7 @@ export const HearingArrangements = () => { diff --git a/apps/judicial-system/web/src/utils/validate.ts b/apps/judicial-system/web/src/utils/validate.ts index db4d20081612..2b1a7b353b0a 100644 --- a/apps/judicial-system/web/src/utils/validate.ts +++ b/apps/judicial-system/web/src/utils/validate.ts @@ -10,6 +10,7 @@ import { CaseFileCategory, CaseIndictmentRulingDecision, CaseType, + DateLog, DefenderChoice, IndictmentDecision, SessionArrangements, @@ -333,18 +334,23 @@ export const isReceptionAndAssignmentStepValid = ( export const isCourtHearingArrangemenstStepValidRC = ( workingCase: Case, - courtDate?: string | null, + arraignmentDate?: DateLog, ): boolean => { return validate([ [workingCase.defenderEmail, ['email-format']], [workingCase.defenderPhoneNumber, ['phonenumber']], - [courtDate ?? workingCase.arraignmentDate?.date, ['empty', 'date-format']], + [ + arraignmentDate + ? arraignmentDate.date + : workingCase.arraignmentDate?.date, + ['empty', 'date-format'], + ], ]).isValid } export const isCourtHearingArrangementsStepValidIC = ( workingCase: Case, - courtDate?: string | null, + arraignmentDate?: DateLog, ): boolean => { return Boolean( workingCase.sessionArrangements && @@ -352,7 +358,9 @@ export const isCourtHearingArrangementsStepValidIC = ( [workingCase.defenderEmail, ['email-format']], [workingCase.defenderPhoneNumber, ['phonenumber']], [ - courtDate ?? workingCase.arraignmentDate?.date, + arraignmentDate + ? arraignmentDate.date + : workingCase.arraignmentDate?.date, ['empty', 'date-format'], ], ]).isValid, @@ -414,16 +422,22 @@ export const isCourtRecordStepValidIC = (workingCase: Case): boolean => { export const isSubpoenaStepValid = ( workingCase: Case, - courtDate?: string | null, - courtRoom?: string | null, + arraignmentDate?: DateLog, ): boolean => { return ( validate([ [ - courtDate ?? workingCase.arraignmentDate?.date, + arraignmentDate + ? arraignmentDate.date + : workingCase.arraignmentDate?.date, ['empty', 'date-format'], ], - [courtRoom ?? workingCase.arraignmentDate?.location, ['empty']], + [ + arraignmentDate + ? arraignmentDate.location + : workingCase.arraignmentDate?.location, + ['empty'], + ], ]).isValid && Boolean( workingCase.defendants?.every((defendant) => defendant.subpoenaType),