Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a flag to see if mapboxgl draw is initialized #1245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ function CustomAoI({

const [selectedForEditing, setSelectedForEditing] = useAtom(selectedForEditingAtom);

const { onUpdate, isDrawing, setIsDrawing, features } = useAois();
const { drawToolInitialized, onUpdate, isDrawing, setIsDrawing, features } = useAois();
const aoiDeleteAll = useSetAtom(aoiDeleteAllAtom);

// Needed so that this component re-renders to when the draw selection changes
// from feature to point.
const [, forceUpdate] = useState(0);
useEffect(() => {
const mbDraw = map?._drawControl;
if (!mbDraw) return;
if (!drawToolInitialized) return;
const mbDraw = map._drawControl;
const aoiSelectedFor = selectedForEditing ? SIMPLE_SELECT : STATIC_MODE;
const selectedFeatures = features.filter(f => f.selected);

Expand All @@ -102,7 +102,7 @@ function CustomAoI({
return () => {
map.off('draw.selectionchange', onSelChange);
};
}, [selectedForEditing]);
}, [drawToolInitialized, selectedForEditing]);

const resetAoisOnMap = useCallback(() => {
const mbDraw = map?._drawControl;
Expand Down
4 changes: 3 additions & 1 deletion app/scripts/components/common/map/controls/aoi/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function DrawControl(props: DrawControlProps) {
const theme = useTheme();
const control = useRef<MapboxDraw>();
const aoisFeatures = useAtomValue(aoisFeaturesAtom);
const { onUpdate, onDelete, onSelectionChange, onDrawModeChange } = useAois();
const { onDrawToolInitialized, onUpdate, onDelete, onSelectionChange, onDrawModeChange } = useAois();

useControl<MapboxDraw>(
() => {
Expand All @@ -56,6 +56,7 @@ export default function DrawControl(props: DrawControlProps) {
map.on('draw.delete', onDelete);
map.on('draw.selectionchange', onSelectionChange);
map.on('draw.modechange', onDrawModeChange);
map.on('draw.actionable', onDrawToolInitialized);
map.on('load', () => {
control.current?.set({
type: 'FeatureCollection',
Expand All @@ -69,6 +70,7 @@ export default function DrawControl(props: DrawControlProps) {
map.off('draw.delete', onDelete);
map.off('draw.selectionchange', onSelectionChange);
map.off('draw.modechange', onDrawModeChange);
map.off('draw.actionable', onDrawToolInitialized);
},
{
position: 'top-left'
Expand Down
9 changes: 8 additions & 1 deletion app/scripts/components/common/map/controls/hooks/use-aois.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import { useCallback } from 'react';
import { useCallback, useState } from 'react';
import { Feature, Polygon } from 'geojson';
import { toAoIid } from '../../utils';
import {
Expand All @@ -15,6 +15,7 @@ export default function useAois() {
const features = useAtomValue(aoisFeaturesAtom);

const [isDrawing, setIsDrawing] = useAtom(isDrawingAtom);
const [drawToolInitialized, setDrawToolInitialized] = useState(false);

const aoisUpdateGeometry = useSetAtom(aoisUpdateGeometryAtom);
const update = useCallback(
Expand All @@ -34,6 +35,10 @@ export default function useAois() {
[update]
);

const onDrawToolInitialized = () => {
setDrawToolInitialized(true);
};

const aoiDelete = useSetAtom(aoisDeleteAtom);
const onDelete = useCallback(
(e) => {
Expand Down Expand Up @@ -65,7 +70,9 @@ export default function useAois() {
onDelete,
onSelectionChange,
onDrawModeChange,
onDrawToolInitialized,
isDrawing,
drawToolInitialized,
setIsDrawing
};
}