Skip to content

Commit

Permalink
fix: Refactor to remove bad setState im VriogramSettingSelect.
Browse files Browse the repository at this point in the history
  • Loading branch information
mheggelund committed Feb 6, 2024
1 parent e085da1 commit 6718eda
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 157 deletions.
3 changes: 2 additions & 1 deletion src/features/Compute/CaseGroup/CaseGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const CaseGroup = ({
runCase: (computeCaseId: string) => void;
}) => {
const [localList, setLocalList] = useState<ComputeCaseDto[]>([]);
const { data } = useFetchCases();
const { data, isLoading } = useFetchCases();
const { modelId } = useParams<{ modelId: string }>();
const { instance, accounts } = useMsal();
const token = useAccessToken(instance, accounts[0]);
Expand Down Expand Up @@ -325,6 +325,7 @@ export const CaseGroup = ({
}
}
};
if (isLoading) return <p>Loading ...</p>;

return (
<>
Expand Down
209 changes: 199 additions & 10 deletions src/features/Compute/CaseGroup/CaseRow/CaseRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CreateComputeCaseInputSettingsForm,
ListComputeCasesByAnalogueModelIdQueryResponse,
ListComputeSettingsDto,
ListComputeSettingsInputDto,
ListComputeSettingsInputValueDto,
ListComputeSettingsMethodDto,
ModelAreaDto,
Expand Down Expand Up @@ -77,7 +78,7 @@ export const CaseRow = ({
const [saved, setSaved] = useState<boolean>(true);
const [caseError, setCaseError] = useState<string>('');

const { data } = useFetchModel();
const { data, isLoading } = useFetchModel();

const channelSettings = settingsFilter('Object');
const variogramSettings = settingsFilter('Variogram');
Expand Down Expand Up @@ -422,6 +423,192 @@ export const CaseRow = ({
return methodList.length > 0;
};

const filterSettings = (
setting: ListComputeSettingsInputDto[] | undefined,
method: string,
) => {
return setting?.filter((value) => value.name === method);
};

const IndicatorSettings =
indicatorSettings && indicatorSettings[0].inputSettings;
const NetToGrossSettings =
netToGrossSettings && netToGrossSettings[0].inputSettings;
const ContiniusParameterSettings =
continiousParameterSettings && continiousParameterSettings[0].inputSettings;

const indicatorFamilySettings = filterSettings(
IndicatorSettings,
'Variogram Family Filter',
);

const indicatorIndicatorSettings = filterSettings(
IndicatorSettings,
'Indicator',
);
const NetGrossGrainSizeSettings = filterSettings(
NetToGrossSettings,
'Net-To-Gross',
);

const NetGrossVariogramFamilySettings = filterSettings(
NetToGrossSettings,
'Variogram Family Filter',
);

const contParamsVariogramFamilySettings = filterSettings(
ContiniusParameterSettings,
'Variogram Family Filter',
);

const contParamsParamsSettings = filterSettings(
ContiniusParameterSettings,
'ContiniousParameter',
);

const contParamsArchelSettings = filterSettings(
ContiniusParameterSettings,
'Archel',
);

const selectedParamValue = useCallback(
(method: string) => {
let settingsValueList: ListComputeSettingsInputValueDto[] | undefined =
[];
let loadedParameters: ListComputeSettingsInputValueDto[] | undefined = [];

switch (method) {
case 'Indicator': {
if (indicatorIndicatorSettings)
settingsValueList = indicatorIndicatorSettings[0].values;
break;
}
case 'Variogram Family Filter': {
if (rowCase.computeMethod.name === 'Indicator') {
if (indicatorFamilySettings)
settingsValueList = indicatorFamilySettings[0].values;
} else if (rowCase.computeMethod.name === 'Net-To-Gross') {
if (NetGrossVariogramFamilySettings)
settingsValueList = NetGrossVariogramFamilySettings[0].values;
} else if (rowCase.computeMethod.name === 'ContiniousParameter') {
if (contParamsVariogramFamilySettings)
settingsValueList = contParamsVariogramFamilySettings[0].values;
}
break;
}
case 'Net-To-Gross': {
if (NetGrossGrainSizeSettings)
settingsValueList = NetGrossGrainSizeSettings[0].values;
break;
}

case 'ContiniousParameter': {
if (contParamsParamsSettings)
settingsValueList = contParamsParamsSettings[0].values;
break;
}
case 'Archel': {
if (contParamsArchelSettings)
settingsValueList = contParamsArchelSettings[0].values;
break;
}
}

loadedParameters =
settingsValueList &&
settingsValueList.filter((i) =>
rowCase.inputSettings?.find(
(s) => s.inputSettingValueId === i.inputSettingValueId,
),
);

return loadedParameters;
},
[
NetGrossGrainSizeSettings,
NetGrossVariogramFamilySettings,
contParamsArchelSettings,
contParamsParamsSettings,
contParamsVariogramFamilySettings,
indicatorFamilySettings,
indicatorIndicatorSettings,
rowCase.computeMethod.name,
rowCase.inputSettings,
],
);

const setIfLoadedValues = useCallback(
(method: string) => {
const loaded = selectedParamValue(method);
switch (method) {
case 'Indicator':
if (loaded)
getDefaultParameters(
loaded,
selectedIndicatorParameters,
setIndicatorParameters,
);
break;

case 'Variogram Family Filter':
if (loaded)
getDefaultParameters(
loaded,
selectedVariogramModels,
setVariogramModels,
);
break;

case 'Net-To-Gross':
if (loaded)
getDefaultParameters(loaded, selectedGrainSize, setGrainSize);
break;

case 'ContiniousParameter':
if (loaded)
getDefaultParameters(loaded, selectedParameters, setParameters);
break;

case 'Archel':
if (loaded)
getDefaultParameters(loaded, selectedArchelFilter, setArchelFilter);
break;
}
},
[
selectedArchelFilter,
selectedGrainSize,
selectedIndicatorParameters,
selectedParamValue,
selectedParameters,
selectedVariogramModels,
],
);

const getDefaultParameters = (
loadedParameters: ListComputeSettingsInputValueDto[],
selectedParameter: ListComputeSettingsInputValueDto[] | undefined,
setParameter: (
value: React.SetStateAction<
ListComputeSettingsInputValueDto[] | undefined
>,
) => void,
) => {
if (selectedParameter === undefined) {
setParameter(loadedParameters);
}
};

useEffect(() => {
setIfLoadedValues('Indicator');
setIfLoadedValues('Variogram Family Filter');
setIfLoadedValues('Net-To-Gross');
setIfLoadedValues('ContiniousParameter');
setIfLoadedValues('Archel');
}, [selectedParamValue, setIfLoadedValues]);

if (isLoading) return <p>Loading ...</p>;

return (
<Styled.Case className={id.length <= 3 ? 'local-case' : ''}>
<Styled.CaseRow
Expand All @@ -432,9 +619,8 @@ export const CaseRow = ({
rowCase={rowCase}
modelAreas={areaList ? areaList : []}
caseType={'Indicator'}
IndicatorSettings={
indicatorSettings && indicatorSettings[0].inputSettings
}
indicatorFamilySettings={indicatorFamilySettings}
indicatorIndicatorSettings={indicatorIndicatorSettings}
selectedModelArea={selectedRowArea(rowCase.computeCaseId)}
selectedIndicatorParameters={selectedIndicatorParameters}
selectedVariogramModels={selectedVariogramModels}
Expand All @@ -444,6 +630,7 @@ export const CaseRow = ({
existingCases={caseList}
saved={saved}
caseError={caseError}
selectedParamValue={selectedParamValue}
/>
)}

Expand All @@ -452,9 +639,8 @@ export const CaseRow = ({
rowCase={rowCase}
modelAreas={areaList ? areaList : []}
caseType={'Net-To-Gross'}
NetToGrossSettings={
netToGrossSettings && netToGrossSettings[0].inputSettings
}
NetGrossGrainSizeSettings={NetGrossGrainSizeSettings}
NetGrossVariogramFamilySettings={NetGrossVariogramFamilySettings}
selectedModelArea={selectedRowArea(rowCase.computeCaseId)}
selectedGrainSize={selectedGrainSize}
selectedVariogramModels={selectedVariogramModels}
Expand All @@ -464,6 +650,7 @@ export const CaseRow = ({
existingCases={caseList}
saved={saved}
caseError={caseError}
selectedParamValue={selectedParamValue}
/>
)}

Expand All @@ -472,10 +659,11 @@ export const CaseRow = ({
rowCase={rowCase}
modelAreas={areaList ? areaList : []}
caseType={'ContiniousParameter'}
ContiniusParameterSettings={
continiousParameterSettings &&
continiousParameterSettings[0].inputSettings
contParamsVariogramFamilySettings={
contParamsVariogramFamilySettings
}
contParamsParamsSettings={contParamsParamsSettings}
contParamsArchelSettings={contParamsArchelSettings}
selectedModelArea={selectedRowArea(rowCase.computeCaseId)}
selectedParameters={selectedParameters}
selectedArchelFilter={selectedArchelFilter}
Expand All @@ -487,6 +675,7 @@ export const CaseRow = ({
existingCases={caseList}
saved={saved}
caseError={caseError}
selectedParamValue={selectedParamValue}
/>
)}

Expand Down
Loading

0 comments on commit 6718eda

Please sign in to comment.