Skip to content

Commit

Permalink
fix(import): bug on empty first line (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
renaudAmsellem authored Aug 29, 2024
1 parent 44720ac commit 7c2bdc0
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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;
}
Expand Down

0 comments on commit 7c2bdc0

Please sign in to comment.