From 82f800389fb18df5a2d40ea1d7c39125ea808d1d Mon Sep 17 00:00:00 2001 From: Thomas Lund Fagermyr <35408743+thomaslf97@users.noreply.github.com> Date: Thu, 14 Nov 2024 14:42:46 +0100 Subject: [PATCH] fix: Fixed editing of model name and desc (#373) --- .../EditNameDescription.tsx | 13 +++-- .../ModelMetadataView/ModelMetadataView.tsx | 50 ++----------------- src/hooks/useFetchImage.tsx | 5 +- 3 files changed, 11 insertions(+), 57 deletions(-) diff --git a/src/features/ModelView/EditNameDescription/EditNameDescription.tsx b/src/features/ModelView/EditNameDescription/EditNameDescription.tsx index 1c4e272..6245d1e 100644 --- a/src/features/ModelView/EditNameDescription/EditNameDescription.tsx +++ b/src/features/ModelView/EditNameDescription/EditNameDescription.tsx @@ -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; isEdit: boolean; - defaultMetadata: AnalogueModelDetail; closeDialog: () => void; }) => { + const { analogueModel } = usePepmContextStore(); const [errors, setErrors] = useState({}); - const [metadata, setMetadata] = - useState(defaultMetadata); + const [metadata, setMetadata] = useState(analogueModel); const [submitting, setSubmitting] = useState(false); useEffect(() => { const cleanupStates = () => { - if (!isEdit) setMetadata(defaultMetadata); + if (!isEdit) setMetadata(analogueModel); setSubmitting(false); }; @@ -37,7 +36,7 @@ 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)); @@ -45,7 +44,7 @@ export const EditNameDescription = ({ }; const handleClose = () => { - setMetadata(defaultMetadata); + setMetadata(analogueModel); if (closeDialog) closeDialog(); }; diff --git a/src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx b/src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx index ad58f28..3352c6e 100644 --- a/src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx +++ b/src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx @@ -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'; @@ -52,6 +48,7 @@ export const ModelMetadataView = ({ setStratigraphicColumns, setStratigraphicUnits, setGeologicalStandards, + setAnalogueModel, } = usePepmContextStore(); const outcropData = useFetchOutcropData(); const countryData = useFetchSmdaCountries(); @@ -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); @@ -137,6 +130,7 @@ export const ModelMetadataView = ({ } const updateNameDescription = useMutation({ + mutationKey: ['analogue-model'], mutationFn: ({ id, requestBody, @@ -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 @@ -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(); }; @@ -299,7 +256,6 @@ export const ModelMetadataView = ({ diff --git a/src/hooks/useFetchImage.tsx b/src/hooks/useFetchImage.tsx index c2db8ce..0155897 100644 --- a/src/hooks/useFetchImage.tsx +++ b/src/hooks/useFetchImage.tsx @@ -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 = () => { @@ -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;