diff --git a/resources/js/components/transactions/import-step-mapping.tsx b/resources/js/components/transactions/import-step-mapping.tsx index 7d6d0e30..81ad185b 100644 --- a/resources/js/components/transactions/import-step-mapping.tsx +++ b/resources/js/components/transactions/import-step-mapping.tsx @@ -23,7 +23,7 @@ interface ImportStepMappingProps { dateFormatDetected: boolean; parsedData: ParsedRow[]; currencyCode: string; - onMappingChange: (field: keyof ColumnMapping, value: string) => void; + onMappingChange: (field: keyof ColumnMapping, value: string | string[]) => void; onDateFormatChange: (format: DateFormat) => void; onNext: () => void; onBack: () => void; @@ -41,11 +41,61 @@ export function ImportStepMapping({ onNext, onBack, }: ImportStepMappingProps) { + const descriptionColumns = Array.isArray(columnMapping.description) + ? columnMapping.description + : columnMapping.description + ? [columnMapping.description] + : []; + const isValid = columnMapping.transaction_date && columnMapping.description && columnMapping.amount; + const getDescriptionFromRow = (row: ParsedRow): string => { + if (!columnMapping.description) { + return ''; + } + + const columns = Array.isArray(columnMapping.description) + ? columnMapping.description + : [columnMapping.description]; + + return columns + .map((col) => String(row[col] || '').trim()) + .filter((val) => val.length > 0) + .join('\n'); + }; + + const handleDescriptionChange = (index: number, value: string) => { + const newColumns = [...descriptionColumns]; + + if (value === '__none__') { + newColumns.splice(index, 1); + } else { + newColumns[index] = value; + } + + if (newColumns.length === 0) { + onMappingChange('description', ''); + } else if (newColumns.length === 1) { + onMappingChange('description', newColumns[0]); + } else { + onMappingChange('description', newColumns); + } + }; + + const addDescriptionColumn = () => { + if (descriptionColumns.length < 3) { + const newColumns = [...descriptionColumns, '']; + if (newColumns.length === 1) { + onMappingChange('description', ''); + } else { + onMappingChange('description', newColumns); + } + } + }; + const previewTransactions = parsedData.slice(0, 3).map((row) => { const date = columnMapping.transaction_date ? parseDate( @@ -53,9 +103,7 @@ export function ImportStepMapping({ dateFormat, ) : null; - const description = columnMapping.description - ? String(row[columnMapping.description] || '') - : ''; + const description = getDescriptionFromRow(row); const amount = columnMapping.amount ? parseAmount(row[columnMapping.amount] as string | number) : null; @@ -112,31 +160,89 @@ export function ImportStepMapping({