Skip to content

Commit

Permalink
feat: Handle max errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Aug 29, 2024
1 parent fa1364f commit b9ea5d3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ export const MapAndLabelProvider: React.FC<MapAndLabelProviderProps> = (
const currentFeatures = formik.values.schemaData;
const updatedFeatures = [...currentFeatures, initialValues];
formik.setFieldValue("schemaData", updatedFeatures);

// TODO: Handle more gracefully - stop user from adding new feature to map?
if (schema.max && updatedFeatures.length > schema.max) {
setMaxError(true);
}
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const Root = () => {
} = mapAndLabelProps;

const [features, setFeatures] = useState<Feature[] | undefined>(undefined);
const { addFeature } = useMapAndLabelContext();
const { addFeature, schema } = useMapAndLabelContext();

useEffect(() => {
const geojsonChangeHandler = ({ detail: geojson }: any) => {
Expand All @@ -143,6 +143,11 @@ const Root = () => {
};
}, [setFeatures, addFeature]);

const rootError: string =
(errors.min && `You must provide at least ${schema.min} response(s)`) ||
(errors.max && `You can provide at most ${schema.max} response(s)`) ||
"";

return (
<Card handleSubmit={validateAndSubmitForm} isValid>
<CardHeader
Expand All @@ -153,13 +158,7 @@ const Root = () => {
howMeasured={howMeasured}
/>
<FullWidthWrapper>
<ErrorWrapper
error={
errors.min
? "Please add at least one feature to the map"
: undefined
}
>
<ErrorWrapper error={rootError}>
<MapContainer environment="standalone">
{/* @ts-ignore */}
<my-map
Expand Down

0 comments on commit b9ea5d3

Please sign in to comment.