From 2af4ecb9dee27449772f9f4990d416f5ca00f2b5 Mon Sep 17 00:00:00 2001 From: Niklas Vanhainen Date: Sat, 9 Nov 2024 14:31:07 +0100 Subject: [PATCH 1/2] Fix off-by-one error in importer error messages when first row is header --- .../elements/ImportMessageList/ImportMessage.tsx | 6 ++---- .../elements/ImportMessageList/ImportMessageItem.tsx | 8 ++++---- src/features/import/hooks/usePreflight.ts | 7 +++++++ src/features/import/utils/problems/types.ts | 2 ++ 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/features/import/components/ImportDialog/elements/ImportMessageList/ImportMessage.tsx b/src/features/import/components/ImportDialog/elements/ImportMessageList/ImportMessage.tsx index f3f8bab030..51d6e0a9a3 100644 --- a/src/features/import/components/ImportDialog/elements/ImportMessageList/ImportMessage.tsx +++ b/src/features/import/components/ImportDialog/elements/ImportMessageList/ImportMessage.tsx @@ -17,7 +17,7 @@ type Props = { description?: string; onCheck?: (checked: boolean) => void; onClickBack?: () => void; - rowIndices?: number[]; + rowNumbers?: number[]; status: 'error' | 'info' | 'success' | 'warning'; title: string; }; @@ -26,14 +26,12 @@ const ImportMessage: FC = ({ description, onCheck, onClickBack, - rowIndices, + rowNumbers, status, title, }) => { const messages = useMessages(messageIds); - const rowNumbers = rowIndices?.map((rowIndex) => rowIndex + 1); - return ( = ({ onCheck, onClickBack, problem }) => { = ({ onCheck, onClickBack, problem }) => { description={messages.preflight.messages.missingIdAndName.description()} onCheck={onCheck} onClickBack={onClickBack} - rowIndices={problem.indices} + rowNumbers={problem.rows} status="error" title={messages.preflight.messages.missingIdAndName.title()} /> @@ -103,7 +103,7 @@ const ImportMessageItem: FC = ({ onCheck, onClickBack, problem }) => { description={messages.preflight.messages.unknownPerson.description()} onCheck={onCheck} onClickBack={onClickBack} - rowIndices={problem.indices} + rowNumbers={problem.rows} status="error" title={messages.preflight.messages.unknownPerson.title()} /> @@ -114,7 +114,7 @@ const ImportMessageItem: FC = ({ onCheck, onClickBack, problem }) => { description={messages.preflight.messages.unknownError.description()} onCheck={onCheck} onClickBack={onClickBack} - rowIndices={problem.indices} + rowNumbers={problem.rows} status="error" title={messages.preflight.messages.unknownError.title()} /> diff --git a/src/features/import/hooks/usePreflight.ts b/src/features/import/hooks/usePreflight.ts index 9f6b4935e6..71e130ac0e 100644 --- a/src/features/import/hooks/usePreflight.ts +++ b/src/features/import/hooks/usePreflight.ts @@ -59,6 +59,13 @@ export default function usePreflight(orgId: number) { ); } + const rowModifier = sheet.firstRowIsHeaders ? 2 : 1; + problems.map((problem) => { + if ('indices' in problem) { + problem.rows = problem.indices.map((index) => index + rowModifier); + } + }); + const hasError = problems.some( (problem) => levelForProblem(problem) == 'error' ); diff --git a/src/features/import/utils/problems/types.ts b/src/features/import/utils/problems/types.ts index 375bd7c9e3..9e6f1fddea 100644 --- a/src/features/import/utils/problems/types.ts +++ b/src/features/import/utils/problems/types.ts @@ -14,6 +14,7 @@ export type ImportFieldProblem = { field: string; indices: number[]; kind: ImportProblemKind.INVALID_FORMAT; + rows?: number[]; }; export type ImportFieldMetaProblem = { @@ -29,6 +30,7 @@ export type ImportRowProblem = { | ImportProblemKind.UNEXPECTED_ERROR | ImportProblemKind.UNKNOWN_PERSON | ImportProblemKind.UNKNOWN_ERROR; + rows?: number[]; }; export type ImportSheetProblem = { From fff5a2a87446a7dcd921d3be733ec898246f7b4b Mon Sep 17 00:00:00 2001 From: Niklas Vanhainen Date: Sat, 9 Nov 2024 21:55:15 +0100 Subject: [PATCH 2/2] map should be forEach --- src/features/import/hooks/usePreflight.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/features/import/hooks/usePreflight.ts b/src/features/import/hooks/usePreflight.ts index 71e130ac0e..5be4491e07 100644 --- a/src/features/import/hooks/usePreflight.ts +++ b/src/features/import/hooks/usePreflight.ts @@ -60,10 +60,11 @@ export default function usePreflight(orgId: number) { } const rowModifier = sheet.firstRowIsHeaders ? 2 : 1; - problems.map((problem) => { + problems.forEach((problem) => { if ('indices' in problem) { problem.rows = problem.indices.map((index) => index + rowModifier); } + return problem; }); const hasError = problems.some(