Skip to content

Commit

Permalink
feat: Duplicate variogram case.
Browse files Browse the repository at this point in the history
  • Loading branch information
mheggelund committed Jan 30, 2024
1 parent dda0c50 commit 13db770
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/features/Compute/CaseGroup/CaseButtons/CaseButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ export const CaseButtons = ({
saved,
isProcessed,
caseStatus,
hasUnsavedCase,
saveCase,
runCase,
deleteCase,
setAlertMessage,
duplicateCase,
}: {
id: string;
caseType: string;
saved: boolean;
isProcessed?: boolean;
caseStatus: ComputeJobStatus;
hasUnsavedCase: boolean;
runCase?: () => void;
saveCase: () => void;
deleteCase: (
computeCaseId: string,
) => Promise<ListComputeCasesByAnalogueModelIdQueryResponse | undefined>;
setAlertMessage: (message: string) => void;
duplicateCase: () => void;
}) => {
const [deleteConfirm, setDeleteConfirm] = useState(false);
const [saveConfirm, setSaveConfirm] = useState(false);
Expand Down Expand Up @@ -72,17 +76,36 @@ export const CaseButtons = ({
)}

{caseType === 'Variogram' && (
<Tooltip title={'Functionality not implemented yet.'}>
<Button
disabled
variant="ghost_icon"
aria-label="duplicate"
// eslint-disable-next-line no-console
onClick={() => console.log('Duplicate')}
>
<Icon data={COPY} size={24}></Icon>
</Button>
</Tooltip>
<>
{id.length < 3 ? (
<Tooltip title={'Can not duplicate unsaved case.'}>
<Button disabled variant="ghost_icon" aria-label="duplicate">
<Icon data={COPY} size={24}></Icon>
</Button>
</Tooltip>
) : (
<>
{hasUnsavedCase ? (
<Tooltip
title={'Only one unsaved case per method at the time.'}
>
<Button disabled variant="ghost_icon" aria-label="duplicate">
<Icon data={COPY} size={24}></Icon>
</Button>
</Tooltip>
) : (
<Button
disabled={hasUnsavedCase}
variant="ghost_icon"
aria-label="duplicate"
onClick={() => duplicateCase()}
>
<Icon data={COPY} size={24}></Icon>
</Button>
)}
</>
)}
</>
)}

{caseType === 'Object' ? (
Expand Down
29 changes: 29 additions & 0 deletions src/features/Compute/CaseGroup/CaseGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { queryClient } from '../../../auth/queryClient';
import { CaseCardComponent } from '../../../components/CaseCardComponent/CaseCardComponent';
import { useAccessToken } from '../../../hooks/useAccessToken';
import { useFetchCases } from '../../../hooks/useFetchCases';
import * as Styled from './CaseGroup.styled';
import { CaseRow } from './CaseRow/CaseRow';

Expand All @@ -38,6 +39,7 @@ export const CaseGroup = ({
runCase: (computeCaseId: string) => void;
}) => {
const [localList, setLocalList] = useState<ComputeCaseDto[]>([]);
const { data } = useFetchCases();
const { modelId } = useParams<{ modelId: string }>();
const { instance, accounts } = useMsal();
const token = useAccessToken(instance, accounts[0]);
Expand Down Expand Up @@ -298,6 +300,32 @@ export const CaseGroup = ({
}
}, [addCase, triggerAddCase]);

const duplicateCase = (id: string) => {
const caseToDuplicate = data?.data.filter((c) => c.computeCaseId === id);
const randomLocalId = Math.floor(Math.random() * 100).toString();
if (caseToDuplicate) {
const newCase: ComputeCaseDto = {
computeCaseId: randomLocalId,
computeMethod: caseToDuplicate[0].computeMethod,
modelArea: caseToDuplicate[0].modelArea,
inputSettings: caseToDuplicate[0].inputSettings,
jobStatus: ComputeJobStatus.NOT_STARTED,
};

const localCasesForMethod = filerLocalList(
caseToDuplicate[0].computeMethod.name,
);

if (
localCasesForMethod.length < 1 &&
localCasesForMethod[0] === undefined
) {
setLocalList([...localList, newCase]);
updateLocalCaseList && updateLocalCaseList('Variogram', true);
}
}
};

return (
<>
{methodName === 'Channel' && (
Expand Down Expand Up @@ -378,6 +406,7 @@ export const CaseGroup = ({
removeLocalCase={removeLocalCase}
settingsFilter={settingsFilter}
variogramFilter={variogramFilter}
duplicateCase={duplicateCase}
/>
))}
</>
Expand Down
18 changes: 18 additions & 0 deletions src/features/Compute/CaseGroup/CaseRow/CaseRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const CaseRow = ({
removeLocalCase,
settingsFilter,
variogramFilter,
duplicateCase,
}: {
rowCase: ComputeCaseDto;
id: string;
Expand Down Expand Up @@ -60,6 +61,7 @@ export const CaseRow = ({
variogramFilter?: (
name: string,
) => ListComputeSettingsMethodDto[] | undefined;
duplicateCase?: (id: string) => void;
}) => {
const [selectedModelArea, setModelArea] = useState<ModelAreaDto[]>();
const [selectedIndicatorParameters, setIndicatorParameters] =
Expand Down Expand Up @@ -378,6 +380,7 @@ export const CaseRow = ({
defaultArea = areaList.filter(
(area) => area.modelAreaId === rowCase[0].modelArea.modelAreaId,
);
setModelArea(defaultArea);
}
return defaultArea;
},
Expand Down Expand Up @@ -408,6 +411,17 @@ export const CaseRow = ({
allCasesList.forEach((r) => setNotSavedVariogram(r, 'ContiniousParameter'));
}, [caseList, allCasesList, saved]);

const hasUnsavedCase = (id: string) => {
const caseMethod = allCasesList.filter((c) => c.computeCaseId === id)[0]
.computeMethod;
const methodList = allCasesList
.filter(
(c) => c.computeMethod.computeMethodId === caseMethod.computeMethodId,
)
.filter((i) => i.computeCaseId.length < 4);
return methodList.length > 0;
};

return (
<Styled.Case className={id.length <= 3 ? 'local-case' : ''}>
<Styled.CaseRow
Expand Down Expand Up @@ -495,10 +509,14 @@ export const CaseRow = ({
saved={saved}
isProcessed={data?.data.isProcessed}
caseStatus={rowCase.jobStatus}
hasUnsavedCase={hasUnsavedCase(id)}
saveCase={() => handleSaveCase(id)}
runCase={runRowCase}
deleteCase={deleteCase}
setAlertMessage={setAlertMessage}
duplicateCase={() => {
duplicateCase && duplicateCase(id);
}}
/>
</Styled.CaseRow>
</Styled.Case>
Expand Down

0 comments on commit 13db770

Please sign in to comment.