Update product import output json
This commit is contained in:
@@ -8,6 +8,7 @@ import { motion } from "framer-motion";
|
|||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import config from "@/config";
|
import config from "@/config";
|
||||||
import { Loader2 } from "lucide-react";
|
import { Loader2 } from "lucide-react";
|
||||||
|
import type { DataValue, FieldType, Result } from "@/components/product-import/types";
|
||||||
// Define base fields without dynamic options
|
// Define base fields without dynamic options
|
||||||
const BASE_IMPORT_FIELDS = [
|
const BASE_IMPORT_FIELDS = [
|
||||||
{
|
{
|
||||||
@@ -343,7 +344,11 @@ const BASE_IMPORT_FIELDS = [
|
|||||||
|
|
||||||
export function Import() {
|
export function Import() {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [importedData, setImportedData] = useState<any[] | null>(null);
|
type ImportFieldKey = (typeof BASE_IMPORT_FIELDS)[number]["key"];
|
||||||
|
type NormalizedProduct = Record<ImportFieldKey | "product_images", string | string[] | boolean | null>;
|
||||||
|
type ImportResult = Result<string> & { all?: Result<string>["validData"] };
|
||||||
|
|
||||||
|
const [importedData, setImportedData] = useState<NormalizedProduct[] | null>(null);
|
||||||
const [selectedCompany, setSelectedCompany] = useState<string | null>(null);
|
const [selectedCompany, setSelectedCompany] = useState<string | null>(null);
|
||||||
const [selectedLine, setSelectedLine] = useState<string | null>(null);
|
const [selectedLine, setSelectedLine] = useState<string | null>(null);
|
||||||
const [startFromScratch, setStartFromScratch] = useState(false);
|
const [startFromScratch, setStartFromScratch] = useState(false);
|
||||||
@@ -390,7 +395,6 @@ export function Import() {
|
|||||||
|
|
||||||
// Handle field value changes
|
// Handle field value changes
|
||||||
const handleFieldChange = (field: string, value: any) => {
|
const handleFieldChange = (field: string, value: any) => {
|
||||||
console.log('Field change:', field, value);
|
|
||||||
if (field === "company") {
|
if (field === "company") {
|
||||||
setSelectedCompany(value);
|
setSelectedCompany(value);
|
||||||
setSelectedLine(null); // Reset line when company changes
|
setSelectedLine(null); // Reset line when company changes
|
||||||
@@ -412,7 +416,6 @@ export function Import() {
|
|||||||
options: fieldOptions.companies || [],
|
options: fieldOptions.companies || [],
|
||||||
},
|
},
|
||||||
onChange: (value: string) => {
|
onChange: (value: string) => {
|
||||||
console.log('Company selected:', value);
|
|
||||||
handleFieldChange("company", value);
|
handleFieldChange("company", value);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -424,7 +427,6 @@ export function Import() {
|
|||||||
options: productLines || [],
|
options: productLines || [],
|
||||||
},
|
},
|
||||||
onChange: (value: string) => {
|
onChange: (value: string) => {
|
||||||
console.log('Line selected:', value);
|
|
||||||
handleFieldChange("line", value);
|
handleFieldChange("line", value);
|
||||||
},
|
},
|
||||||
disabled: !selectedCompany,
|
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 {
|
try {
|
||||||
console.log("Imported Data:", data);
|
const rows = (data.all?.length ? data.all : data.validData) ?? [];
|
||||||
console.log("File:", file);
|
const formattedRows: NormalizedProduct[] = rows.map((row) => {
|
||||||
setImportedData(data);
|
const baseValues = importFields.reduce((acc, field) => {
|
||||||
|
const rawRow = row as Record<string, DataValue>;
|
||||||
|
acc[field.key as ImportFieldKey] = normalizeValue(rawRow[field.key], field.fieldType);
|
||||||
|
return acc;
|
||||||
|
}, {} as Record<ImportFieldKey, string | string[] | boolean | null>);
|
||||||
|
|
||||||
|
const rawProductImages = (row as Record<string, unknown>).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);
|
setIsOpen(false);
|
||||||
toast.success("Data imported successfully");
|
toast.success("Data imported successfully");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user