Skip to content

Commit 316ddb9

Browse files
committed
Misc PR adjustments; change zoom reset behavior + uncrop images.
1 parent ff8623d commit 316ddb9

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

src/app/[locale]/seller/sale-items/[id]/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export default function BuyFromSellerForm({ params }: { params: { id: string } }
174174
alt="seller logo"
175175
fill
176176
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
177-
style={{ objectFit: 'cover', maxHeight: '200px', maxWidth: '100%' }}
177+
style={{ objectFit: 'contain', maxHeight: '200px', maxWidth: '100%' }}
178178
/>
179179
</div>
180180
<div className="my-auto">

src/components/shared/map/Map.tsx

+34-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useTranslations } from 'next-intl';
22
import Image from 'next/image';
3-
import React, { useEffect, useState, useCallback, useContext } from 'react';
3+
import React, { useEffect, useState, useCallback, useContext, useRef } from 'react';
44
import { MapContainer, Marker, Popup, TileLayer, useMapEvents } from 'react-leaflet';
55
import L, { LatLngExpression, LatLngBounds, LatLngTuple } from 'leaflet';
66
import _ from 'lodash';
@@ -132,39 +132,44 @@ const Map = ({
132132
logger.debug('Sellers Array:', { sellers });
133133
}, [sellers]);
134134

135-
// Function to handle marker click
136-
const handleMarkerClick = (sellerCoordinates: LatLngTuple) => {
137-
if (!mapRef.current) return;
135+
const useMarkerZoomHandler = (mapRef: React.RefObject<L.Map>) => {
136+
const lastClickedMarker = useRef<string | null>(null);
138137

139-
const map = mapRef.current;
140-
const currentZoom = map.getZoom();
141-
const maxZoom = map.getMaxZoom();
142-
143-
// Check if zoom & pan have already been applied
144-
const hasZoomed = mapRef.current?.getContainer().dataset.zoomApplied === "true";
145-
146-
if (hasZoomed) return; // Prevent further zoom & pan on second click
138+
// Function to handle marker click
139+
const handleMarkerClick = (sellerCoordinates: LatLngTuple) => {
140+
if (!mapRef.current) return;
147141

148-
// Apply zoom increase only once
149-
const targetZoom = Math.min(currentZoom + 3, maxZoom);
150-
const zoomLevel = targetZoom;
142+
const map = mapRef.current;
143+
const currentZoom = map.getZoom();
144+
const maxZoom = map.getMaxZoom();
151145

152-
// Convert marker lat/lng to pixel position
153-
const markerPoint = map.latLngToContainerPoint(sellerCoordinates);
146+
const coordKey = sellerCoordinates.join(',');
154147

155-
// Adjust popup position: move **left (-X) and up (-Y)**
156-
const offsetX = -3; // Slight left shift
157-
const offsetY = 28; // Moves up instead of down
158-
const panOffset = L.point(offsetX, offsetY);
148+
// Prevent zooming again on the same marker
149+
if (lastClickedMarker.current === coordKey) return;
159150

160-
// Calculate new center position based on offset
161-
const newCenter = map.containerPointToLatLng(markerPoint.subtract(panOffset));
151+
// Update the last clicked marker
152+
lastClickedMarker.current = coordKey;
162153

163-
// Apply view changes **only once**
164-
map.setView(newCenter, zoomLevel, { animate: false });
154+
// Calculate target zoom
155+
const targetZoom = Math.min(currentZoom + 3, maxZoom);
165156

166-
// Mark zoom as applied (prevents further zooming & movement)
167-
mapRef.current.getContainer().dataset.zoomApplied = "true";
157+
// Convert lat/lng to pixel position
158+
const markerPoint = map.latLngToContainerPoint(sellerCoordinates);
159+
160+
// Offset to move popup up and left
161+
const OFFSET_X = -3;
162+
const OFFSET_Y = 28;
163+
const panOffset = L.point(OFFSET_X, OFFSET_Y);
164+
165+
// New center for map
166+
const newCenter = map.containerPointToLatLng(markerPoint.subtract(panOffset));
167+
168+
// Zoom and pan with animation
169+
map.setView(newCenter, targetZoom, { animate: true });
170+
};
171+
172+
return handleMarkerClick;
168173
};
169174

170175
useEffect(() => {
@@ -173,6 +178,8 @@ const Map = ({
173178
}
174179
}, [mapRef.current]);
175180

181+
const handleMarkerClick = useMarkerZoomHandler(mapRef);
182+
176183
const saveMapState = () => {
177184
try{
178185
if (!mapRef.current) {

src/components/shared/map/MapMarkerPopup.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ const MapMarkerPopup = ({ seller }: { seller: any }) => {
6565
<Image
6666
src={imageUrl}
6767
alt="Seller Image"
68-
width={imageUrl === '/images/logo.svg' ? 80 : 150}
69-
height={imageUrl === '/images/logo.svg' ? 40 : 70}
68+
width={150}
69+
height={70}
7070
style={{
71-
objectFit: imageUrl === '/images/logo.svg' ? 'contain' : 'cover',
72-
width: imageUrl === '/images/logo.svg' ? '80px' : '100%',
73-
height: imageUrl === '/images/logo.svg' ? '40px' : '100%',
71+
objectFit: 'contain',
72+
width: '100%',
73+
height: '100%',
7474
}}
7575
/>
7676
</div>

0 commit comments

Comments
 (0)