Skip to content

Commit

Permalink
Merge pull request #1452 from AAFC-BICoE/35135-Import-Workbook-append…
Browse files Browse the repository at this point in the history
…-data-replacing-instead-of-appending

35135 Import Workbook append data replacing instead of appending
  • Loading branch information
brandonandre authored Oct 31, 2024
2 parents 057301d + bffc5d5 commit 13efc32
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions packages/dina-ui/components/workbook/SaveWorkbookProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function SaveWorkbookProgress({
const userSelectedSameNameExistingResource = useRef<any>(undefined);

const sameNameParentSamples = useRef<any[]>([]);
const userSelectedSameNameParentSamples = useRef<any>(undefined);
const userSelectedSameNameParentSample = useRef<any>(undefined);

const resourcesUpdatedCount = useRef<number>(0);

Expand Down Expand Up @@ -149,7 +149,8 @@ export function SaveWorkbookProgress({
{
filter: {
rsql: `materialSampleName=="${resource?.materialSampleName}";group==${group}`
}
},
include: "attachment"
}
);

Expand Down Expand Up @@ -188,6 +189,7 @@ export function SaveWorkbookProgress({
// Else Only one resource with matching name, append data to resource
if (resp.data[0]) {
resource.id = resp.data[0].id;
userSelectedSameNameExistingResource.current = resp.data[0];

// Update count of existing resources updated for final confirmation screen
resourcesUpdatedCount.current =
Expand All @@ -201,9 +203,6 @@ export function SaveWorkbookProgress({

// Update count of existing resources updated for final confirmation screen
resourcesUpdatedCount.current = resourcesUpdatedCount.current + 1;

// Reset user selected resource to undefined
userSelectedSameNameExistingResource.current = undefined;
}

// Handle checking parent samples with same name logic
Expand All @@ -212,7 +211,7 @@ export function SaveWorkbookProgress({

if (parentSampleName) {
// There was no parent sample cached from user clicking the Select button from table
if (!userSelectedSameNameParentSamples.current) {
if (!userSelectedSameNameParentSample.current) {
for (const columnMapping of Object.values(workbookColumnMap)) {
if (
columnMapping.fieldPath ===
Expand Down Expand Up @@ -272,13 +271,11 @@ export function SaveWorkbookProgress({
"parentMaterialSample.materialSampleName"
) {
columnMapping.valueMapping[parentSampleName] = {
id: userSelectedSameNameParentSamples.current.id,
type: userSelectedSameNameParentSamples.current.type
id: userSelectedSameNameParentSample.current.id,
type: userSelectedSameNameParentSample.current.type
};
}
}
// Reset user selected resource to undefined for next
userSelectedSameNameParentSamples.current = undefined;
}
}

Expand All @@ -289,7 +286,14 @@ export function SaveWorkbookProgress({
key,
group ?? ""
);
appendDataToArrayField(key, resource);
}

// Reset user selected resource to undefined
userSelectedSameNameExistingResource.current = undefined;

// Reset user selected resource to undefined for next
userSelectedSameNameParentSample.current = undefined;
}

const savedArgs = await save(
Expand Down Expand Up @@ -336,6 +340,26 @@ export function SaveWorkbookProgress({
saveProgress(workbookResources.length);
finishUpload(sourceSet.current);
}

// Append data to resource[key] field if field is array
function appendDataToArrayField(key: string, resource: any) {
if (
userSelectedSameNameExistingResource.current &&
Array.isArray(userSelectedSameNameExistingResource.current[key])
) {
if (resource[key]) {
resource[key] = [
...resource[key],
...userSelectedSameNameExistingResource.current[key]
];
} else if (resource.relationships?.[key]) {
resource.relationships[key].data = [
...resource.relationships?.[key].data,
...userSelectedSameNameExistingResource.current[key]
];
}
}
}
}

function pause() {
Expand Down Expand Up @@ -413,7 +437,7 @@ export function SaveWorkbookProgress({
if (sameNameExistingResources.current.length > 0) {
userSelectedSameNameExistingResource.current = original;
} else if (sameNameParentSamples.current.length > 0) {
userSelectedSameNameParentSamples.current = original;
userSelectedSameNameParentSample.current = original;
}
statusRef.current = "SAVING";
resumeSavingWorkbook();
Expand Down Expand Up @@ -488,6 +512,7 @@ export function SaveWorkbookProgress({
path={"collection-api/material-sample"}
columns={multipleMatchingResourcesColumns}
defaultSort={[{ desc: true, id: "createdOn" }]}
include="attachment"
/>
</div>
) : sameNameParentSamples.current.length > 0 ? (
Expand Down

0 comments on commit 13efc32

Please sign in to comment.