Skip to content

Commit

Permalink
fix: Fixed editing of model name and desc (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaslf97 authored Nov 14, 2024
1 parent cca30f9 commit 82f8003
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ import { AnalogueModelDetail } from '../../../api/generated';
import { validateValues } from '../../HandleModel/HandleModelComponent/HandleModelComponent.hooks';
import { ModelMetadata } from '../../HandleModel/ModelMetadata/ModelMetadata';
import * as Styled from './EditNameDescription.styled';
import { usePepmContextStore } from '../../../hooks/GlobalState';
export const EditNameDescription = ({
edit,
isEdit,
defaultMetadata,
closeDialog,
}: {
edit: (metadata: AnalogueModelDetail) => Promise<void>;
isEdit: boolean;
defaultMetadata: AnalogueModelDetail;
closeDialog: () => void;
}) => {
const { analogueModel } = usePepmContextStore();
const [errors, setErrors] = useState({});
const [metadata, setMetadata] =
useState<AnalogueModelDetail>(defaultMetadata);
const [metadata, setMetadata] = useState<AnalogueModelDetail>(analogueModel);
const [submitting, setSubmitting] = useState(false);

useEffect(() => {
const cleanupStates = () => {
if (!isEdit) setMetadata(defaultMetadata);
if (!isEdit) setMetadata(analogueModel);
setSubmitting(false);
};

Expand All @@ -37,15 +36,15 @@ export const EditNameDescription = ({
if (Object.keys(errors).length === 0 && submitting) {
finishSubmit();
}
}, [defaultMetadata, edit, errors, isEdit, metadata, submitting]);
}, [analogueModel, edit, errors, isEdit, metadata, submitting]);

const handleSubmit = () => {
setErrors(validateValues(metadata, undefined, isEdit));
setSubmitting(true);
};

const handleClose = () => {
setMetadata(defaultMetadata);
setMetadata(analogueModel);
if (closeDialog) closeDialog();
};

Expand Down
50 changes: 3 additions & 47 deletions src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import { useMutation } from '@tanstack/react-query';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useParams } from 'react-router-dom';
import {
AddAnalogueModelMetadataCommandForm,
AddMetadataDto,
AnalogueModelDetail,
AnalogueModelMetadataService,
GenerateThumbnailCommand,
JobsService,
MetadataDto,
UpdateAnalogueModelCommandBody,
} from '../../../api/generated';
import { AnalogueModelsService } from '../../../api/generated/services/AnalogueModelsService';
Expand Down Expand Up @@ -52,6 +48,7 @@ export const ModelMetadataView = ({
setStratigraphicColumns,
setStratigraphicUnits,
setGeologicalStandards,
setAnalogueModel,
} = usePepmContextStore();
const outcropData = useFetchOutcropData();
const countryData = useFetchSmdaCountries();
Expand All @@ -66,10 +63,6 @@ export const ModelMetadataView = ({

const { modelId } = useParams();

const defaultMetadata: AnalogueModelDetail = analogueModel
? analogueModel
: analogueModelDefault;

const generateThumbnail = useMutation({
mutationFn: (requestBody: GenerateThumbnailCommand) => {
return JobsService.postApiJobsComputeThumbnailGen(requestBody);
Expand Down Expand Up @@ -137,6 +130,7 @@ export const ModelMetadataView = ({
}

const updateNameDescription = useMutation({
mutationKey: ['analogue-model'],
mutationFn: ({
id,
requestBody,
Expand All @@ -148,34 +142,6 @@ export const ModelMetadataView = ({
},
});

const uploadModelMetadata = useMutation({
mutationFn: ({
id,
requestBody,
}: {
id: string;
requestBody: AddAnalogueModelMetadataCommandForm;
}) => {
return AnalogueModelMetadataService.putApiAnalogueModelsMetadata(
id,
requestBody,
);
},
onSuccess: () => {
queryClient.refetchQueries();
},
});

const metadataList: AddMetadataDto[] = [];
function addMetadataFields(metadata?: MetadataDto[]) {
const filteredMetadata = metadata?.filter(
(m) => m.metadataType !== 'NoRelevant',
);
if (!filteredMetadata) return;
const obj = filteredMetadata.map((x) => ({ metadataId: x.metadataId }));
metadataList.push(...obj);
}

const updateModelMetadata = async (metadata: AnalogueModelDetail) => {
const id = analogueModel.analogueModelId
? analogueModel.analogueModelId
Expand All @@ -190,16 +156,7 @@ export const ModelMetadataView = ({
id: id,
requestBody: modelMetadata,
});

addMetadataFields(metadata.metadata);
const readyMetadata: AddAnalogueModelMetadataCommandForm = {
metadata: metadataList,
};

await uploadModelMetadata.mutateAsync({
id: id,
requestBody: readyMetadata,
});
setAnalogueModel(metadata);
toggleEditMetadata();
};

Expand Down Expand Up @@ -299,7 +256,6 @@ export const ModelMetadataView = ({
<EditNameDescription
edit={updateModelMetadata}
isEdit={isAddModelDialog}
defaultMetadata={defaultMetadata}
closeDialog={toggleEditMetadata}
/>
</Styled.DescriptionMeta>
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/useFetchImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { useQuery } from '@tanstack/react-query';
import { useMsal } from '@azure/msal-react';
import { useAccessToken } from './useAccessToken';
import { analogueModelDefault, usePepmContextStore } from './GlobalState';
import { usePepmContextStore } from './GlobalState';
import { getAnalogueModelImage } from '../api/custom/getAnalogueModelImageById';

export const useFetchImage = () => {
Expand All @@ -18,8 +18,7 @@ export const useFetchImage = () => {
queryKey: ['analogue-model-image', analogueModel.analogueModelId, imageId],
queryFn: () =>
getAnalogueModelImage(analogueModel.analogueModelId, imageId),
enabled:
!!token && analogueModel !== analogueModelDefault && imageId !== '',
enabled: !!token && imageId !== '',
});

return query;
Expand Down

0 comments on commit 82f8003

Please sign in to comment.