Skip to content

Commit

Permalink
refactor: Metadata refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
mheggelund committed Nov 28, 2023
1 parent 3de5291 commit a137320
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
19 changes: 10 additions & 9 deletions src/features/AddModel/AddModelDialog/AddModelDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ interface AddModelDialogProps {
export default interface MetadataProps {
name: string;
description: string;
field: MetadataDto[];
zone?: MetadataDto[];
formation: MetadataDto[];
metadata: MetadataDto[];
analogue?: AnalogueList[];
}

Expand All @@ -38,9 +36,7 @@ export type ErrorType = {
const defaultMetadata: MetadataProps = {
name: '',
description: '',
field: [],
zone: [],
formation: [],
metadata: [],
analogue: [],
};

Expand Down Expand Up @@ -83,13 +79,18 @@ export const AddModelDialog = ({
errors.description = 'Description not provided';
}

if (inputValues?.field === undefined || inputValues?.field?.length === 0) {
if (
inputValues?.metadata === undefined ||
inputValues?.metadata?.filter((m) => m.metadataType === 'Field').length <=
0
) {
errors.field = 'Field not selected';
}

if (
inputValues?.formation === undefined ||
inputValues?.formation?.length === 0
inputValues?.metadata === undefined ||
inputValues?.metadata?.filter((m) => m.metadataType === 'Formation')
.length <= 0
) {
errors.formation = 'Formation not selected';
}
Expand Down
39 changes: 32 additions & 7 deletions src/features/AddModel/ModelMetadata/ModelMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ export const ModelMetadata = ({
enabled: !!token,
});

function handleAddMetadata(
e: AutocompleteChanges<MetadataDto>,
propType: string,
) {
const metadataList: MetadataDto[] = metadata.metadata.filter(
(i) => i.metadataType !== propType,
);
const newList = [...metadataList, ...e.selectedItems];
setMetadata({
...metadata,
metadata: [...newList],
});
}

if (isLoading || !data?.success) return <p>Loading ...</p>;
if (analougeData.isLoading || !analougeData?.data?.success)
return <p>Loading ...</p>;
Expand Down Expand Up @@ -88,9 +102,12 @@ export const ModelMetadata = ({
label="Field"
options={data.data.filter((d) => d.metadataType === 'Field')}
optionLabel={(option) => option.value}
selectedOptions={metadata?.field}
selectedOptions={metadata?.metadata.filter(
(m) => m.metadataType === 'Field',
)}
multiple
onOptionsChange={(e: AutocompleteChanges<MetadataDto>) =>
setMetadata({ ...metadata, field: e.selectedItems })
handleAddMetadata(e, 'Field')
}
></Autocomplete>
{errors.field && <Label label="This field is required"></Label>}
Expand All @@ -103,10 +120,12 @@ export const ModelMetadata = ({
(d) => d.metadataType === 'Formation',
)}
optionLabel={(option) => option.value}
selectedOptions={metadata?.formation}
selectedOptions={metadata?.metadata.filter(
(m) => m.metadataType === 'Formation',
)}
multiple
onOptionsChange={(e: AutocompleteChanges<MetadataDto>) =>
setMetadata({ ...metadata, formation: e.selectedItems })
handleAddMetadata(e, 'Formation')
}
></Autocomplete>
{errors.formation && (
Expand All @@ -122,16 +141,22 @@ export const ModelMetadata = ({
selectedOptions={metadata?.analogue}
multiple
onOptionsChange={(e: AutocompleteChanges<AnalogueList>) =>
setMetadata({ ...metadata, analogue: e.selectedItems })
setMetadata({
...metadata,
analogue: e.selectedItems,
})
}
></Autocomplete>
<Autocomplete
label="Zone (optional)"
options={data.data.filter((d) => d.metadataType === 'Zone')}
optionLabel={(option) => option.value}
selectedOptions={metadata?.zone}
selectedOptions={metadata?.metadata.filter(
(m) => m.metadataType === 'Zone',
)}
multiple
onOptionsChange={(e: AutocompleteChanges<MetadataDto>) =>
setMetadata({ ...metadata, zone: e.selectedItems })
handleAddMetadata(e, 'Zone')
}
></Autocomplete>
</Styled.AutocompleteRow>
Expand Down
4 changes: 1 addition & 3 deletions src/pages/Browse/Browse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ export const Browse = () => {
}

async function uploadMetadata(modelId: string, metadata: MetadataProps) {
addMetadataFields(metadata.zone);
addMetadataFields(metadata.field);
addMetadataFields(metadata.formation);
addMetadataFields(metadata.metadata);
addAnalogueFields(metadata.analogue);

const readyMetadata: AddAnalogueModelMetadataCommandForm = {
Expand Down

0 comments on commit a137320

Please sign in to comment.