Skip to content

Commit

Permalink
fix(j-s): Court Arrangements (#15406)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
gudjong and kodiakhq[bot] authored Jul 2, 2024
1 parent b8a64b9 commit 4f44e1a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,64 +41,55 @@ export const useCourtArrangements = (
const { setAndSendCaseToServer } = useCase()
const [original, setOriginal] =
useState<[DateLogKey, DateLog | undefined | null]>()
const [courtDate, setCourtDate] = useState<DateLog | null>()
const [courtDate, setCourtDate] = useState<DateLog>({})
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
}

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[] = []) => {
Expand Down Expand Up @@ -141,6 +132,12 @@ export const CourtArrangements: FC<Props> = (props) => {
} = props
const { formatMessage } = useIntl()

const [courtRoom, setCourtRoom] = useState<string>(courtDate?.location ?? '')

useEffect(() => {
setCourtRoom(courtDate?.location ?? '')
}, [courtDate?.location])

const renderCourtArrangements = () => (
<>
<Box marginBottom={2}>
Expand All @@ -159,9 +156,10 @@ export const CourtArrangements: FC<Props> = (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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ const Conclusion: FC = () => {
<CourtArrangements
handleCourtDateChange={handleCourtDateChange}
handleCourtRoomChange={handleCourtRoomChange}
courtDate={courtDate}
courtDate={workingCase.courtDate}
blueBox={false}
/>
</BlueBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ const Subpoena: FC = () => {
],
)

const stepIsValid = isSubpoenaStepValid(
workingCase,
courtDate?.date,
courtDate?.location,
)
const stepIsValid = isSubpoenaStepValid(workingCase, courtDate)

return (
<PageLayout
Expand Down Expand Up @@ -183,7 +179,7 @@ const Subpoena: FC = () => {
<CourtArrangements
handleCourtDateChange={handleCourtDateChange}
handleCourtRoomChange={handleCourtRoomChange}
courtDate={courtDate}
courtDate={workingCase.arraignmentDate}
dateTimeDisabled={isArraignmentDone}
courtRoomDisabled={isArraignmentDone}
courtRoomRequired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ const HearingArrangements = () => {

const initialize = useCallback(() => {
if (!workingCase.arraignmentDate && workingCase.requestedCourtDate) {
handleCourtDateChange(new Date(workingCase.requestedCourtDate))
setWorkingCase((theCase) => ({
...theCase,
arraignmentDate: { date: theCase.requestedCourtDate },
}))
}

setAndSendCaseToServer(
Expand All @@ -70,12 +73,7 @@ const HearingArrangements = () => {
workingCase,
setWorkingCase,
)
}, [
handleCourtDateChange,
setAndSendCaseToServer,
setWorkingCase,
workingCase,
])
}, [setAndSendCaseToServer, setWorkingCase, workingCase])

useOnceOn(isCaseUpToDate, initialize)

Expand Down Expand Up @@ -110,7 +108,7 @@ const HearingArrangements = () => {

const stepIsValid = isCourtHearingArrangementsStepValidIC(
workingCase,
courtDate?.date,
courtDate,
)

const isCorrectingRuling = workingCase.notifications?.some(
Expand Down Expand Up @@ -304,7 +302,7 @@ const HearingArrangements = () => {
<CourtArrangements
handleCourtDateChange={handleCourtDateChange}
handleCourtRoomChange={handleCourtRoomChange}
courtDate={courtDate}
courtDate={workingCase.arraignmentDate}
courtRoomDisabled={isCorrectingRuling}
dateTimeDisabled={isCorrectingRuling}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -89,12 +92,7 @@ export const HearingArrangements = () => {
workingCase,
setWorkingCase,
)
}, [
handleCourtDateChange,
setAndSendCaseToServer,
setWorkingCase,
workingCase,
])
}, [setAndSendCaseToServer, setWorkingCase, workingCase])

useOnceOn(isCaseUpToDate, initialize)

Expand Down Expand Up @@ -129,7 +127,7 @@ export const HearingArrangements = () => {

const stepIsValid = isCourtHearingArrangemenstStepValidRC(
workingCase,
courtDate?.date,
courtDate,
)

const isCorrectingRuling = workingCase.notifications?.some(
Expand Down Expand Up @@ -173,7 +171,7 @@ export const HearingArrangements = () => {
<CourtArrangements
handleCourtDateChange={handleCourtDateChange}
handleCourtRoomChange={handleCourtRoomChange}
courtDate={courtDate}
courtDate={workingCase.arraignmentDate}
courtRoomDisabled={isCorrectingRuling}
dateTimeDisabled={isCorrectingRuling}
/>
Expand Down
30 changes: 22 additions & 8 deletions apps/judicial-system/web/src/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CaseFileCategory,
CaseIndictmentRulingDecision,
CaseType,
DateLog,
DefenderChoice,
IndictmentDecision,
SessionArrangements,
Expand Down Expand Up @@ -333,26 +334,33 @@ 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 &&
validate([
[workingCase.defenderEmail, ['email-format']],
[workingCase.defenderPhoneNumber, ['phonenumber']],
[
courtDate ?? workingCase.arraignmentDate?.date,
arraignmentDate
? arraignmentDate.date
: workingCase.arraignmentDate?.date,
['empty', 'date-format'],
],
]).isValid,
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 4f44e1a

Please sign in to comment.