From f5148757e34b203b998a21ba1d6ff88a52bd48e7 Mon Sep 17 00:00:00 2001 From: mheggelund Date: Tue, 27 Feb 2024 08:39:02 +0100 Subject: [PATCH] Core: Temp empty option. --- .../AddModelDialog/AddModelDialog.hooks.ts | 8 --- .../MetadataSelect/MetadataSelect.tsx | 62 +++++++++++++++++-- .../AddModel/ModelMetadata/ModelMetadata.tsx | 12 +++- .../ModelMetadataView/ModelMetadataView.tsx | 7 ++- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/src/features/AddModel/AddModelDialog/AddModelDialog.hooks.ts b/src/features/AddModel/AddModelDialog/AddModelDialog.hooks.ts index 7052035f..3e9b00a0 100644 --- a/src/features/AddModel/AddModelDialog/AddModelDialog.hooks.ts +++ b/src/features/AddModel/AddModelDialog/AddModelDialog.hooks.ts @@ -47,14 +47,6 @@ export const validateValues = ( errors.field = 'Field not selected'; } - if ( - inputValues?.metadata === undefined || - inputValues?.metadata?.filter((m) => m.metadataType === 'Formation') - .length <= 0 - ) { - errors.formation = 'Formation not selected'; - } - if ( inputValues?.analogues === undefined || inputValues?.analogues?.length <= 0 diff --git a/src/features/AddModel/ModelMetadata/MetadataSelect/MetadataSelect.tsx b/src/features/AddModel/ModelMetadata/MetadataSelect/MetadataSelect.tsx index 0a1d887a..c2d29b3c 100644 --- a/src/features/AddModel/ModelMetadata/MetadataSelect/MetadataSelect.tsx +++ b/src/features/AddModel/ModelMetadata/MetadataSelect/MetadataSelect.tsx @@ -1,3 +1,4 @@ +/* eslint-disable max-lines-per-function */ import { Autocomplete, AutocompleteChanges } from '@equinor/eds-core-react'; import { MetadataDto } from '../../../../api/generated'; @@ -19,26 +20,77 @@ export const MetadataSelect = ({ type: string, ) => void; }) => { + const emptyOption: MetadataDto[] = [ + { + metadataId: type, + metadataType: 'NoRelevant', + value: 'Not relevant', + }, + ]; + const filteredOptions = data.filter((d) => d.metadataType === type); + const optionList: MetadataDto[] = filteredOptions + ? emptyOption.concat(filteredOptions) + : emptyOption; + const setSelectedMetadataOptions = (type: string) => { - if (!isLoading && data && metadata) { - const dataProps = data.filter((z) => z.metadataType === type); + let selectedOptions: MetadataDto[] = []; + if (!isLoading && data && metadata) { const selectedProps = metadata.filter((m) => m.metadataType === type); - return dataProps.filter( + const noRelevantSelected = metadata + .filter((m) => m.metadataType === 'NoRelevant') + .filter((t) => t.metadataId === type); + + const loadedProps = optionList.filter( (c) => selectedProps.findIndex((x: MetadataDto) => x.value === c.value) > -1, ); + + if ( + metadata.length > 0 && + selectedProps.length > 0 && + noRelevantSelected.length === 0 + ) { + selectedOptions = loadedProps; + } else if ( + selectedProps.length === 0 && + noRelevantSelected.length === 0 + ) { + selectedOptions = emptyOption; + } else if ( + selectedProps.length === 0 && + noRelevantSelected.length !== 0 + ) { + selectedOptions = []; + } } + return selectedOptions; }; + const resLitstLength = setSelectedMetadataOptions(type)?.length; return ( d.metadataType === type)} + options={optionList} optionLabel={(option) => option.value} - selectedOptions={setSelectedMetadataOptions(type)} + selectedOptions={ + setSelectedMetadataOptions(type) && + setSelectedMetadataOptions(type) !== undefined && + resLitstLength && + resLitstLength > 0 + ? setSelectedMetadataOptions(type) + : resLitstLength && resLitstLength === 0 + ? [ + { + metadataId: type, + metadataType: 'NoRelevant', + value: 'Not relevant', + }, + ] + : [] + } multiple onOptionsChange={(e: AutocompleteChanges) => handleAddMetadata(e, type) diff --git a/src/features/AddModel/ModelMetadata/ModelMetadata.tsx b/src/features/AddModel/ModelMetadata/ModelMetadata.tsx index 01d0fdc7..381e796f 100644 --- a/src/features/AddModel/ModelMetadata/ModelMetadata.tsx +++ b/src/features/AddModel/ModelMetadata/ModelMetadata.tsx @@ -61,11 +61,17 @@ export const ModelMetadata = ({ propType: string, ) { if (!metadata.metadata) return; - const metadataList: MetadataDto[] = metadata.metadata.filter( - (i) => i.metadataType !== propType, - ); + + // Filert out metadata of the same type as dropdown props type + const metadataList: MetadataDto[] = metadata.metadata + .filter((i) => i.metadataType !== propType) + .filter((n) => n.metadataId !== propType); const newList = [...metadataList, ...e.selectedItems]; + + if (propType === 'Formation') console.log(e.selectedItems); + if (propType === 'Formation') console.log(newList); + setMetadata({ ...metadata, metadata: [...newList], diff --git a/src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx b/src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx index 1b03a0cc..7d91b079 100644 --- a/src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx +++ b/src/features/ModelView/ModelMetadataView/ModelMetadataView.tsx @@ -97,8 +97,11 @@ export const ModelMetadataView = () => { const analougueList: AddAnalogueDto[] = []; function addMetadataFields(metadata?: MetadataDto[]) { - if (!metadata) return; - const obj = metadata.map((x) => ({ metadataId: x.metadataId })); + const filteredMetadata = metadata?.filter( + (m) => m.metadataType !== 'NoRelevant', + ); + if (!filteredMetadata) return; + const obj = filteredMetadata.map((x) => ({ metadataId: x.metadataId })); metadataList.push(...obj); }