Skip to content

Commit

Permalink
Core: Temp empty option.
Browse files Browse the repository at this point in the history
  • Loading branch information
mheggelund committed Feb 27, 2024
1 parent 5d1172a commit f514875
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
8 changes: 0 additions & 8 deletions src/features/AddModel/AddModelDialog/AddModelDialog.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-lines-per-function */
import { Autocomplete, AutocompleteChanges } from '@equinor/eds-core-react';
import { MetadataDto } from '../../../../api/generated';

Expand All @@ -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 (
<Autocomplete
variant={errors ? 'error' : undefined}
label={type}
options={data.filter((d) => 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<MetadataDto>) =>
handleAddMetadata(e, type)
Expand Down
12 changes: 9 additions & 3 deletions src/features/AddModel/ModelMetadata/ModelMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit f514875

Please sign in to comment.