diff --git a/packages/dina-ui/components/workbook/column-mapping/WorkbookColumnMapping.tsx b/packages/dina-ui/components/workbook/column-mapping/WorkbookColumnMapping.tsx index 3c75b81c1..1c6edefed 100644 --- a/packages/dina-ui/components/workbook/column-mapping/WorkbookColumnMapping.tsx +++ b/packages/dina-ui/components/workbook/column-mapping/WorkbookColumnMapping.tsx @@ -139,12 +139,20 @@ export function WorkbookColumnMapping({ const values = Object.keys( (columnUniqueValues ?? {})[sheet]?.[columnName] || {} ); - if (!relationshipMapping[columnName] && values.length > 0) { + + if (Array.isArray(relationshipMapping[columnName])) { + if (!relationshipMapping[columnName].length) { + unmappedColumnNames.push( + workbookColumnMap[columnName].originalColumnName + ); + } + } else if (!relationshipMapping[columnName] && values.length > 0) { unmappedColumnNames.push( workbookColumnMap[columnName].originalColumnName ); } else { const mappedValues = Object.keys(relationshipMapping[columnName] || {}); + for (const value of values) { if (mappedValues.indexOf(value.replace(".", "_")) === -1) { unmappedColumnNames.push( @@ -530,12 +538,11 @@ export function WorkbookColumnMapping({ async function onRelatedRecordChange( columnHeader: string, fieldValue: string, - relatedRecord: string, + relatedRecord: string | string[], targetType: string ) { const columnHeaderFormatted = columnHeader.replaceAll(".", "_"); const fieldValueFormatted = fieldValue.replaceAll(".", "_"); - if (relationshipMapping) { // Check if the dropdown option selected is undefined (was cleared) if (!relatedRecord) { @@ -546,16 +553,30 @@ export function WorkbookColumnMapping({ setRelationshipMapping(updatedMapping); } } else { - setRelationshipMapping({ - ...relationshipMapping, - [columnHeaderFormatted]: { - ...relationshipMapping?.[columnHeaderFormatted], - [fieldValueFormatted]: { - id: relatedRecord, - type: targetType + if (Array.isArray(relatedRecord)) { + const newValue = relatedRecord.map((id) => ({ + id, + type: targetType + })); + setRelationshipMapping({ + ...relationshipMapping, + [columnHeaderFormatted]: { + ...relationshipMapping?.[columnHeaderFormatted], + [fieldValueFormatted]: newValue } - } - }); + }); + } else { + setRelationshipMapping({ + ...relationshipMapping, + [columnHeaderFormatted]: { + ...relationshipMapping?.[columnHeaderFormatted], + [fieldValueFormatted]: { + id: relatedRecord, + type: targetType + } + } + }); + } } } } diff --git a/packages/dina-ui/components/workbook/column-mapping/useColumnMapping.tsx b/packages/dina-ui/components/workbook/column-mapping/useColumnMapping.tsx index aa5d929f9..039fd1e39 100644 --- a/packages/dina-ui/components/workbook/column-mapping/useColumnMapping.tsx +++ b/packages/dina-ui/components/workbook/column-mapping/useColumnMapping.tsx @@ -6,6 +6,7 @@ import { Tooltip, filterBy, useApiClient, + useDinaFormContext, useQuery } from "../../../../common-ui/lib"; import { useDinaIntl } from "../../../intl/dina-ui-intl"; @@ -20,6 +21,7 @@ import FieldMappingConfig from "../utils/FieldMappingConfig"; import { useWorkbookConverter } from "../utils/useWorkbookConverter"; import { FieldOptionType, + PERSON_SELECT_FIELDS, WorkbookColumnInfo, compareAlphanumeric, findMatchField, @@ -31,6 +33,7 @@ import { FieldMapType } from "./WorkbookColumnMapping"; import { Person } from "../../../types/agent-api/resources/Person"; import { FaExclamationTriangle } from "react-icons/fa"; import { ResourceNameIdentifier } from "../../../types/common/resources/ResourceNameIdentifier"; +import { PersonSelectField } from "../../resource-select-fields/resource-select-fields"; export function useColumnMapping() { const { formatMessage } = useDinaIntl(); @@ -721,13 +724,16 @@ export function useColumnMapping() { metadatas.find((item) => compareAlphanumeric(item.name, value)); break; } - // If relationship is found, set it. If not, reset it so it's empty. if (found) { - theRelationshipMapping[columnHeader][value.replace(".", "_")] = pick( - found, - ["id", "type"] - ); + if (PERSON_SELECT_FIELDS.has(fieldPath)) { + theRelationshipMapping[columnHeader][value.replace(".", "_")] = [ + pick(found, ["id", "type"]) + ]; + } else { + theRelationshipMapping[columnHeader][value.replace(".", "_")] = + pick(found, ["id", "type"]); + } } } } @@ -748,10 +754,15 @@ export function useColumnMapping() { return undefined; } - const selectElemName = `relationshipMapping.${columnHeader.replaceAll( - ".", - "_" - )}.${fieldValue.replaceAll(".", "_")}.id`; + const selectElemName = PERSON_SELECT_FIELDS.has(fieldPath) + ? `relationshipMapping.${columnHeader.replaceAll( + ".", + "_" + )}.${fieldValue.replaceAll(".", "_")}` + : `relationshipMapping.${columnHeader.replaceAll( + ".", + "_" + )}.${fieldValue.replaceAll(".", "_")}.id`; let options: any[] = []; let targetType: string = ""; @@ -850,28 +861,51 @@ export function useColumnMapping() { return (