Skip to content

Commit 43c6311

Browse files
Enhance error handling and scrolling behavior in error summary and catch details components
1 parent 67ac97c commit 43c6311

4 files changed

Lines changed: 21 additions & 25 deletions

File tree

app/components/errorSummary.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ export const ErrorSummary = ({
1616
}: React.PropsWithChildren<IErrorSummaryProps>) => {
1717
const { t } = useTranslation(["errorsText", "common"]);
1818
const summaryRef = useRef<HTMLDivElement>(null);
19+
const hasFocusedRef = useRef(false);
1920

2021
useEffect(() => {
21-
if (errors.length > 0) {
22+
if (errors.length > 0 && !hasFocusedRef.current) {
2223
summaryRef.current?.focus();
24+
hasFocusedRef.current = true;
2325
}
2426
}, [errors]);
2527

app/hooks/useScrollOnPageError.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import isEmpty from "lodash/isEmpty";
2-
import { useEffect } from "react";
3-
import { scrollToId } from "~/helpers";
4-
import type { IError, IErrorsTransformed } from "~/types";
5-
6-
export const useScrollOnPageError = (errors: IError[] | IErrorsTransformed | undefined) => {
7-
useEffect(() => {
8-
if (!isEmpty(errors)) {
9-
scrollToId("errorIsland");
10-
}
11-
}, [errors]);
12-
};
1+
import isEmpty from "lodash/isEmpty";
2+
import { useEffect } from "react";
3+
import { scrollToId } from "~/helpers";
4+
import type { IError, IErrorsTransformed } from "~/types";
5+
6+
export const useScrollOnPageError = (errors: IError[] | IErrorsTransformed | undefined) => {
7+
useEffect(() => {
8+
if (!isEmpty(errors)) {
9+
scrollToId("errorIsland");
10+
const el = globalThis.document.getElementById("errorIsland") as HTMLDivElement | null;
11+
el?.focus();
12+
}
13+
}, [errors]);
14+
};

app/models/addCatchDetails.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ export const AddCatchDetailsAction = async (request: Request, params: Params): P
462462
return redirect("/forbidden");
463463
}
464464

465-
const recordsPerPage: number = parseInt(getEnv().PROCESSING_STATEMENT_CATCH_PER_PAGE, 10);
465+
const recordsPerPage: number = Number.parseInt(getEnv().PROCESSING_STATEMENT_CATCH_PER_PAGE, 10);
466466
const ctches = getUpdatedCatches(updatedProcessingStatement, productId);
467467
const totalPages = Math.ceil(ctches.length / recordsPerPage);
468468

@@ -472,7 +472,7 @@ export const AddCatchDetailsAction = async (request: Request, params: Params): P
472472

473473
if (cancelCatch) {
474474
if (values["species"]) {
475-
session.set("retainedSpecies", values["species"] as string);
475+
session.set("retainedSpecies", values["species"]);
476476
}
477477
const redirectUrl = `/create-processing-statement/${documentNumber}/add-catch-details/${productId}?pageNo=${goToPage}`;
478478
return redirect(redirectUrl, {

app/routes/create-processing-statement.$documentNumber.add-catch-details._index.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from "~/composite-components";
2424
import type { Catch, pageLinks, ErrorResponse, Species, ICountry } from "~/types";
2525
import { getMeta, scrollToId, querySpecies, displayErrorMessagesInOrder } from "~/helpers";
26-
import { useIsHydrated } from "~/hooks";
26+
import { useIsHydrated, useScrollOnPageError } from "~/hooks";
2727
import { AddCatchDetailsAction, AddCatchDetailsLoader } from "~/models";
2828

2929
interface ILoaderData {
@@ -644,15 +644,7 @@ const AddCatchDetailsIndex = () => {
644644
});
645645

646646
const navigationLinks = populateNavigationLinks(previousLinkLayout, nextLinkLayout);
647-
648-
// Derive a boolean from errors to avoid object reference issues in useEffect
649-
const hasErrors = !isEmpty(errors);
650-
651-
useEffect(() => {
652-
if (hasErrors) {
653-
scrollToId("errorIsland");
654-
}
655-
}, [hasErrors]); // Boolean primitive, safe to use as dependency
647+
useScrollOnPageError(errors);
656648

657649
useEffect(() => {
658650
if (response) {

0 commit comments

Comments
 (0)