Skip to content

Commit

Permalink
Merge pull request #1467 from AAFC-BICoE/35176-Allow-selecting-multip…
Browse files Browse the repository at this point in the history
…le-agents-on-workbook-upload

35176 allow selecting multiple agents on workbook upload
  • Loading branch information
brandonandre authored Nov 28, 2024
2 parents 7075f47 + 86e7346 commit fdca2ae
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
}
});
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Tooltip,
filterBy,
useApiClient,
useDinaFormContext,
useQuery
} from "../../../../common-ui/lib";
import { useDinaIntl } from "../../../intl/dina-ui-intl";
Expand All @@ -20,6 +21,7 @@ import FieldMappingConfig from "../utils/FieldMappingConfig";
import { useWorkbookConverter } from "../utils/useWorkbookConverter";
import {
FieldOptionType,
PERSON_SELECT_FIELDS,
WorkbookColumnInfo,
compareAlphanumeric,
findMatchField,
Expand All @@ -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();
Expand Down Expand Up @@ -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"]);
}
}
}
}
Expand All @@ -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 = "";
Expand Down Expand Up @@ -850,28 +861,51 @@ export function useColumnMapping() {

return (
<div className="d-flex">
<SelectField
className="flex-fill"
name={selectElemName}
options={options}
hideLabel={true}
isMulti={false}
selectProps={{
isClearable: true,
menuPortalTarget: document.body,
styles: {
menuPortal: (base) => ({ ...base, zIndex: 9999 })
}
}}
onChange={(newValue) => {
onChangeRelatedRecord(
columnHeader,
fieldValue,
newValue as string,
targetType
);
}}
/>
{PERSON_SELECT_FIELDS.has(fieldPath) ? (
<PersonSelectField
onChange={(newValue) => {
onChangeRelatedRecord(
columnHeader,
fieldValue,
(newValue as any)?.map((person) => person.id),
targetType
);
}}
className="flex-fill"
name={selectElemName}
hideLabel={true}
isMulti={true}
selectProps={{
isClearable: true,
menuPortalTarget: document.body,
styles: {
menuPortal: (base) => ({ ...base, zIndex: 9999 })
}
}}
/>
) : (
<SelectField
className="flex-fill"
name={selectElemName}
options={options}
hideLabel={true}
selectProps={{
isClearable: true,
menuPortalTarget: document.body,
styles: {
menuPortal: (base) => ({ ...base, zIndex: 9999 })
}
}}
onChange={(newValue) => {
onChangeRelatedRecord(
columnHeader,
fieldValue,
newValue as string,
targetType
);
}}
/>
)}
{showDuplicateWarningTooltip && (
<Tooltip
disableSpanMargin={true}
Expand Down
13 changes: 9 additions & 4 deletions packages/dina-ui/components/workbook/types/Workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,14 @@ export interface WorkbookColumnMap {

export interface RelationshipMapping {
[columnHeader: string]: {
[value: string]: {
id: string;
type: string;
};
[value: string]:
| {
id: string;
type: string;
}
| {
id: string;
type: string;
}[];
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FieldMappingConfigType,
getFlattenedConfig,
LinkOrCreateSetting,
PERSON_SELECT_FIELDS,
WorkbookColumnMap,
WorkbookDataTypeEnum
} from "..";
Expand Down Expand Up @@ -400,10 +401,12 @@ export function useWorkbookConverter(
) {
const attributeName = fieldPath.substring(fieldPath.lastIndexOf(".") + 1);
const value = resource[attributeName];

if (isEmptyWorkbookValue(value)) {
delete resource[attributeName];
return;
}

if (attributeName === "relationshipConfig") {
resource.type = value.type;
resource.relationships = {};
Expand Down Expand Up @@ -578,14 +581,17 @@ export function useWorkbookConverter(
columnMap[fieldPath + "." + attrNameInValue]?.[
childValue.trim().replace(".", "_")
];

if (valueToLink) {
break;
}
}
}
}
if (valueToLink) {
valuesForRelationship.push(valueToLink);
valuesForRelationship.push(
...(Array.isArray(valueToLink) ? valueToLink : [valueToLink])
);
} else {
if (
relationshipConfig.linkOrCreateSetting ===
Expand Down Expand Up @@ -648,15 +654,14 @@ export function useWorkbookConverter(
}
}
}
if (valuesForRelationship.length === value.length) {
if (!resource.relationships) {
resource.relationships = {};
}
if (!resource.relationships) {
resource.relationships = {};
}
if (valuesForRelationship.length) {
resource.relationships[attributeName] = {
data: valuesForRelationship
};
delete resource[attributeName];
return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,3 +900,7 @@ export function trimSpace(workbookData: WorkbookJSON) {
}
return workbookData;
}
export const PERSON_SELECT_FIELDS = new Set([
"preparedBy.displayName",
"collectingEvent.collectors.displayName"
]);
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,7 @@ describe("collecting-event edit page", () => {
expect(wrapper.getByRole("button", { name: /primary/i })).toBeDisabled();

// Add a second assertion:
userEvent.click(
wrapper.getByRole("button", {
name: /add another georeference assertion/i
})
);

userEvent.click(wrapper.getByTestId("add-another-button"));
await new Promise(setImmediate);

// Make 2nd assertion primary:
Expand Down

0 comments on commit fdca2ae

Please sign in to comment.