From 24aee1db90a4b89510a59cf36d2b903d88245a17 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 23 Sep 2025 11:46:46 -0400 Subject: [PATCH] Update product import output json --- inventory/src/pages/Import.tsx | 85 ++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 9 deletions(-) diff --git a/inventory/src/pages/Import.tsx b/inventory/src/pages/Import.tsx index 4d87c1c..6680185 100644 --- a/inventory/src/pages/Import.tsx +++ b/inventory/src/pages/Import.tsx @@ -8,6 +8,7 @@ import { motion } from "framer-motion"; import { useQuery } from "@tanstack/react-query"; import config from "@/config"; import { Loader2 } from "lucide-react"; +import type { DataValue, FieldType, Result } from "@/components/product-import/types"; // Define base fields without dynamic options const BASE_IMPORT_FIELDS = [ { @@ -343,7 +344,11 @@ const BASE_IMPORT_FIELDS = [ export function Import() { const [isOpen, setIsOpen] = useState(false); - const [importedData, setImportedData] = useState(null); + type ImportFieldKey = (typeof BASE_IMPORT_FIELDS)[number]["key"]; + type NormalizedProduct = Record; + type ImportResult = Result & { all?: Result["validData"] }; + + const [importedData, setImportedData] = useState(null); const [selectedCompany, setSelectedCompany] = useState(null); const [selectedLine, setSelectedLine] = useState(null); const [startFromScratch, setStartFromScratch] = useState(false); @@ -390,7 +395,6 @@ export function Import() { // Handle field value changes const handleFieldChange = (field: string, value: any) => { - console.log('Field change:', field, value); if (field === "company") { setSelectedCompany(value); setSelectedLine(null); // Reset line when company changes @@ -412,7 +416,6 @@ export function Import() { options: fieldOptions.companies || [], }, onChange: (value: string) => { - console.log('Company selected:', value); handleFieldChange("company", value); }, }; @@ -424,7 +427,6 @@ export function Import() { options: productLines || [], }, onChange: (value: string) => { - console.log('Line selected:', value); handleFieldChange("line", value); }, disabled: !selectedCompany, @@ -507,11 +509,76 @@ export function Import() { } }); - const handleData = async (data: any, file: File) => { + const getDefaultValue = (fieldType: FieldType): string | string[] | boolean | null => { + switch (fieldType.type) { + case "multi-select": + case "multi-input": + return []; + case "checkbox": + return false; + default: + return null; + } + }; + + const normalizeValue = (value: DataValue, fieldType: FieldType): string | string[] | boolean | null => { + if (value === undefined || value === null || value === "") { + return getDefaultValue(fieldType); + } + + if (fieldType.type === "multi-select" || fieldType.type === "multi-input") { + if (Array.isArray(value)) { + return value.length ? value : []; + } + + if (typeof value === "string") { + const separator = fieldType.separator ?? ","; + return value + .split(separator) + .map((part) => part.trim()) + .filter(Boolean); + } + + return []; + } + + if (Array.isArray(value)) { + return value.length ? value : []; + } + + if (typeof value === "boolean") { + return value; + } + + return String(value); + }; + + const handleData = async (data: ImportResult, _file: File) => { try { - console.log("Imported Data:", data); - console.log("File:", file); - setImportedData(data); + const rows = (data.all?.length ? data.all : data.validData) ?? []; + const formattedRows: NormalizedProduct[] = rows.map((row) => { + const baseValues = importFields.reduce((acc, field) => { + const rawRow = row as Record; + acc[field.key as ImportFieldKey] = normalizeValue(rawRow[field.key], field.fieldType); + return acc; + }, {} as Record); + + const rawProductImages = (row as Record).product_images; + let normalizedProductImages: string | string[] | boolean | null = null; + + if (Array.isArray(rawProductImages)) { + normalizedProductImages = rawProductImages; + } else if (typeof rawProductImages === "string" && rawProductImages.trim().length > 0) { + normalizedProductImages = rawProductImages; + } + + return { + ...baseValues, + product_images: normalizedProductImages, + } as NormalizedProduct; + }); + + setImportedData(formattedRows); setIsOpen(false); toast.success("Data imported successfully"); } catch (error) { @@ -587,4 +654,4 @@ export function Import() { /> ); -} \ No newline at end of file +}