This commit is contained in:
j3d1 2026-07-23 05:17:44 +02:00
parent 796fef81be
commit 7df585d774
11 changed files with 246 additions and 72 deletions

View file

@ -218,6 +218,7 @@ export default {
isDragOver: false,
fileAnalysis: null,
detectedColumns: [],
parsedRows: [],
previewData: [],
columnMapping: {},
requiredFields: [
@ -239,11 +240,11 @@ export default {
},
mounted() {
// Load existing data if resuming
if (this.payload.uploaded_file) {
this.uploadedFile = this.payload.uploaded_file;
if (this.payload.file_name) {
this.uploadTime = this.payload.upload_time;
this.fileAnalysis = this.payload.file_analysis;
this.detectedColumns = this.payload.detected_columns || [];
this.parsedRows = this.payload.parsed_rows || [];
this.previewData = this.payload.preview_data || [];
this.columnMapping = this.payload.column_mapping || {};
}
@ -296,55 +297,66 @@ export default {
},
async analyzeFile(file) {
// This is a simplified version - in reality, you'd use a library like Papa Parse for CSV
// or SheetJS for Excel files
// Parsing happens entirely client-side. The backend never sees the
// raw file - only the structured `items` list produced here, sent
// later as generic payload/request body.
if (file.name.endsWith('.csv')) {
const text = await file.text();
const lines = text.split('\n').filter(line => line.trim());
if (lines.length > 0) {
// Parse CSV header
const headers = lines[0].split(',').map(h => h.trim().replace(/"/g, ''));
this.detectedColumns = headers;
// Parse preview data
this.previewData = lines.slice(1, 6).map(line => {
const values = line.split(',').map(v => v.trim().replace(/"/g, ''));
const row = {};
headers.forEach((header, index) => {
row[header] = values[index] || '';
});
return row;
});
this.fileAnalysis = {
totalRows: lines.length - 1, // Exclude header
totalColumns: headers.length,
validRows: lines.length - 1, // Simplified - assume all valid for demo
errorRows: 0
};
}
this.parsedRows = this.parseCsv(text);
} else {
// For Excel files, you'd use a library like SheetJS
// This is a mock implementation
// For Excel files, you'd use a library like SheetJS to parse
// client-side. This is a mock implementation.
this.detectedColumns = ['Name', 'Category', 'Quantity', 'Unit', 'Description'];
this.previewData = [
this.parsedRows = [
{ Name: 'Sample Item 1', Category: 'Tools', Quantity: '1', Unit: 'piece', Description: 'Sample description' },
{ Name: 'Sample Item 2', Category: 'Hardware', Quantity: '5', Unit: 'box', Description: 'Another sample' }
];
this.fileAnalysis = {
totalRows: 100,
totalColumns: 5,
validRows: 98,
errorRows: 2
};
}
this.previewData = this.parsedRows.slice(0, 5);
this.fileAnalysis = {
totalRows: this.parsedRows.length,
totalColumns: this.detectedColumns.length,
validRows: this.parsedRows.length,
errorRows: 0
};
// Auto-map columns based on common names
this.autoMapColumns();
},
// Simple CSV parser: no external dependency, entirely client-side.
// Returns an array of row objects keyed by the detected header columns.
parseCsv(text) {
const lines = text.split('\n').filter(line => line.trim());
if (lines.length === 0) {
this.detectedColumns = [];
return [];
}
const headers = lines[0].split(',').map(h => h.trim().replace(/"/g, ''));
this.detectedColumns = headers;
return lines.slice(1).map(line => {
const values = line.split(',').map(v => v.trim().replace(/"/g, ''));
const row = {};
headers.forEach((header, index) => {
row[header] = values[index] || '';
});
return row;
});
},
// Convert the fully parsed rows + column mapping into the generic item
// dicts consumed by the "Import Items" step, which creates each one via
// the standard generic InventoryItem create endpoint.
buildItemsFromMapping() {
return this.parsedRows.map(row => ({
name: this.columnMapping.name ? row[this.columnMapping.name] : '',
description: this.columnMapping.description ? row[this.columnMapping.description] : '',
category: this.columnMapping.category ? row[this.columnMapping.category] : '',
quantity: this.columnMapping.quantity ? row[this.columnMapping.quantity] : undefined,
})).filter(item => item.name);
},
autoMapColumns() {
const mapping = {};
@ -401,6 +413,7 @@ export default {
this.uploadTime = null;
this.fileAnalysis = null;
this.detectedColumns = [];
this.parsedRows = [];
this.previewData = [];
this.columnMapping = {};
this.updatePayload();
@ -429,12 +442,14 @@ export default {
updatePayload() {
this.$emit('update', {
uploaded_file: this.uploadedFile,
file_name: this.uploadedFile?.name || null,
upload_time: this.uploadTime,
file_analysis: this.fileAnalysis,
detected_columns: this.detectedColumns,
parsed_rows: this.parsedRows,
preview_data: this.previewData,
column_mapping: this.columnMapping
column_mapping: this.columnMapping,
items: this.buildItemsFromMapping()
});
},