From 7c2bdc0827d4fdbd546b71346f5311ee2edaefa2 Mon Sep 17 00:00:00 2001 From: renaudAmsellem Date: Thu, 29 Aug 2024 11:04:05 +0200 Subject: [PATCH] fix(import): bug on empty first line (#553) --- .../ImportModalSelectFileStep.tsx | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/libs/ui/src/components/ImportModal/ImportModalSelectFileStep/ImportModalSelectFileStep.tsx b/libs/ui/src/components/ImportModal/ImportModalSelectFileStep/ImportModalSelectFileStep.tsx index 047f185f1..4a746f1c5 100644 --- a/libs/ui/src/components/ImportModal/ImportModalSelectFileStep/ImportModalSelectFileStep.tsx +++ b/libs/ui/src/components/ImportModal/ImportModalSelectFileStep/ImportModalSelectFileStep.tsx @@ -3,7 +3,7 @@ // License text available at https://www.gnu.org/licenses/lgpl-3.0.txt import {extractArgsFromString} from '@leav/utils'; import {message, Space, Spin} from 'antd'; -import {KitAlert, KitUpload} from 'aristid-ds'; +import {KitAlert, KitUpload, useKitNotification} from 'aristid-ds'; import {IKitDragger} from 'aristid-ds/dist/Kit/DataEntry/Upload/types'; import {useState} from 'react'; import {read as xlsxRead, utils as xlsxUtils} from 'xlsx'; @@ -28,6 +28,7 @@ const defaultMode = ImportMode.upsert; function ImportModalSelectFileStep({onGetAttributes}: IImportModalSelectFileStepsProps): JSX.Element { const {t} = useSharedTranslation(); + const {kitNotification} = useKitNotification(); const {state, dispatch} = useImportReducerContext(); const {file} = state; @@ -57,7 +58,8 @@ function ImportModalSelectFileStep({onGetAttributes}: IImportModalSelectFileStep const firstRowAddresses = Object.keys(workbook.Sheets[sheetName]).filter( k => !!k.match(/\b[A-Z]+1\b/g) ); - const isMapped = !!workbook.Sheets[sheetName][firstRowAddresses[0]].c; + + const isMapped = !!workbook.Sheets[sheetName][firstRowAddresses[0]]?.c; // Mapping is present. if (isMapped) { @@ -179,11 +181,20 @@ function ImportModalSelectFileStep({onGetAttributes}: IImportModalSelectFileStep reader.readAsBinaryString(fileToImport); reader.onload = async e => { - const res = await _setFileData(reader.result); - if (res) { - dispatch({type: ImportReducerActionTypes.SET_FILE, file: fileToImport}); + try { + const res = await _setFileData(reader.result); + + if (res) { + dispatch({type: ImportReducerActionTypes.SET_FILE, file: fileToImport}); + } + dispatch({type: ImportReducerActionTypes.SET_OK_BTN, okBtn: res}); + } catch (error) { + const errorMessage = error?.message ?? t('error.error_occurred'); + kitNotification.error({ + message: t('error.error_occurred'), + description: errorMessage + }); } - dispatch({type: ImportReducerActionTypes.SET_OK_BTN, okBtn: res}); }; reader.onloadstart = e => { @@ -194,6 +205,13 @@ function ImportModalSelectFileStep({onGetAttributes}: IImportModalSelectFileStep setLoading(false); }; + reader.onerror = e => { + kitNotification.error({ + message: t('error.error_occurred'), + description: reader.error?.message ?? '' + }); + }; + // Prevent default upload. We'll handle it ourselves later on return false; }