Skip to content

Commit

Permalink
Merge pull request #1280 from AAFC-BICoE/33385-change-relationship-re…
Browse files Browse the repository at this point in the history
…solving-query

33385 change relationship resolving query
  • Loading branch information
brandonandre authored Apr 22, 2024
2 parents 31c3105 + bcf6f8b commit a658847
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export function WorkbookColumnMapping({
sheetOptions,
workbookColumnMap,
relationshipMapping,
resolveColumnMappingAndRelationshipMapping
resolveColumnMappingAndRelationshipMapping,
getResourceSelectField
} = useColumnMapping(
groupName,
sheet,
Expand Down Expand Up @@ -404,9 +405,9 @@ export function WorkbookColumnMapping({

/**
* When the dropdown value is changed in the relationship mapping section.
*
*
* This will update the relationship mapping to contain the new uuid values.
*
*
* @param columnHeader The spreadsheet column it's being mapped
* @param fieldValue The value in the spreadsheet that the related record is being mapped
* @param relatedRecord The UUID of the resource selected in the relationship mapping dropdown
Expand Down Expand Up @@ -540,8 +541,8 @@ export function WorkbookColumnMapping({

<RelationshipFieldMapping
sheetIndex={sheet}
groupName={groupName}
onChangeRelatedRecord={onRelatedRecordChange}
getResourceSelectField={getResourceSelectField}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@ import { useEffect, useMemo, useState } from "react";
import {
SelectField,
filterBy,
useAccount,
useApiClient,
useQuery
} from "../../../../common-ui/lib";
import { useDinaIntl } from "../../../intl/dina-ui-intl";
import {
CollectingEvent,
Collection,
ManagedAttribute,
MaterialSample,
PreparationMethod,
PreparationType,
Project,
Protocol,
StorageUnit
} from "../../../types/collection-api";
import { ManagedAttribute } from "../../../types/collection-api";
import { useWorkbookContext } from "../WorkbookProvider";
import {
LinkOrCreateSetting,
Expand Down Expand Up @@ -77,12 +66,6 @@ export function useColumnMapping(
return [];
}
}, [spreadsheetData]);
const { isAdmin } = useAccount();
const groupFilter = !isAdmin
? {
rsql: `group==${groupName}`
}
: undefined;

const [fieldOptions, setFieldOptions] = useState<FieldOptionType[]>([]);
const [fieldMap, setFieldMap] = useState<FieldMapType[]>([]);
Expand All @@ -104,32 +87,62 @@ export function useColumnMapping(

const { loading: collectionLoading, response: collectionResp } = useQuery<
RelationshipResource[]
>({
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=collection`
});
>(
{
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=collection`
},
{
deps: [groupName]
}
);
const { loading: preparationTypeLoading, response: preparationTypeResp } =
useQuery<RelationshipResource[]>({
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=preparation-type`
});
useQuery<RelationshipResource[]>(
{
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=preparation-type`
},
{
deps: [groupName]
}
);
const { loading: preparationMethodLoading, response: preparationMethodResp } =
useQuery<RelationshipResource[]>({
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=preparation-method`
});
useQuery<RelationshipResource[]>(
{
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=preparation-method`
},
{
deps: [groupName]
}
);
const { loading: protocolLoading, response: protocolResp } = useQuery<
RelationshipResource[]
>({
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=protocol`
});
>(
{
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=protocol`
},
{
deps: [groupName]
}
);
const { loading: storageUnitLoading, response: storageUnitResp } = useQuery<
RelationshipResource[]
>({
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=storage-unit`
});
>(
{
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=storage-unit`
},
{
deps: [groupName]
}
);
const { loading: projectLoading, response: projectResp } = useQuery<
RelationshipResource[]
>({
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=project`
});
>(
{
path: `collection-api/resource-name-identifier?filter[group][EQ]=${groupName}&filter[type][EQ]=project`
},
{
deps: [groupName]
}
);

const loadingData =
attrLoading ||
Expand All @@ -141,12 +154,29 @@ export function useColumnMapping(
projectLoading;

const managedAttributes = attrResp?.data || [];
const collections = collectionResp?.data || [];
const preparationTypes = preparationTypeResp?.data || [];
const preparationMethods = preparationMethodResp?.data || [];
const protocols = protocolResp?.data || [];
const storageUnits = storageUnitResp?.data || [];
const projects = projectResp?.data || [];
const collections = (collectionResp?.data || []).map((item) => ({
...item,
type: "collection"
}));
const preparationTypes = (preparationTypeResp?.data || []).map((item) => ({
...item,
type: "preparation-type"
}));
const preparationMethods = (preparationMethodResp?.data || []).map(
(item) => ({ ...item, type: "preparation-method" })
);
const protocols = (protocolResp?.data || []).map((item) => ({
...item,
type: "protocol"
}));
const storageUnits = (storageUnitResp?.data || []).map((item) => ({
...item,
type: "storage-unit"
}));
const projects = (projectResp?.data || []).map((item) => ({
...item,
type: "project"
}));

const [loading, setLoading] = useState<boolean>(loadingData);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
import { Card } from "react-bootstrap";
import { DinaMessage } from "../../../../dina-ui/intl/dina-ui-intl";
import { useWorkbookContext } from "../WorkbookProvider";
import { useColumnMapping } from "../column-mapping/useColumnMapping";
import { useEffect, useMemo } from "react";
import { useFormikContext } from "formik";
import { FieldMapType } from "../column-mapping/WorkbookColumnMapping";

export interface RelationshipFieldMappingProps {
sheetIndex: number;
onChangeRelatedRecord: (columnHeader: string, fieldValue: string, relatedRecord: string, targetType: string) => void;
groupName: string;
onChangeRelatedRecord: (
columnHeader: string,
fieldValue: string,
relatedRecord: string,
targetType: string
) => void;
getResourceSelectField: (
onChangeRelatedRecord: (
columnHeader: string,
fieldValue: string,
relatedRecord: string,
targetType: string
) => void,
columnHeader: string,
fieldPath?: string | undefined,
fieldValue?: string | undefined
) => JSX.Element | undefined;
}

export function RelationshipFieldMapping({
sheetIndex,
onChangeRelatedRecord,
groupName
getResourceSelectField
}: RelationshipFieldMappingProps) {
const { columnUniqueValues, type, workbookColumnMap, relationshipMapping } =
useWorkbookContext();
const selectedType = type ?? "material-sample";
const { getResourceSelectField } = useColumnMapping(
groupName,
sheetIndex,
selectedType
);

const { setValues, values } = useFormikContext();

Expand All @@ -38,9 +46,11 @@ export function RelationshipFieldMapping({

// Do not display skipped records on the relationship mapping section, this array contains the path and if it's skipped.
const skippedRecords = useMemo(() => {
const fieldMap = ((values as any)?.["fieldMap"] as FieldMapType[] | undefined);
const fieldMap = (values as any)?.["fieldMap"] as
| FieldMapType[]
| undefined;
if (fieldMap === undefined) return {};

return fieldMap.reduce((acc, record) => {
if (record.targetField) {
acc[record.targetField] = record.skipped;
Expand Down Expand Up @@ -87,7 +97,8 @@ export function RelationshipFieldMapping({
(columnName) =>
workbookColumnMap[columnName]?.mapRelationship &&
workbookColumnMap[columnName].showOnUI &&
skippedRecords[workbookColumnMap[columnName].fieldPath ?? ""] === false
skippedRecords[workbookColumnMap[columnName].fieldPath ?? ""] ===
false
)
.map((columnName, index1) => {
const thisColumnMap = workbookColumnMap[columnName]!;
Expand All @@ -102,7 +113,12 @@ export function RelationshipFieldMapping({
<div className="col-3">{fieldValue}</div>
<div className="col-3">{counts[fieldValue]}</div>
<div className="col-3">
{getResourceSelectField(onChangeRelatedRecord, columnName, fieldPath, fieldValue)}
{getResourceSelectField(
onChangeRelatedRecord,
columnName,
fieldPath,
fieldValue
)}
</div>
</div>
));
Expand Down

0 comments on commit a658847

Please sign in to comment.