Skip to content

Commit

Permalink
Merge pull request #151 from afadil/develop
Browse files Browse the repository at this point in the history
small fixes
  • Loading branch information
afadil authored Oct 27, 2024
2 parents 6326ee2 + 1d9f008 commit 785a6a2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
77 changes: 44 additions & 33 deletions src/pages/activity/import/hooks/useCsvParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useCallback } from 'react';
import Papa from 'papaparse';
import { ImportFormat } from '@/lib/types';
import { ImportFormat, ImportMappingData } from '@/lib/types';

Check failure on line 3 in src/pages/activity/import/hooks/useCsvParser.ts

View workflow job for this annotation

GitHub Actions / release (macos-latest, --target aarch64-apple-darwin)

'ImportFormat' is declared but its value is never read.

Check failure on line 3 in src/pages/activity/import/hooks/useCsvParser.ts

View workflow job for this annotation

GitHub Actions / release (macos-latest, --target x86_64-apple-darwin)

'ImportFormat' is declared but its value is never read.

Check failure on line 3 in src/pages/activity/import/hooks/useCsvParser.ts

View workflow job for this annotation

GitHub Actions / release (ubuntu-22.04)

'ImportFormat' is declared but its value is never read.

Check failure on line 3 in src/pages/activity/import/hooks/useCsvParser.ts

View workflow job for this annotation

GitHub Actions / release (windows-latest)

'ImportFormat' is declared but its value is never read.
import { validateCsvStructure, initializeColumnMapping } from '../utils/csvValidation';

export function useCsvParser() {
Expand All @@ -21,44 +21,55 @@ export function useCsvParser() {
setValidationErrors({});
};

const parseCsvFile = useCallback((file: File, form: any) => {
resetFileStates();
setSelectedFile(file);
const parseCsvFile = useCallback(
(
file: File,
mapping: ImportMappingData,
onUpdateMapping?: (updates: Partial<ImportMappingData>) => void,
) => {
resetFileStates();
setSelectedFile(file);

Papa.parse(file, {
complete: (results: Papa.ParseResult<string[]>) => {
if (results.data && results.data.length > 0) {
setCsvData(results.data);
const headerRow = results.data[0].map((header) => header.trim());
setHeaders(headerRow);
Papa.parse<string[]>(file, {
complete: (results) => {
if (results.data && results.data.length > 0) {
setCsvData(results.data);
const headerRow = results.data[0].map((header) => header.trim());
setHeaders(headerRow);

const isValid = validateCsvStructure(headerRow);
const isValid = validateCsvStructure(headerRow);

if (!isValid) {
setIsValidCsv(false);
setError(
"Oops! The CSV file structure doesn't look quite right. Please make sure your file starts with a header row containing multiple column names.",
);
if (!isValid) {
setIsValidCsv(false);
setError(
"Oops! The CSV file structure doesn't look quite right. Please make sure your file starts with a header row containing multiple column names.",
);
} else {
const initialMapping = initializeColumnMapping(headerRow);
if (onUpdateMapping) {
onUpdateMapping({
fieldMappings: {
...mapping.fieldMappings,
...initialMapping,
},
});
}
}
} else {
const initialMapping = initializeColumnMapping(headerRow);
form.setValue('mapping.columns', {
...form.getValues('mapping.columns'),
...initialMapping,
} as Record<ImportFormat, string>);
setIsValidCsv(false);
setError('The CSV file appears to be empty.');
}
} else {
setIsLoading(false);
},
error: (error: any) => {
setIsValidCsv(false);
setError('The CSV file appears to be empty.');
}
setIsLoading(false);
},
error: (error: any) => {
setIsValidCsv(false);
setError(`Error parsing CSV: ${error.message}`);
setIsLoading(false);
},
});
}, []);
setError(`Error parsing CSV: ${error.message}`);
setIsLoading(false);
},
});
},
[],
);

return {
csvData,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/activity/import/import-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ export function ActivityImportForm({
(acceptedFiles: File[]) => {
const file = acceptedFiles[0];
resetFileStates();
parseCsvFile(file, mapping);
parseCsvFile(file, mapping, updateMapping);
setAccordionValue('');
},
[parseCsvFile, mapping, resetFileStates],
[parseCsvFile, mapping, resetFileStates, updateMapping],
);

const { saveAndCheckImportMutation } = useActivityImportMutations({
Expand Down
2 changes: 1 addition & 1 deletion src/pages/activity/import/utils/csvValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,5 @@ export function isCashActivity(activityType: ActivityType): boolean {
}

export function validateTickerSymbol(symbol: string): boolean {
return tickerRegex.test(symbol);
return tickerRegex.test(symbol.trim());
}

0 comments on commit 785a6a2

Please sign in to comment.