Skip to content

Commit

Permalink
Show URL polygons in the map on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
nerik committed Oct 6, 2023
1 parent 530acb1 commit 3221d5b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 16 additions & 1 deletion app/scripts/components/common/map/controls/aoi/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import MapboxDraw from '@mapbox/mapbox-gl-draw';
import { useAtomValue } from 'jotai';
import { useRef } from 'react';
import { useControl } from 'react-map-gl';
import { aoisFeaturesAtom } from '$components/exploration/atoms/atoms';

// import type { MapRef } from 'react-map-gl';

Expand All @@ -11,13 +14,25 @@ type DrawControlProps = ConstructorParameters<typeof MapboxDraw>[0] & {
};

export default function DrawControl(props: DrawControlProps) {
const control = useRef<MapboxDraw>();
const aoisFeatures = useAtomValue(aoisFeaturesAtom);

useControl<MapboxDraw>(
() => new MapboxDraw(props),
() => {
control.current = new MapboxDraw(props);
return control.current;
},
({ map }: { map: any }) => {
map.on('draw.create', props.onCreate);
map.on('draw.update', props.onUpdate);
map.on('draw.delete', props.onDelete);
map.on('draw.selectionchange', props.onSelectionChange);
map.on('load', () => {
control.current?.set({
type: 'FeatureCollection',
features: aoisFeatures
});
});
},
({ map }: { map: any }) => {
map.off('draw.create', props.onCreate);
Expand Down
12 changes: 10 additions & 2 deletions app/scripts/components/common/map/controls/hooks/use-aois.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import { useAtomValue, useSetAtom } from 'jotai';
import { useCallback } from 'react';
import { Polygon } from 'geojson';
import { toAoIid } from '../../utils';
import { aoisDeleteAtom, aoisFeaturesAtom, aoisSetSelectedAtom, aoisUpdateGeometryAtom } from '$components/exploration/atoms/atoms';
import {
aoisDeleteAtom,
aoisFeaturesAtom,
aoisSetSelectedAtom,
aoisUpdateGeometryAtom
} from '$components/exploration/atoms/atoms';

export default function useAois() {
const features = useAtomValue(aoisFeaturesAtom);

const aoisUpdateGeometry = useSetAtom(aoisUpdateGeometryAtom);
const onUpdate = useCallback(
(e) => {
const updates = e.features.map((f) => ({ id: toAoIid(f.id), geometry: f.geometry as Polygon }));
const updates = e.features.map((f) => ({
id: toAoIid(f.id),
geometry: f.geometry as Polygon
}));
aoisUpdateGeometry(updates);
},
[aoisUpdateGeometry]
Expand Down

0 comments on commit 3221d5b

Please sign in to comment.