Skip to content

Commit

Permalink
basic back/change navigation re-added
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Sep 6, 2024
1 parent a9211bc commit a54c9b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ interface MapAndLabelContextValue {
features?: Feature[];
updateMapKey: number;
activeIndex: number;
editFeature: (index: number) => void;
formik: FormikProps<SchemaUserData>;
validateAndSubmitForm: () => void;
isFeatureInvalid: (index: number) => boolean;
addInitialFeaturesToMap: (features: Feature[]) => void;
editFeature: (index: number) => void;
copyFeature: (sourceIndex: number, destinationIndex: number) => void;
removeFeature: (index: number) => void;
mapAndLabelProps: PresentationalProps;
Expand Down Expand Up @@ -151,8 +152,13 @@ export const MapAndLabelProvider: React.FC<MapAndLabelProviderProps> = (

const addFeatureToMap = (geojson: GeoJSONChange) => {
resetErrors();

setFeatures(geojson["EPSG:3857"].features);
setActiveIndex((features && features?.length - 2) || activeIndex + 1);
};

const addInitialFeaturesToMap = (features: Feature[]) => {
setFeatures(features);
// TODO: setActiveIndex ?
};

const addFeatureToForm = () => {
Expand Down Expand Up @@ -189,15 +195,13 @@ export const MapAndLabelProvider: React.FC<MapAndLabelProviderProps> = (
(f) => f.properties?.label !== label,
);

// If the removed feature was not the final one in the array, shift all feature labels after the removed feature so they remain incremental
if (features && features?.length > index) {
filteredFeatures?.map((f) => {
if (f.properties && Number(f.properties?.label) > Number(label)) {
const newLabel = Number(f.properties.label) - 1;
Object.assign(f, { properties: { label: `${newLabel}` } });
}
});
}
// Shift any feature labels that are larger than the removed feature label so they remain incremental
filteredFeatures?.map((f) => {
if (f.properties && Number(f.properties?.label) > Number(label)) {
const newLabel = Number(f.properties.label) - 1;
Object.assign(f, { properties: { label: `${newLabel}` } });
}
});
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
Expand All @@ -220,9 +224,10 @@ export const MapAndLabelProvider: React.FC<MapAndLabelProviderProps> = (
activeIndex,
schema,
mapAndLabelProps: props,
editFeature,
formik,
validateAndSubmitForm,
addInitialFeaturesToMap,
editFeature,
copyFeature,
removeFeature,
isFeatureInvalid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,12 @@ const Root = () => {
schema,
updateMapKey,
validateAndSubmitForm,
addInitialFeaturesToMap,
} = useMapAndLabelContext();
const {
title,
description,
fn,
info,
policyRef,
howMeasured,
Expand All @@ -217,8 +219,15 @@ const Root = () => {
latitude,
longitude,
boundaryBBox,
previouslySubmittedData,
} = mapAndLabelProps;

// If coming "back" or "changing", load initial features & tabs onto the map
// Pre-populating form fields within tabs is handled via formik.initialValues in Context.tsx
if (previouslySubmittedData?.data?.[fn]?.features?.length > 0) {
addInitialFeaturesToMap(previouslySubmittedData?.data?.[fn]?.features);
}

const rootError: string =
(errors.min &&
`You must plot at least ${schema.min} ${schema.type}(s) on the map`) ||
Expand Down

0 comments on commit a54c9b6

Please sign in to comment.