Skip to content

Commit

Permalink
fix: use effect bug and better info for users (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
craigyu authored Feb 8, 2024
1 parent 00212c8 commit 2a74a8f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ const getPageText = () => ({
secondaryButtonText: 'Cancel'
},
emptySection: {
title: 'Nothing to show yet!'
title: 'Nothing to show yet!',
emptyOrchard: 'Empty orchard(s)!'
},
invalidPTNumberMsg: 'One or more of the parent tree numbers entered are invalid because these numbers might not exist within your orchard composition.',
errNotifEndMsg: 'Please review your entries and remember to check all pages.',
Expand Down Expand Up @@ -785,3 +786,9 @@ export const getEmptySectionDescription = (setStep: Function) => (
Please, fill the orchard ID to complete the cone and pollen table.
</span>
);

export const noParentTreeDescription = (
<span>
No parent tree found under selected orchard(s)
</span>
);
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import InfoSectionRow from '../../InfoSection/InfoSectionRow';
import {
pageText, headerTemplate, geneticWorthDict, SummarySectionConfig,
DEFAULT_PAGE_SIZE, DEFAULT_PAGE_NUMBER, DEFAULT_MIX_PAGE_SIZE,
PopSizeAndDiversityConfig, getDownloadUrl, fileConfigTemplate, getEmptySectionDescription
PopSizeAndDiversityConfig, getDownloadUrl, fileConfigTemplate,
getEmptySectionDescription, noParentTreeDescription
} from './constants';
import {
TabTypes, HeaderObj, RowItem
Expand All @@ -49,7 +50,8 @@ import {
getTabString, processOrchards, combineObjectValues, calcSummaryItems,
processParentTreeData, cleanTable, fillCompostitionTables, configHeaderOpt,
fillCalculatedInfo, generateGenWorthPayload, addNewMixRow, calcMixTabInfoItems,
fillMixTable
fillMixTable,
hasParentTreesForSelectedOrchards
} from './utils';

import './styles.scss';
Expand Down Expand Up @@ -102,6 +104,7 @@ const ParentTreeStep = () => {
const [applicableGenWorths, setApplicableGenWorths] = useState<string[]>([]);
// Array that stores invalid p.t. numbers uploaded from users from composition tabs
const [invalidPTNumbers, setInvalidPTNumbers] = useState<string[]>([]);
const [isOrchardEmpty, setIsOrchardEmpty] = useState<boolean>(false);

const emptySectionDescription = getEmptySectionDescription(setStep);

Expand Down Expand Up @@ -181,15 +184,23 @@ const ParentTreeStep = () => {
&& allParentTreeQuery.isFetched
&& allParentTreeQuery.data
) {
processParentTreeData(
allParentTreeQuery.data,
state,
orchardsData.map((o) => o.selectedItem?.code),
currentPage,
currPageSize,
setSlicedRows,
setStepData
);
const orchardIds = orchardsData.map((o) => o.selectedItem?.code);

if (hasParentTreesForSelectedOrchards(orchardIds, allParentTreeQuery.data)) {
setDisableOptions(false);
processParentTreeData(
allParentTreeQuery.data,
state,
orchardsData.map((o) => o.selectedItem?.code),
currentPage,
currPageSize,
setSlicedRows,
setStepData
);
} else {
setDisableOptions(true);
setIsOrchardEmpty(true);
}
}
}, [state.tableRowData, allParentTreeQuery.isFetched]);

Expand Down Expand Up @@ -452,7 +463,25 @@ const ParentTreeStep = () => {
}
{
disableOptions
? <EmptySection title={pageText.emptySection.title} description={emptySectionDescription} pictogram="CloudyWindy" />
? (
<EmptySection
title={
isOrchardEmpty
? pageText.emptySection.emptyOrchard
: pageText.emptySection.title
}
description={
isOrchardEmpty
? noParentTreeDescription
: emptySectionDescription
}
pictogram={
isOrchardEmpty
? 'Question'
: 'CloudyWindy'
}
/>
)
: renderPagination(
state,
currentTab,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ export const processParentTreeData = (
setStepData: Function
) => {
const modifiedState = { ...state };
const clonedAllData = structuredClone(state.allParentTreeData);
let clonedTableRowData: RowDataDictType = structuredClone(state.tableRowData);
const allParentTreeData = {};
let tableRowData: RowDataDictType = structuredClone(state.tableRowData);

data.forEach((parentTree) => {
Object.assign(clonedAllData, { [parentTree.parentTreeNumber]: parentTree });
Object.assign(allParentTreeData, { [parentTree.parentTreeNumber]: parentTree });
if (
!Object.prototype.hasOwnProperty.call(clonedTableRowData, parentTree.parentTreeNumber)
!Object.prototype.hasOwnProperty.call(tableRowData, parentTree.parentTreeNumber)
&& orchardIds.includes(parentTree.orchardId)
) {
const newRowData: RowItem = structuredClone(rowTemplate);
Expand All @@ -234,25 +234,52 @@ export const processParentTreeData = (
newRowData[genWorthName].value = String(singleGenWorthObj.geneticQualityValue);
}
});
clonedTableRowData = Object.assign(clonedTableRowData, {
tableRowData = Object.assign(tableRowData, {
[parentTree.parentTreeNumber]: populateStrInputId(parentTree.parentTreeNumber, newRowData)
});
}
});

modifiedState.tableRowData = clonedTableRowData;
modifiedState.allParentTreeData = clonedAllData;
modifiedState.tableRowData = tableRowData;
modifiedState.allParentTreeData = allParentTreeData;

sliceTableRowData(
Object.values(clonedTableRowData),
Object.values(tableRowData),
currentPage,
currPageSize,
false,
'parentTreeNumber',
setSlicedRows
);

// Only set data if tableRowData is not empty, otherwise a inf loop will occur.
setStepData('parentTreeStep', modifiedState);
};

/**
* Determines if selected orchards contains a least one parent tree.
*/
export const hasParentTreesForSelectedOrchards = (
orchardIds: (string | undefined)[],
data: ParentTreeGeneticQualityType[]
): boolean => {
const proceed = true;
const stop = false;
let hasParentTrees = false;

// Loop through every parent tree data obj
// and stop as soon as a tree is found with the matching orchard id.
data.every((parentTree) => {
if (orchardIds.includes(parentTree.orchardId)) {
hasParentTrees = true;
return stop;
}
return proceed;
});

return hasParentTrees;
};

export const getMixRowTemplate = (): RowItem => {
const newRow = structuredClone(rowTemplate);
newRow.isMixTab = true;
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/types/ParentTreeGeneticQualityType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export type SingleParentTreeGeneticObj = {
geneticQualityValue: number;
};

/**
* The type returned from get parent trees by species endpoint.
*/
export type ParentTreeGeneticQualityType = {
[key: string]: any;
parentTreeId: number;
Expand Down

0 comments on commit 2a74a8f

Please sign in to comment.