Skip to content

Commit

Permalink
feat(component): update map settings display
Browse files Browse the repository at this point in the history
Pin is now like the Map page and, when clicked, displays the popup

fix ONEARMY#3856
  • Loading branch information
Lahuen Garcia committed Sep 25, 2024
1 parent ab5f426 commit 7af45bd
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 71 deletions.
1 change: 0 additions & 1 deletion packages/components/src/MapWithPin/MapPin.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const Default: StoryFn<typeof MapPin> = () => {
return (
<MapPin
position={position}
draggable={true}
ondragend={(lng: number) => {
position.lng = lng
}}
Expand Down
10 changes: 7 additions & 3 deletions packages/components/src/MapWithPin/MapPin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import L from 'leaflet'

import customMarkerIcon from '../../assets/icons/map-marker.png'

import type { DivIcon } from 'leaflet'

const customMarker = L.icon({
iconUrl: customMarkerIcon,
iconSize: [20, 28],
Expand All @@ -15,16 +17,17 @@ export interface IProps {
lat: number
lng: number
}
draggable: boolean
ondragend(lng: number): void
markerIcon?: DivIcon
onClick?: () => void
}

export const MapPin = (props: IProps) => {
const markerRef = React.useRef(null)

return (
<Marker
draggable={props.draggable}
draggable
ondragend={() => {
const marker: any = markerRef.current

Expand All @@ -39,7 +42,8 @@ export const MapPin = (props: IProps) => {
}}
position={[props.position.lat, props.position.lng]}
ref={markerRef}
icon={customMarker}
icon={props.markerIcon || customMarker}
onclick={props.onClick}
/>
)
}
7 changes: 6 additions & 1 deletion packages/components/src/MapWithPin/MapWithPin.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from 'react'

import { MapWithPin } from './MapWithPin'

import type { Meta, StoryFn } from '@storybook/react'
import type { Map } from 'react-leaflet'

export default {
title: 'Map/MapWithPin',
Expand All @@ -9,10 +12,12 @@ export default {

export const Default: StoryFn<typeof MapWithPin> = () => {
const position = { lat: 0, lng: 0 }
const newMapRef = React.useRef<Map>(null)

return (
<MapWithPin
mapRef={newMapRef}
position={position}
draggable={true}
updatePosition={(_position: { lat: number; lng: number }) => {
position.lat = _position.lat
position.lng = _position.lng
Expand Down
97 changes: 51 additions & 46 deletions packages/components/src/MapWithPin/MapWithPin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,37 @@ import { Map } from '../Map/Map'
import { OsmGeocoding } from '../OsmGeocoding/OsmGeocoding'
import { MapPin } from './MapPin'

import type { LeafletMouseEvent } from 'leaflet'
import type { DivIcon, LeafletMouseEvent } from 'leaflet'
import type { Map as MapType } from 'react-leaflet'
import type { Result } from '../OsmGeocoding/types'

import 'leaflet/dist/leaflet.css'

const useUserLocation = 'Use my current location'
const mapInstructions =
"You can click on the map, or drag the marker to adjust it's position."
"You can double click on the map to adjust it's position. Click on your pin to see the preview of your card."

export interface Props {
mapRef: React.RefObject<MapType>
position: {
lat: number
lng: number
}
draggable: boolean
markerIcon?: DivIcon
updatePosition?: any
center?: any
zoom?: number
hasUserLocation?: boolean
onClickMapPin?: () => void
popup?: React.ReactNode
}

export const MapWithPin = (props: Props) => {
const [zoom, setZoom] = React.useState(props.zoom || 1)
const [center, setCenter] = React.useState(
props.center || [props.position.lat, props.position.lng],
)
const { draggable, position } = props
const { mapRef, position, markerIcon, onClickMapPin, popup } = props

const hasUserLocation = props.hasUserLocation || false
const onPositionChanged =
Expand All @@ -58,22 +62,20 @@ export const MapWithPin = (props: Props) => {
)
}

const onClick = (evt: LeafletMouseEvent) => {
const onDblClick = (evt: LeafletMouseEvent) => {
onPositionChanged({ ...evt.latlng })
}

return (
<Flex sx={{ flexDirection: 'column', gap: 2 }}>
{draggable && (
<Alert
variant="info"
sx={{
marginTop: 2,
}}
>
<Text sx={{ fontSize: 1 }}>{mapInstructions}</Text>
</Alert>
)}
<Alert
variant="info"
sx={{
marginTop: 2,
}}
>
<Text sx={{ fontSize: 1 }}>{mapInstructions}</Text>
</Alert>
<div
style={{
position: 'relative',
Expand All @@ -90,52 +92,55 @@ export const MapWithPin = (props: Props) => {
zIndex: 2,
}}
>
{draggable && (
<Flex style={{ width: '280px' }}>
<OsmGeocoding
callback={(data: Result) => {
if (data.lat && data.lon) {
onPositionChanged({
lat: data.lat,
lng: data.lon,
})
setCenter([data.lat, data.lon])
setZoom(15)
}
<Flex style={{ width: '280px' }}>
<OsmGeocoding
callback={(data: Result) => {
if (data.lat && data.lon) {
onPositionChanged({
lat: data.lat,
lng: data.lon,
})
setCenter([data.lat, data.lon])
setZoom(15)
}
}}
countrycodes=""
acceptLanguage="en"
/>
{hasUserLocation && (
<Button
type="button"
mx={2}
onClick={(evt) => {
evt.preventDefault()
setLocationToNavigatorLocation()
}}
countrycodes=""
acceptLanguage="en"
/>
{hasUserLocation && (
<Button
type="button"
mx={2}
onClick={(evt) => {
evt.preventDefault()
setLocationToNavigatorLocation()
}}
>
{useUserLocation}
</Button>
)}
</Flex>
)}
>
{useUserLocation}
</Button>
)}
</Flex>
</Box>
<Map
ref={mapRef}
className="markercluster-map"
center={center}
zoom={zoom}
zoomControl={false}
setZoom={setZoom}
onclick={onClick}
ondblclick={onDblClick}
doubleClickZoom={false}
style={{
height: '300px',
zIndex: 1,
}}
>
{popup}
<ZoomControl position="topright" />
<MapPin
position={position}
draggable={draggable}
markerIcon={markerIcon}
onClick={onClickMapPin}
ondragend={(evt: any) => {
if (evt.lat && evt.lng)
onPositionChanged({
Expand Down
3 changes: 2 additions & 1 deletion src/models/common.models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export interface ILocation {
postcode: string
value: string
}
interface ILatLng {

export interface ILatLng {
lat: number
lng: number
}
Expand Down
6 changes: 0 additions & 6 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { IComment } from './discussion.models'

export * from './common.models'
export * from './discussion.models'
export * from './howto.models'
Expand All @@ -14,7 +12,3 @@ export * from './tags.model'
export * from './user.models'
export * from './moderation.model'
export * from './userPreciousPlastic.models'

export interface UserComment extends IComment {
isEditable: boolean
}
6 changes: 4 additions & 2 deletions src/pages/Maps/Content/MapView/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MapMemberCard, PinProfile } from 'oa-components'
import { IModerationStatus } from 'oa-shared'
import { MAP_GROUPINGS } from 'src/stores/Maps/maps.groupings'

import type { ILatLng } from 'oa-shared'
import type { Map } from 'react-leaflet'
import type { IMapPin, IMapPinWithDetail } from 'src/models/maps.models'

Expand All @@ -15,12 +16,13 @@ interface IProps {
mapRef: React.RefObject<Map>
newMap?: boolean
onClose?: () => void
customPosition?: ILatLng
}

export const Popup = (props: IProps) => {
const leafletRef = useRef<LeafletPopup>(null)
const activePin = props.activePin as IMapPinWithDetail
const { mapRef, newMap, onClose } = props
const { mapRef, newMap, onClose, customPosition } = props

useEffect(() => {
openPopup()
Expand All @@ -47,7 +49,7 @@ export const Popup = (props: IProps) => {
activePin.location && (
<LeafletPopup
ref={leafletRef}
position={[activePin.location.lat, activePin.location.lng]}
position={customPosition ? customPosition : [activePin.location.lat, activePin.location.lng]}
offset={new L.Point(2, -10)}
closeButton={false}
className={activePin !== undefined ? '' : 'closed'}
Expand Down
15 changes: 15 additions & 0 deletions src/pages/UserSettings/SettingsPageMapPin.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ vi.mock('src/common/hooks/useCommonStores', () => ({
},
mapsStore: {
getPin: vi.fn().mockResolvedValue(mockPin),
getPinDetail: vi.fn().mockResolvedValue(mockPin),
},
themeStore: {
currentTheme: {
id: 'string',
siteName: 'string',
logo: 'string',
badge: 'string',
avatar: 'string',
howtoHeading: 'string',
academyResource: 'string',
styles: {
communityProgramURL: '',
},
},
},
},
}),
Expand Down
Loading

0 comments on commit 7af45bd

Please sign in to comment.