Skip to content

Commit

Permalink
fit to draw area bounds on edit
Browse files Browse the repository at this point in the history
  • Loading branch information
j8seangel committed Aug 14, 2024
1 parent 4d7accb commit 0289606
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/fishing-map/features/areas/areas.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { RootState } from 'store'
import { listAsSentence } from 'utils/shared'
import { t } from 'features/i18n/i18n'

type DrawnDatasetGeometry = FeatureCollection<Polygon, { draw_id: number }>
export type DrawnDatasetGeometry = FeatureCollection<Polygon, { draw_id: number }>

interface DatasetArea {
id: string
Expand Down
22 changes: 20 additions & 2 deletions apps/fishing-map/features/map/overlays/draw/DrawDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useCallback, useEffect, useState } from 'react'
import bbox from '@turf/bbox'
import cx from 'classnames'
import { useTranslation } from 'react-i18next'
import { Feature, Polygon } from 'geojson'
import { useSelector } from 'react-redux'
import { Button, InputText, IconButton, SwitchRow } from '@globalfishingwatch/ui-components'
import { DrawFeatureType } from '@globalfishingwatch/deck-layers'
import { useMapFitBounds } from 'features/map/map-bounds.hooks'
import { useLocationConnect } from 'routes/routes.hook'
import {
useAddDataviewFromDatasetToWorkspace,
Expand All @@ -17,9 +19,11 @@ import {
resetAreaList,
fetchDatasetAreasThunk,
selectDatasetAreasById,
DrawnDatasetGeometry,
} from 'features/areas/areas.slice'
import { AsyncReducerStatus } from 'utils/async-slice'
import { selectMapDrawingEditId, selectMapDrawingMode } from 'routes/routes.selectors'
import { Bbox } from 'types'
import { useMapDrawConnect } from '../../map-draw.hooks'
import { getDrawDatasetDefinition, getFileWithFeatures } from './draw.utils'
import styles from './DrawDialog.module.css'
Expand All @@ -31,6 +35,7 @@ const MIN_DATASET_NAME_LENGTH = 3
function MapDraw() {
const { t } = useTranslation()
const dispatch = useAppDispatch()
const fitMapBounds = useMapFitBounds()
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
const [layerName, setLayerName] = useState<string>('')
Expand All @@ -48,12 +53,25 @@ function MapDraw() {
const drawFeaturesIndexes = drawLayer?.getSelectedFeatureIndexes() || []
const hasOverlappingFeatures = drawLayer?.getHasOverlappingFeatures()

const fetchDrawArea = useCallback(
async (datasetId: string) => {
const fetchDatasetAreasAction = await dispatch(fetchDatasetAreasThunk({ datasetId }))
if (fetchDatasetAreasThunk.fulfilled.match(fetchDatasetAreasAction)) {
const areaBbox = bbox(fetchDatasetAreasAction.payload as DrawnDatasetGeometry) as Bbox
if (areaBbox) {
fitMapBounds(areaBbox, { padding: 150, fitZoom: true })
}
}
},
[dispatch, fitMapBounds]
)

useEffect(() => {
if (mapDrawEditDataset) {
setLayerName(mapDrawEditDataset.name)
dispatch(fetchDatasetAreasThunk({ datasetId: mapDrawEditDataset.id }))
fetchDrawArea(mapDrawEditDataset.id)
}
}, [dispatch, mapDrawEditDataset])
}, [dispatch, fetchDrawArea, mapDrawEditDataset])

useEffect(() => {
if (
Expand Down

0 comments on commit 0289606

Please sign in to comment.