Skip to content

Commit

Permalink
map: format updated location value
Browse files Browse the repository at this point in the history
  • Loading branch information
SteRiccio committed Sep 15, 2023
1 parent 9110259 commit 651c813
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
23 changes: 10 additions & 13 deletions webapp/components/Map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import React from 'react'
import { MapContainer, ScaleControl } from 'react-leaflet'
import PropTypes from 'prop-types'

import { Points } from '@openforis/arena-core'

import { ButtonSave } from '@webapp/components'

import i18n from '@core/i18n/i18nFactory'
Expand Down Expand Up @@ -35,7 +33,7 @@ const INITIAL_ZOOM_LEVEL = 3

export const Map = (props) => {
const { editable, layers, markerPoint, markerTitle, showOptions } = props
const { centerPositionLatLon, mapEventHandlers, markerPointUpdated, onMarkerPointUpdated, onSaveClick } =
const { centerPositionLatLon, markerPointUpdated, markerPointUpdatedToString, onMarkerPointUpdated, onSaveClick } =
useMap(props)

if (!centerPositionLatLon) {
Expand All @@ -47,12 +45,7 @@ export const Map = (props) => {
{editable && <div className="location-edit-info">{i18n.t('mapView.locationEditInfo')}</div>}

<MapContextProvider>
<MapContainer
center={centerPositionLatLon}
doubleClickZoom={false}
zoom={INITIAL_ZOOM_LEVEL}
eventHandlers={mapEventHandlers}
>
<MapContainer center={centerPositionLatLon} doubleClickZoom={false} zoom={INITIAL_ZOOM_LEVEL}>
<ScaleControl position="topleft" />
<MapLayersControl layers={layers} />
<MapMarker
Expand All @@ -61,9 +54,13 @@ export const Map = (props) => {
onPointUpdated={onMarkerPointUpdated}
title={markerTitle}
/>
{showOptions && <MapOptionsEditor />}
<KmlUploader />
<MapBaseLayerPeriodSelector />
{showOptions && (
<>
<MapOptionsEditor />
<KmlUploader />
<MapBaseLayerPeriodSelector />
</>
)}
<ShowZoomLevel />
{/* <WmtsComponent /> */}
</MapContainer>
Expand All @@ -74,7 +71,7 @@ export const Map = (props) => {
{markerPointUpdated && (
<div className="location-updated-label">
<label>
{i18n.t('mapView.locationUpdated')}:<span>{Points.toString(markerPointUpdated)}</span>
{i18n.t('mapView.locationUpdated')}:<span> {markerPointUpdatedToString}</span>
</label>
</div>
)}
Expand Down
21 changes: 6 additions & 15 deletions webapp/components/Map/useMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'

import { PointFactory, Points } from '@openforis/arena-core'

Expand Down Expand Up @@ -43,19 +43,6 @@ export const useMap = (props) => {
}))
}, [centerPoint, fromPointToLatLon, markerPoint, srsIndex])

const mapEventHandlers = useMemo(
() => ({
dblclick(event) {
const [latitude, longitute] = event.latlng
setState((statePrev) => ({
...statePrev,
markerPositionLatLon: PointFactory.createInstance({ x: longitute, y: latitude }),
}))
},
}),
[]
)

const onMarkerPointUpdated = useCallback((markerPointUpdated) => {
setState((statePrev) => ({ ...statePrev, markerPointUpdated }))
}, [])
Expand All @@ -64,10 +51,14 @@ export const useMap = (props) => {
onMarkerPointChange(markerPointUpdated)
}, [markerPointUpdated, onMarkerPointChange])

const markerPointUpdatedToString = markerPointUpdated
? `${markerPointUpdated.y.toFixed(5)}, ${markerPointUpdated.x.toFixed(5)}`
: ''

return {
centerPositionLatLon,
mapEventHandlers,
markerPointUpdated,
markerPointUpdatedToString,
onMarkerPointUpdated,
onSaveClick,
}
Expand Down

0 comments on commit 651c813

Please sign in to comment.