Skip to content

Commit

Permalink
feat: Add no relevant option to analogue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mheggelund committed Feb 28, 2024
1 parent 1751138 commit e61bf22
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 41 deletions.
7 changes: 0 additions & 7 deletions src/features/AddModel/AddModelDialog/AddModelDialog.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ export const validateValues = (
errors.description = 'Description not provided';
}

if (
inputValues?.analogues === undefined ||
inputValues?.analogues?.length <= 0
) {
errors.analogues = 'Analogues not selected';
}

if (!files.NC && !isEdit) {
errors.file = 'NC file missing';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable max-lines-per-function */
/* eslint-disable react/prop-types */

import { Autocomplete, AutocompleteChanges } from '@equinor/eds-core-react';
import { AnalogueList, AnalogueModelDetail } from '../../../../api/generated';

export const AnalogueSelect = ({
errors,
data,
analogue,
metadata,
setMetadata,
}: {
errors: string | undefined;
data: AnalogueList[];
analogue: AnalogueList[] | undefined;
metadata: AnalogueModelDetail;
setMetadata: (metadata: AnalogueModelDetail) => void;
}) => {
const emptyOption: AnalogueList[] = [
{
analogueId: 'Analogue',
name: 'NoRelevant',
description: 'Not relevant',
},
];

const selectedOptions =
metadata.analogues.length > 0 ? metadata.analogues : emptyOption;

const optionList: AnalogueList[] =
data.length > 0 ? emptyOption.concat(data) : emptyOption;

const intersection = optionList.filter(
(a) =>
selectedOptions &&
selectedOptions.some((b) => JSON.stringify(a) === JSON.stringify(b)),
);

function handleAddMetadata(e: AutocompleteChanges<AnalogueList>) {
const removeNotSelected = e.selectedItems.filter(
(i) => i.analogueId !== 'Analogue',
);
const newList = [...removeNotSelected];

setMetadata({
...metadata,
analogues: [...newList],
});
}

return (
<Autocomplete
variant={errors ? 'error' : undefined}
label="Analogue"
options={optionList}
optionLabel={(option) => option.name}
multiple
initialSelectedOptions={
intersection.length === 0 ? emptyOption : intersection
}
onOptionsChange={(e: AutocompleteChanges<AnalogueList>) =>
handleAddMetadata(e)
}
/>
);
};
38 changes: 8 additions & 30 deletions src/features/AddModel/ModelMetadata/ModelMetadata.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable max-lines-per-function */
import {
Autocomplete,
AutocompleteChanges,
Label,
Typography,
Expand All @@ -10,14 +9,14 @@ import { ErrorType } from '../AddModelDialog/AddModelDialog';
import { useMsal } from '@azure/msal-react';
import { useQuery } from '@tanstack/react-query';
import {
AnalogueList,
AnalogueModelDetail,
AnaloguesService,
MetadataDto,
MetadataService,
OpenAPI,
} from '../../../api/generated';
import { useAccessToken } from '../../../hooks/useAccessToken';
import { AnalogueSelect } from './AnalogueSelect/AnalogueSelect';
import { MetadataSelect } from './MetadataSelect/MetadataSelect';
import * as Styled from './ModelMetadata.styled';

Expand Down Expand Up @@ -46,16 +45,6 @@ export const ModelMetadata = ({
enabled: !!token,
});

const setSelectedAnalogueOptions = () => {
if (analougeData.data?.success && metadata.analogues) {
const analogue = metadata?.analogues;

return analougeData.data?.data.filter(
(c) => analogue.findIndex((x: AnalogueList) => x.name === c.name) > -1,
);
}
};

function handleAddMetadata(
e: AutocompleteChanges<MetadataDto>,
propType: string,
Expand All @@ -70,13 +59,9 @@ export const ModelMetadata = ({
const removeNotSelected = e.selectedItems.filter(
(i) => i.metadataType === propType,
);
// console.log(removeNotSelected)

const newList = [...metadataList, ...removeNotSelected];

// if (propType === 'Formation') console.log(e.selectedItems);
// if (propType === 'Formation') console.log(newList);

setMetadata({
...metadata,
metadata: [...newList],
Expand Down Expand Up @@ -152,20 +137,13 @@ export const ModelMetadata = ({
</Styled.AutocompleteRow>
<Styled.AutocompleteRow>
<Styled.Required>
<Autocomplete
variant={errors.analogues ? 'error' : undefined}
label="Analogue"
options={analougeData.data.data}
optionLabel={(option) => option.name}
selectedOptions={setSelectedAnalogueOptions()}
multiple
onOptionsChange={(e: AutocompleteChanges<AnalogueList>) =>
setMetadata({
...metadata,
analogues: e.selectedItems,
})
}
></Autocomplete>
<AnalogueSelect
errors={errors.analogues}
data={analougeData.data.data}
analogue={analougeData.data.data}
metadata={metadata}
setMetadata={setMetadata}
/>
{errors.analogues && (
<Label label="This field is required"></Label>
)}
Expand Down
8 changes: 5 additions & 3 deletions src/features/ModelTable/ModelTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ export const ModelTable = ({
size: 100,
cell: ({ row }) => (
<Styled.List>
{row.original.analogues.map((a) => (
<p key={a.analogueId}>{a.name + ', '}</p>
))}
{row.original.analogues.length > 0
? row.original.analogues.map((a) => (
<p key={a.analogueId}>{a.name + ', '}</p>
))
: 'Not relevant'}
</Styled.List>
),
},
Expand Down
2 changes: 1 addition & 1 deletion src/features/ModelView/TableDataCell/TableDataCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const TableDataCell = ({
<Typography key={m.analogueId}>{m.name}</Typography>
))
) : (
<Typography> - </Typography>
<Typography> Not relevant </Typography>
)
) : data.metadata?.filter((m) => m.metadataType === type).length ? (
data.metadata
Expand Down

0 comments on commit e61bf22

Please sign in to comment.