Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Metadata refactored #174

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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