Skip to content

Commit 239fe07

Browse files
committed
set and zoom map to search center as set in user preference settings
1 parent 7836012 commit 239fe07

File tree

5 files changed

+72
-43
lines changed

5 files changed

+72
-43
lines changed

.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ NEXT_PUBLIC_API_URL=http://localhost:8001/api/v1
88
NEXT_PUBLIC_PI_SDK_URL=https://sdk.minepi.com/pi-sdk.js
99

1010
NEXT_PUBLIC_SENTRY_DSN="ADD YOUR SENTRY DSN"
11-
NEXT_PUBLIC_IMAGE_PLACEHOLDER_URL="ADD YOUR IMAGE PLACEHOLDER URL"
11+
NEXT_PUBLIC_IMAGE_PLACEHOLDER_URL='/path/to/default/image.png'

src/app/[locale]/page.tsx

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { useContext, useEffect, useState, useRef } from 'react';
1111
import { Button } from '@/components/shared/Forms/Buttons/Buttons';
1212
import SearchBar from '@/components/shared/SearchBar/SearchBar';
1313
import { fetchSellers } from '@/services/sellerApi';
14-
import { fetchUserLocation } from '@/services/userSettingsApi';
14+
import { fetchUserLocation, fetchUserSettings } from '@/services/userSettingsApi';
15+
import { DeviceLocationType, IUserSettings } from '@/constants/types';
1516

1617
import { AppContext } from '../../../context/AppContextProvider';
1718
import logger from '../../../logger.config.mjs';
@@ -27,35 +28,66 @@ export default function Index() {
2728
lat: 0,
2829
lng: 0,
2930
});
31+
const [searchCenter, setSetSearchCenter] = useState<{ lat: number; lng: number }>({
32+
lat: 0,
33+
lng: 0,
34+
});
35+
const [findme, setFindme] = useState<DeviceLocationType>(DeviceLocationType.SearchCenter);
36+
const [dbUserSettings, setDbUserSettings] = useState<IUserSettings | null>(null);
3037
const [zoomLevel, setZoomLevel] = useState(2);
3138
const [locationError, setLocationError] = useState<string | null>(null);
3239
const [searchQuery, setSearchQuery] = useState<string>('');
3340
const [isSearchClicked, setSearchClicked] = useState(false);
3441
const [searchResults, setSearchResults] = useState<any[]>([]);
3542

36-
const { isSigningInUser } = useContext(AppContext);
43+
const { isSigningInUser, currentUser, autoLoginUser } = useContext(AppContext);
3744

3845
// Default map center (example: New York City)
3946
const defaultMapCenter = { lat: 20, lng: -74.006 };
4047

4148
useEffect(() => {
42-
const fetchLocationOnLoad = async () => {
49+
if (!currentUser) {
50+
logger.info("User not logged in; attempting auto-login..");
51+
autoLoginUser();
52+
}
53+
54+
const getUserSettingsData = async () => {
4355
try {
44-
const location = await fetchUserLocation();
45-
setMapCenter(location.origin);
46-
setZoomLevel(location.radius);
47-
logger.info('User location obtained successfully on initial load:', {
48-
location,
49-
});
56+
const data = await fetchUserSettings();
57+
if (data) {
58+
console.log('Fetched user settings data successfully: ', data.findme)
59+
logger.info('Fetched user settings data successfully:', { data });
60+
setDbUserSettings(data);
61+
setSetSearchCenter(data.search_map_center.coordinates)
62+
} else {
63+
logger.warn('User Settings not found.');
64+
setDbUserSettings(null);
65+
}
5066
} catch (error) {
51-
logger.error('Error getting location on initial load.', { error });
52-
setMapCenter(defaultMapCenter);
53-
setZoomLevel(2);
67+
logger.error('Error fetching user settings data:', { error });
5468
}
5569
};
70+
getUserSettingsData();
71+
}, [currentUser]);
72+
73+
// useEffect(() => {
74+
// const fetchLocationOnLoad = async () => {
75+
// try {
76+
// const location = await fetchUserLocation();
77+
// setMapCenter(location.origin);
78+
// setZoomLevel(location.radius);
79+
// logger.info('User location obtained successfully on initial load:', {
80+
// location,
81+
// });
82+
// } catch (error) {
83+
// logger.error('Error getting location on initial load.', { error });
84+
// setMapCenter(defaultMapCenter);
85+
// setZoomLevel(2);
86+
// }
87+
// };
5688

57-
fetchLocationOnLoad();
58-
}, [isSigningInUser]);
89+
// fetchLocationOnLoad();
90+
// }, [isSigningInUser]);
5991

6092
const handleLocationButtonClick = async () => {
6193
try {
@@ -95,7 +127,7 @@ export default function Index() {
95127
return (
96128
<>
97129
<DynamicMap
98-
center={[mapCenter.lat, mapCenter.lng]}
130+
center={searchCenter}
99131
zoom={zoomLevel}
100132
mapRef={mapRef}
101133
searchQuery={searchQuery}

src/components/shared/map/Map.tsx

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ const Map = ({
7272
popupAnchor: [1, -34],
7373
});
7474

75+
// Define the crosshair icon for the center of the map
76+
const crosshairIcon = new L.Icon({
77+
iconUrl: '/images/icons/crosshair.png',
78+
iconSize: [100, 100],
79+
iconAnchor: [60, 60],
80+
});
81+
7582
const [position, setPosition] = useState<L.LatLng | null>(null);
7683
const [sellers, setSellers] = useState<ISellerWithSettings[]>([]);
7784
const [origin, setOrigin] = useState(center);
@@ -173,6 +180,8 @@ const Map = ({
173180
logger.warn('Map instance is not ready yet');
174181
return;
175182
}
183+
console.log("initial user center:", center.toString())
184+
mapInstance.setView(center, 8, { animate: true })
176185

177186
const bounds = mapInstance.getBounds();
178187
if (bounds) {
@@ -203,32 +212,10 @@ const Map = ({
203212

204213
logger.info('Fetched additional sellers:', { additionalSellers });
205214

206-
// Filter sellers within the new bounds, checking if coordinates are defined
207-
const filteredSellers = additionalSellers.filter(
208-
(seller) =>
209-
seller.coordinates &&
210-
newBounds.contains([seller.coordinates[0], seller.coordinates[1]])
211-
);
212-
logger.info('Filtered sellers within bounds', { filteredSellers });
213-
214-
// Filter out sellers that are not within the new bounds from the existing sellers
215-
const remainingSellers = sellers.filter(
216-
(seller) =>
217-
seller.coordinates &&
218-
newBounds.contains([seller.coordinates[0], seller.coordinates[1]])
219-
);
220-
logger.info('Remaining sellers within bounds:', { remainingSellers });
221-
222-
// Combine remaining and filtered sellers, remove duplicates, and cap at 36 sellers
223-
const updatedSellers = removeDuplicates([...remainingSellers, ...filteredSellers]);
224-
225-
// Log the combined sellers before slicing
226-
logger.info('Combined sellers (before capping at 36):', { updatedSellers });
227-
228-
setSellers(updatedSellers.slice(0, 36)); // Cap the total sellers to 36
215+
setSellers(additionalSellers); // Cap the total sellers to 36
229216

230217
logger.info('Sellers after capping at 36:', {
231-
updatedSellers: updatedSellers.slice(0, 36),
218+
additionalSellers: additionalSellers,
232219
});
233220

234221
} catch (error) {
@@ -351,6 +338,10 @@ const Map = ({
351338
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
352339
noWrap={true}
353340
/>
341+
<Marker
342+
position={center as LatLngExpression}
343+
icon={crosshairIcon}
344+
></Marker>
354345
<LocationMarker />
355346
{sellers.map((seller) => (
356347
<Marker

src/components/shared/sidebar/sidebar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ function Sidebar(props: any) {
291291

292292
const translateFindMeOptions = [
293293
{
294-
value: 'auto',
294+
value: 'Auto',
295295
name: t('SIDE_NAVIGATION.FIND_ME_OPTIONS.PREFERRED_AUTO'),
296296
},
297297
{
298-
value: 'deviceGPS',
298+
value: 'GPS',
299299
name: t('SIDE_NAVIGATION.FIND_ME_OPTIONS.PREFERRED_DEVICE_GPS'),
300300
},
301301
{

src/constants/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,10 @@ export type PartialReview = {
7272
receiver: string;
7373
}
7474

75-
export interface IReviewOutput extends IReviewFeedback, PartialReview {}
75+
export interface IReviewOutput extends IReviewFeedback, PartialReview {}
76+
77+
export enum DeviceLocationType {
78+
Automatic = 'Auto',
79+
GPS = 'GPS',
80+
SearchCenter = 'searchCenter'
81+
}

0 commit comments

Comments
 (0)