Skip to content

Commit

Permalink
fix remove after first bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Sep 6, 2024
1 parent 0e59f17 commit 600d650
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const MAP_ID = "map-and-label-map";
interface MapAndLabelContextValue {
schema: Schema;
features?: Feature[];
updateMap: boolean;
updateMapKey: number;
activeIndex: number;
editFeature: (index: number) => void;
formik: FormikProps<SchemaUserData>;
Expand Down Expand Up @@ -118,7 +118,7 @@ export const MapAndLabelProvider: React.FC<MapAndLabelProviderProps> = (

const [features, setFeatures] = useGeoJSONChange(MAP_ID, handleGeoJSONChange);

const [updateMap, setUpdateMap] = useState<boolean>(false);
const [updateMapKey, setUpdateMapKey] = useState<number>(0);

const resetErrors = () => {
setMinError(false);
Expand Down Expand Up @@ -183,9 +183,14 @@ export const MapAndLabelProvider: React.FC<MapAndLabelProviderProps> = (
};

const removeFeatureFromMap = (index: number) => {
setFeatures(features?.filter((_, i) => i !== index));
// `updateMap` is set as the `key` prop on the map container to force a re-render of its children (aka <my-map />) on change
setUpdateMap(true);
// Order of features varies by change/modification, filter on label not array position
const filteredFeatures = features?.filter(
(feature) => feature.properties?.label !== `${index + 1}`,
);
// TODO: shift labels so they remain incremental
setFeatures(filteredFeatures);
// `updateMapKey` is set as a unique `key` prop on the map container to force a re-render of its children (aka <my-map />) on change
setUpdateMapKey(updateMapKey + 1);
};

const removeFeature = (index: number) => {
Expand All @@ -199,7 +204,7 @@ export const MapAndLabelProvider: React.FC<MapAndLabelProviderProps> = (
<MapAndLabelContext.Provider
value={{
features,
updateMap,
updateMapKey,
activeIndex,
schema,
mapAndLabelProps: props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const Root = () => {
features,
mapAndLabelProps,
schema,
updateMap,
updateMapKey,
validateAndSubmitForm,
} = useMapAndLabelContext();
const {
Expand Down Expand Up @@ -236,12 +236,12 @@ const Root = () => {
howMeasured={howMeasured}
/>
<FullWidthWrapper>
<ErrorWrapper error={rootError} key={updateMap ? 1 : 0}>
<ErrorWrapper error={rootError} key={updateMapKey}>
<MapContainer environment="standalone">
{/* @ts-ignore */}
<my-map
id={MAP_ID}
data-testid="map-and-label-map"
data-testid={MAP_ID}
basemap={basemap}
ariaLabelOlFixedOverlay={`An interactive map for plotting and describing individual ${schemaName.toLocaleLowerCase()}`}
drawMode
Expand Down

0 comments on commit 600d650

Please sign in to comment.