Skip to content

Commit

Permalink
Fix add polygon
Browse files Browse the repository at this point in the history
When #141 was merged, it updated the polygon to read from the RTK query
cache. This broke the add polygon feature and is addressed here.
  • Loading branch information
Matt Stone committed Oct 27, 2022
1 parent 3db0dea commit 77c0f59
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/app/src/components/DrawTools/useAddPolygonCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import { useMap } from 'react-leaflet';
import { useDispatch, useSelector } from 'react-redux';
import { DomUtil } from 'leaflet';

import { setPolygon } from '../../store/mapSlice';
import { generateInitialPolygonPoints } from '../../utils';
import { useUpdateBoundaryShapeMutation } from '../../api/boundaries';
import { useBoundaryId, useEndpointToastError } from '../../hooks';
import api from '../../api/api';

export default function useAddPolygonCursor() {
const map = useMap();
const dispatch = useDispatch();
const id = useBoundaryId();

const addPolygonMode = useSelector(state => state.map.addPolygonMode);
const [updateShape, { error }] = useUpdateBoundaryShapeMutation();
useEndpointToastError(error);

const addPolygonFromEvent = useCallback(
event => {
Expand All @@ -18,17 +24,30 @@ export default function useAddPolygonCursor() {
return;
}

map.flyTo(event.latlng);
dispatch(
setPolygon({
points: generateInitialPolygonPoints({
const polygon = {
coordinates: [
generateInitialPolygonPoints({
mapBounds: map.getBounds(),
center: event.latlng,
}),
})
],
};

map.flyTo(event.latlng);

dispatch(
api.util.updateQueryData(
'getBoundaryDetails',
id,
draftDetails => {
draftDetails.submission.shape = polygon;
}
)
);

updateShape({ id, shape: polygon });
},
[map, dispatch]
[map, dispatch, id, updateShape]
);

useEffect(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/app/src/store/mapSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const mapSlice = createSlice({
name: 'map',
initialState,
reducers: {
// TODO Remove polygon from this slice
// useEditingPolygon and useAddPolygonCursor no longer use it
setPolygon: (state, { payload: newPolygon }) => {
state.polygon = {
...DEFAULT_POLYGON,
Expand Down

0 comments on commit 77c0f59

Please sign in to comment.