Skip to content

Commit

Permalink
fix: clean mix table data (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
craigyu authored Nov 15, 2023
1 parent 284f3a6 commit 449f5b5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const renderTableCell = (
<TextInput
labelText=""
hideLabel
invalidText=""
type="number"
placeholder="Add value"
value={rowData[header.id]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { getAllParentTrees } from '../../../api-service/orchardAPI';
import MultiOptionsObj from '../../../types/MultiOptionsObject';
import DescriptionBox from '../../DescriptionBox';
import InfoSection from '../../InfoSection';
import { ParentTreeGeneticQualityType } from '../../../types/ParentTreeGeneticQualityType';
import { ParentTreeStepDataObj } from '../../../views/Seedlot/SeedlotRegistrationForm/definitions';
import { postFile } from '../../../api-service/seedlotAPI';
import postForCalculation from '../../../api-service/geneticWorthAPI';
Expand Down Expand Up @@ -164,25 +163,20 @@ const ParentTreeStep = (
queryFn: () => (
getAllParentTrees(seedlotSpecies.code)
),
onSuccess: (data: ParentTreeGeneticQualityType[]) => processParentTreeData(
data,
state,
orchardsData.map((o) => o.selectedItem?.code),
currentPage,
currPageSize,
setSlicedRows,
setStepData
),
staleTime: 3 * (60 * 60 * 1000), // will not refetch for 3 hours
cacheTime: 3.5 * (60 * 60 * 1000) // data is cached 3.5 hours then deleted
});

// Re-populate table if it is emptied by users and data is cached
/**
* Populate table when data is first fetched
* Re-populate table if it is emptied by users and data is cached
*/
useEffect(() => {
const disabled = processOrchards(orchards).length === 0;
if (
!disabled
&& Object.keys(state.tableRowData).length === 0
&& allParentTreeQuery.isFetched
&& allParentTreeQuery.data
) {
processParentTreeData(
Expand All @@ -195,7 +189,7 @@ const ParentTreeStep = (
setStepData
);
}
}, [state.tableRowData]);
}, [state.tableRowData, allParentTreeQuery.isFetched]);

useEffect(() => configHeaderOpt(
geneticWorthDict,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { OrchardObj } from '../OrchardStep/definitions';
import {
RowItem, InfoSectionConfigType, RowDataDictType,
HeaderObj, TabTypes, CompUploadResponse, GeneticWorthDictType, MixUploadResponse
} from './definitions';
import { EMPTY_NUMBER_STRING, rowTemplate } from './constants';
import InfoDisplayObj from '../../../types/InfoDisplayObj';
import { sliceTableRowData } from '../../../utils/PaginationUtils';
import { ParentTreeStepDataObj } from '../../../views/Seedlot/SeedlotRegistrationForm/definitions';
import { ParentTreeGeneticQualityType } from '../../../types/ParentTreeGeneticQualityType';
import MultiOptionsObj from '../../../types/MultiOptionsObject';
import { recordKeys } from '../../../utils/RecordUtils';
import { generateDefaultRows } from '../../../views/Seedlot/SeedlotRegistrationForm/utils';
import { GenWorthCalcPayload, CalcPayloadResType } from '../../../types/GeneticWorthTypes';

import { isPtNumberInvalid, populateRowData } from './TableComponents/utils';
import { OrchardObj } from '../OrchardStep/definitions';

import {
RowItem, InfoSectionConfigType, RowDataDictType,
HeaderObj, TabTypes, CompUploadResponse, GeneticWorthDictType, MixUploadResponse
} from './definitions';
import { EMPTY_NUMBER_STRING, rowTemplate } from './constants';

export const getTabString = (selectedIndex: number) => {
switch (selectedIndex) {
Expand Down Expand Up @@ -230,17 +233,21 @@ export const cleanTable = (
headerConfig: HeaderObj[],
currentTab: keyof TabTypes,
setStepData: Function
) => {
): ParentTreeStepDataObj => {
const clonedState = structuredClone(state);
const fieldsToClean = headerConfig
.filter((header) => header.editable && header.availableInTabs.includes(currentTab))
.map((header) => header.id);
const parentTreeNumbers = Object.keys(clonedState.tableRowData);
parentTreeNumbers.forEach((parentTreeNumber) => {
fieldsToClean.forEach((field) => {
clonedState.tableRowData[parentTreeNumber][field] = '';
if (currentTab === 'mixTab') {
clonedState.mixTabData = generateDefaultRows();
} else {
const fieldsToClean = headerConfig
.filter((header) => header.editable && header.availableInTabs.includes(currentTab))
.map((header) => header.id);
const parentTreeNumbers = Object.keys(clonedState.tableRowData);
parentTreeNumbers.forEach((parentTreeNumber) => {
fieldsToClean.forEach((field) => {
clonedState.tableRowData[parentTreeNumber][field] = '';
});
});
});
}
setStepData(clonedState);
return clonedState;
};
Expand Down

0 comments on commit 449f5b5

Please sign in to comment.