import { getPropertyInPath, PropertiesOrBuilders, Property } from "@firecms/core"; import { BooleanSwitchWithLabel, ChevronRightIcon, DateTimeField, ExpandablePanel, Select, SelectItem, Table, TableBody, TableCell, TableHeader, TableRow, TextField, Typography } from "@firecms/ui"; import { ImportConfig } from "../types"; import { getIn, setIn } from "@firecms/formex"; import { useTranslation } from "@firecms/core"; export interface DataPropertyMappingProps { importConfig: ImportConfig; destinationProperties: Record; buildPropertyView?: (props: { isIdColumn: boolean, property: Property | null, propertyKey: string | null, importKey: string }) => React.ReactNode; } export function DataNewPropertiesMapping({ importConfig, destinationProperties, buildPropertyView }: DataPropertyMappingProps) { const { t } = useTranslation(); const headersMapping = importConfig.headersMapping; const headingsOrder = importConfig.headingsOrder; const idColumn = importConfig.idColumn; const originProperties = importConfig.originProperties; return ( <> importConfig.setIdColumn(value ?? undefined)}/>
{t("column_in_file")} {t("map_to_property")} {destinationProperties && headingsOrder.map((importKey) => { const mappedKey = headersMapping[importKey]; const propertyKey = headersMapping[importKey]; const property = mappedKey ? getPropertyInPath(destinationProperties, mappedKey) as Property : null; const originProperty = getPropertyInPath(originProperties, importKey) as Property | undefined; const originDataType = originProperty ? (originProperty.dataType === "array" && typeof originProperty.of === "object" ? `${originProperty.dataType} - ${(originProperty.of as Property).dataType}` : originProperty.dataType) : undefined; return {importKey} {originProperty && {originDataType}} {buildPropertyView?.({ isIdColumn: importKey === idColumn, property, propertyKey, importKey })} ; } )}
{t("default_values_description")}
{t("property")} {t("default_value")} {destinationProperties && getAllPropertyKeys(destinationProperties).map((key) => { const property = getPropertyInPath(destinationProperties, key); if (typeof property !== "object" || property === null) { return null; } if (!["number", "string", "boolean", "map"].includes(property.dataType)) { return null; } return {key} { const newValues = setIn(importConfig.defaultValues, key, value); importConfig.setDefaultValues(newValues); }}/> ; } )}
); } function getAllPropertyKeys(properties: PropertiesOrBuilders, currentKey?: string): string[] { return Object.entries(properties).reduce((acc, [key, property]) => { const accumulatedKey = currentKey ? `${currentKey}.${key}` : key; if (typeof property !== "function" && property.dataType === "map" && property.properties) { const childProperties = getAllPropertyKeys(property.properties, accumulatedKey); return [...acc, ...childProperties]; } return [...acc, accumulatedKey]; }, [] as string[]); } function IdSelectField({ idColumn, headersMapping, onChange }: { idColumn?: string, headersMapping: Record; onChange: (value: string | null) => void }) { const { t } = useTranslation(); return
; } function DefaultValuesField({ property, onValueChange, defaultValue }: { property: Property, onValueChange: (value: any) => void, defaultValue?: any }) { const { t } = useTranslation(); if (property.dataType === "string") { return onValueChange(event.target.value)}/>; } else if (property.dataType === "number") { return onValueChange(event.target.value)}/>; } else if (property.dataType === "boolean") { return onValueChange(v === null ? undefined : v)} label={defaultValue === undefined ? t("do_not_set_value") : defaultValue === true ? t("set_value_to_true") : t("set_value_to_false")} /> } else if (property.dataType === "date") { return { onValueChange(dateValue); }} clearable={true} /> } return null; }