Skip to content

Commit

Permalink
Chore/metadata validation (#334)
Browse files Browse the repository at this point in the history
* chore: Validate GDE input.

* chore: Validate Outcrop input.

* chore: Change error message, remove log.

* chore: Move stratCol dialig. Add input validation and error feedback.

* chore: Fix gde error logic.

* chore: Fix duplicate key in model table.
  • Loading branch information
mheggelund authored Oct 11, 2024
1 parent 7108d3b commit 08d3396
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 129 deletions.
34 changes: 33 additions & 1 deletion src/components/GrossDepositionEnviroment/GdeSelect/GdeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import { GeologicalStandardDto } from '../../../api/generated';
import { useFetchGrossDepData } from '../../../hooks/useFetchGrossDepData';
import * as StyledDialog from '../../../styles/addRowDialog/AddRowDialog.styled';
import { sortList } from '../../../utils/SortList';
import { GdeType } from '../GrossDepositionEnviromentGroup/GrossDepositionEnviromentGroup';
import {
GDEErrorType,
GdeType,
} from '../GrossDepositionEnviromentGroup/GrossDepositionEnviromentGroup';

export const GdeSelect = ({
gdeObject,
setGdeObject,
error,
setErrors,
}: {
gdeObject: GdeType;
setGdeObject: React.Dispatch<React.SetStateAction<GdeType>>;
error: GDEErrorType;
setErrors: React.Dispatch<React.SetStateAction<GDEErrorType>>;
}) => {
const GdeData = useFetchGrossDepData();

Expand Down Expand Up @@ -50,8 +57,11 @@ export const GdeSelect = ({
...gdeObject,
grossDepEnv: e.selectedItems[0],
});
setErrors({});
}}
noOptionsText="No options"
variant={error.GDE ? 'error' : undefined}
helperText={error.GDE ? error.GDE : undefined}
/>

<Autocomplete
Expand All @@ -66,6 +76,16 @@ export const GdeSelect = ({
});
}}
noOptionsText="No options"
variant={
error.DEnv && gdeObject.grossDepEnv !== undefined
? 'error'
: undefined
}
helperText={
error.DEnv && gdeObject.grossDepEnv !== undefined
? error.DEnv
: undefined
}
/>

<Autocomplete
Expand All @@ -80,6 +100,16 @@ export const GdeSelect = ({
});
}}
noOptionsText="No options"
variant={
error.subEnv && gdeObject.grossDepEnv !== undefined
? 'error'
: undefined
}
helperText={
error.subEnv && gdeObject.grossDepEnv !== undefined
? error.subEnv
: undefined
}
/>

<Autocomplete
Expand All @@ -94,6 +124,8 @@ export const GdeSelect = ({
});
}}
noOptionsText="No options"
variant={error.AEl ? 'error' : undefined}
helperText={error.subEnv ? error.AEl : undefined}
/>
</StyledDialog.AutocompleteList>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { GDEErrorType, GdeType } from './GrossDepositionEnviromentGroup';

export const validateInput = async (gdeObject: GdeType) => {
const errorObject: GDEErrorType = {};
const message = 'Value missing';

if (gdeObject.grossDepEnv === undefined) {
errorObject.GDE = message;
}
if (gdeObject.depEnv === undefined) {
errorObject.DEnv = message;
}
if (gdeObject.subenv === undefined) {
errorObject.subEnv = message;
}
if (gdeObject.architecturalElements?.length === 0) {
errorObject.AEl = message;
}

return errorObject;
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import * as StyledDialog from '../../../styles/addRowDialog/AddRowDialog.styled'
import { GdeSelect } from '../GdeSelect/GdeSelect';
import * as Styled from './GrossDepositionEnviromentGroup.styled';

import { validateInput } from './GDE.hooks';

export interface GdeType {
grossDepEnv?: GeologicalStandardDto;
depEnv?: GeologicalStandardDto;
Expand All @@ -34,6 +36,13 @@ const defaultGdeData: GdeType = {
subenv: undefined,
architecturalElements: [],
};

export type GDEErrorType = {
GDE?: string;
DEnv?: string;
subEnv?: string;
AEl?: string;
};
export const GrossDepositionEnviromentGroup = ({
modelIdParent,
defaultMetadata,
Expand All @@ -49,6 +58,8 @@ export const GrossDepositionEnviromentGroup = ({
}) => {
const [showGdeDialog, setShowGdeDialog] = useState<boolean>(false);
const [gdeObject, setGdeObject] = useState<GdeType>(defaultGdeData);
const [errors, setErrors] = useState<GDEErrorType>({});

const handleGdeDialog = () => {
setShowGdeDialog(!showGdeDialog);
};
Expand All @@ -73,13 +84,16 @@ export const GrossDepositionEnviromentGroup = ({

const handleAddGde = async () => {
const id = modelIdParent ? modelIdParent : defaultMetadata.analogueModelId;
const err = await validateInput(gdeObject);
setErrors(err);

if (
id &&
gdeObject.grossDepEnv &&
gdeObject.depEnv &&
gdeObject.subenv &&
gdeObject.architecturalElements
gdeObject.architecturalElements &&
gdeObject.architecturalElements.length > 0
) {
const architecturalElementsList: string[] = [];
gdeObject.architecturalElements.map((x) =>
Expand Down Expand Up @@ -162,7 +176,12 @@ export const GrossDepositionEnviromentGroup = ({
<StyledDialog.DialogWindow open={showGdeDialog}>
<Dialog.Header>Add Gross Deposition Enviroment</Dialog.Header>
<Dialog.CustomContent>
<GdeSelect gdeObject={gdeObject} setGdeObject={setGdeObject} />
<GdeSelect
gdeObject={gdeObject}
setGdeObject={setGdeObject}
error={errors}
setErrors={setErrors}
/>
</Dialog.CustomContent>
<StyledDialog.Actions>
<Button onClick={handleAddGde}>Add</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const defaultOutcropData: OutcropType = {
basins: [],
};

export type OutcropErrorType = {
Analogue?: string;
};

export const OutcropAnalogueGroup = ({
modelIdParent,
defaultMetadata,
Expand All @@ -44,17 +48,31 @@ export const OutcropAnalogueGroup = ({
outcropGroup: OutcropDto[];
}) => {
const [showOutcropDialog, setShowOutcropDialog] = useState<boolean>(false);
const [errors, setErrors] = useState<OutcropErrorType>({});
const [outcropObject, setOutcropObject] =
useState<OutcropType>(defaultOutcropData);

const useOutcrop = useOutcropAnalouge();

const handleOutcropDialog = () => {
setShowOutcropDialog(!showOutcropDialog);
setOutcropObject(defaultOutcropData);
};

const validateInput = async (outcropObject: OutcropType) => {
const errorObject: OutcropErrorType = {};

if (outcropObject.outcropId === undefined) {
errorObject.Analogue = 'Value missing';
}

return errorObject;
};

const handleAddOutcropAnalogue = async () => {
const id = modelIdParent ? modelIdParent : defaultMetadata.analogueModelId;
const err = await validateInput(outcropObject);
setErrors(err);

if (id && outcropObject.outcropId) {
const postRequestBody: AddAnalogueModelOutcropForm = {
Expand Down Expand Up @@ -139,6 +157,7 @@ export const OutcropAnalogueGroup = ({
outcropObject={outcropObject}
outcropGroup={outcropGroup}
setOutcropObject={setOutcropObject}
error={errors}
/>
</Dialog.CustomContent>
<StyledDialog.Actions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import { OutcropDto } from '../../../api/generated';
import { useFetchOutcropData } from '../../../hooks/useFetchOutcropData';
import * as StyledDialog from '../../../styles/addRowDialog/AddRowDialog.styled';
import { sortList } from '../../../utils/SortList';
import { OutcropType } from '../OutcropAnalogueGroup/OutcropAnalogueGroup';
import {
OutcropErrorType,
OutcropType,
} from '../OutcropAnalogueGroup/OutcropAnalogueGroup';

export const OutcropSelect = ({
outcropObject,
outcropGroup,
error,
setOutcropObject,
}: {
outcropObject: OutcropType;
outcropGroup: OutcropDto[];
error: OutcropErrorType;
setOutcropObject: React.Dispatch<React.SetStateAction<OutcropType>>;
}) => {
const OutcropData = useFetchOutcropData();
Expand Down Expand Up @@ -55,6 +60,8 @@ export const OutcropSelect = ({
}}
optionDisabled={filterDisabled}
noOptionsText="No options"
variant={error.Analogue ? 'error' : undefined}
helperText={error.Analogue ? error.Analogue : undefined}
/>

<Autocomplete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
StratColumnDto,
StratUnitDto,
} from '../../../api/generated';
import { StratColumnType } from '../../../features/HandleModel/HandleModelComponent/HandleModelComponent';
import {
useFetchSmdaCountries,
useFetchSmdaFields,
Expand All @@ -15,13 +14,21 @@ import {
} from '../../../hooks/useFetchStratColData';
import * as StyledDialog from '../../../styles/addRowDialog/AddRowDialog.styled';
import { sortList } from '../../../utils/SortList';
import {
StratColumnErrorType,
StratColumnType,
} from '../StratigrapicGroups/StratigrapicGroups';

export const StratigraphicColumnSelect = ({
stratColumnObject,
setStratColumnObject,
error,
setErrors,
}: {
stratColumnObject: StratColumnType;
setStratColumnObject: React.Dispatch<React.SetStateAction<StratColumnType>>;
error: StratColumnErrorType;
setErrors: React.Dispatch<React.SetStateAction<StratColumnErrorType>>;
}) => {
const countryData = useFetchSmdaCountries();
const fieldData = useFetchSmdaFields();
Expand Down Expand Up @@ -74,8 +81,11 @@ export const StratigraphicColumnSelect = ({
level2: undefined,
level3: undefined,
});
setErrors({});
}}
noOptionsText="No options"
variant={error.country ? 'error' : undefined}
helperText={error.country ? error.country : undefined}
/>

<Autocomplete
Expand All @@ -97,6 +107,16 @@ export const StratigraphicColumnSelect = ({
stratColumnObject.field ? [stratColumnObject.field] : []
}
noOptionsText="No options"
variant={
error.field && stratColumnObject.country !== undefined
? 'error'
: undefined
}
helperText={
error.field && stratColumnObject.country !== undefined
? error.field
: undefined
}
/>

<Autocomplete
Expand All @@ -121,11 +141,22 @@ export const StratigraphicColumnSelect = ({
level2: undefined,
level3: undefined,
});
setErrors({});
}}
selectedOptions={
stratColumnObject.stratColumn ? [stratColumnObject.stratColumn] : []
}
noOptionsText="No options"
variant={
error.stratColumn && stratColumnObject.country !== undefined
? 'error'
: undefined
}
helperText={
error.stratColumn && stratColumnObject.country !== undefined
? error.stratColumn
: undefined
}
/>

<Autocomplete
Expand All @@ -141,18 +172,29 @@ export const StratigraphicColumnSelect = ({
),
)}
optionLabel={(option) => option.identifier}
onOptionsChange={(e: AutocompleteChanges<StratUnitDto>) =>
onOptionsChange={(e: AutocompleteChanges<StratUnitDto>) => {
setStratColumnObject({
...stratColumnObject,
level1: e.selectedItems[0],
level2: undefined,
level3: undefined,
})
}
});
setErrors({});
}}
selectedOptions={
stratColumnObject.level1 ? [stratColumnObject.level1] : []
}
noOptionsText="No options"
variant={
error.level1 && stratColumnObject.stratColumn !== undefined
? 'error'
: undefined
}
helperText={
error.level1 && stratColumnObject.stratColumn !== undefined
? error.level1
: undefined
}
/>

<Autocomplete
Expand All @@ -172,17 +214,28 @@ export const StratigraphicColumnSelect = ({
),
)}
optionLabel={(option) => option.identifier}
onOptionsChange={(e: AutocompleteChanges<StratUnitDto>) =>
onOptionsChange={(e: AutocompleteChanges<StratUnitDto>) => {
setStratColumnObject({
...stratColumnObject,
level2: e.selectedItems[0],
level3: undefined,
})
}
});
setErrors({});
}}
selectedOptions={
stratColumnObject.level2 ? [stratColumnObject.level2] : []
}
noOptionsText="No options"
variant={
error.level2 && stratColumnObject.level1 !== undefined
? 'error'
: undefined
}
helperText={
error.level2 && stratColumnObject.level1 !== undefined
? error.level2
: undefined
}
/>

<Autocomplete
Expand Down
Loading

0 comments on commit 08d3396

Please sign in to comment.