From b0f6e8b8b88c67b4d99330345cbc0f145f4fccf2 Mon Sep 17 00:00:00 2001 From: mheggelund Date: Thu, 7 Dec 2023 08:54:16 +0100 Subject: [PATCH] refactor: Seperated MetadataSelect into own component. --- .../MetadataSelect/MetadataSelect.tsx | 48 ++++++++++++++++ .../AddModel/ModelMetadata/ModelMetadata.tsx | 55 +++---------------- 2 files changed, 55 insertions(+), 48 deletions(-) create mode 100644 src/features/AddModel/ModelMetadata/MetadataSelect/MetadataSelect.tsx diff --git a/src/features/AddModel/ModelMetadata/MetadataSelect/MetadataSelect.tsx b/src/features/AddModel/ModelMetadata/MetadataSelect/MetadataSelect.tsx new file mode 100644 index 00000000..fde47002 --- /dev/null +++ b/src/features/AddModel/ModelMetadata/MetadataSelect/MetadataSelect.tsx @@ -0,0 +1,48 @@ +import { Autocomplete, AutocompleteChanges } from '@equinor/eds-core-react'; +import { MetadataDto } from '../../../../api/generated'; + +export const MetadataSelect = ({ + type, + errors, + data, + metadata, + isLoading, + handleAddMetadata, +}: { + type: string; + errors: string | undefined; + data: MetadataDto[]; + metadata: MetadataDto[]; + isLoading: boolean; + handleAddMetadata: ( + e: AutocompleteChanges, + type: string, + ) => void; +}) => { + const setSelectedMetadataOptions = (type: string) => { + if (!isLoading && data) { + const dataProps = data.filter((z) => z.metadataType === type); + + const selectedProps = metadata.filter((m) => m.metadataType === type); + + return dataProps.filter( + (c) => + selectedProps.findIndex((x: MetadataDto) => x.value === c.value) > -1, + ); + } + }; + + return ( + d.metadataType === type)} + optionLabel={(option) => option.value} + selectedOptions={setSelectedMetadataOptions(type)} + multiple + onOptionsChange={(e: AutocompleteChanges) => + handleAddMetadata(e, type) + } + > + ); +}; diff --git a/src/features/AddModel/ModelMetadata/ModelMetadata.tsx b/src/features/AddModel/ModelMetadata/ModelMetadata.tsx index f85242d9..7eba47e1 100644 --- a/src/features/AddModel/ModelMetadata/ModelMetadata.tsx +++ b/src/features/AddModel/ModelMetadata/ModelMetadata.tsx @@ -17,6 +17,7 @@ import { OpenAPI, } from '../../../api/generated'; import { useAccessToken } from '../../../hooks/useAccessToken'; +import { MetadataSelect } from './MetadataSelect/MetadataSelect'; import * as Styled from './ModelMetadata.styled'; export const ModelMetadata = ({ @@ -69,8 +70,12 @@ export const ModelMetadata = ({ }); } - if (isLoading || !data?.success) return

Loading ...

; - if (analougeData.isLoading || !analougeData?.data?.success) + if ( + isLoading || + !data?.success || + analougeData.isLoading || + !analougeData?.data?.success + ) return

Loading ...

; return ( @@ -169,49 +174,3 @@ export const ModelMetadata = ({ ); }; - -const MetadataSelect = ({ - type, - errors, - data, - metadata, - isLoading, - handleAddMetadata, -}: { - type: string; - errors: string | undefined; - data: MetadataDto[]; - metadata: MetadataDto[]; - isLoading: boolean; - handleAddMetadata: ( - e: AutocompleteChanges, - type: string, - ) => void; -}) => { - const setSelectedMetadataOptions = (type: string) => { - if (!isLoading && data) { - const dataProps = data.filter((z) => z.metadataType === type); - - const selectedProps = metadata.filter((m) => m.metadataType === type); - - return dataProps.filter( - (c) => - selectedProps.findIndex((x: MetadataDto) => x.value === c.value) > -1, - ); - } - }; - - return ( - d.metadataType === type)} - optionLabel={(option) => option.value} - selectedOptions={setSelectedMetadataOptions(type)} - multiple - onOptionsChange={(e: AutocompleteChanges) => - handleAddMetadata(e, type) - } - > - ); -};