1
1
'use client' ;
2
2
3
3
import L from 'leaflet' ;
4
-
5
4
import { useTranslations } from 'next-intl' ;
6
5
import dynamic from 'next/dynamic' ;
7
6
import Image from 'next/image' ;
@@ -11,7 +10,9 @@ import { useContext, useEffect, useState, useRef } from 'react';
11
10
import { Button } from '@/components/shared/Forms/Buttons/Buttons' ;
12
11
import SearchBar from '@/components/shared/SearchBar/SearchBar' ;
13
12
import { fetchSellers } from '@/services/sellerApi' ;
14
- import { fetchUserLocation } from '@/services/userSettingsApi' ;
13
+ import { fetchUserSettings } from '@/services/userSettingsApi' ;
14
+ import { DeviceLocationType , IUserSettings } from '@/constants/types' ;
15
+ import { userLocation } from '@/utils/geolocation' ;
15
16
16
17
import { AppContext } from '../../../context/AppContextProvider' ;
17
18
import logger from '../../../logger.config.mjs' ;
@@ -23,58 +24,92 @@ export default function Index() {
23
24
} ) ;
24
25
const mapRef = useRef < L . Map | null > ( null ) ;
25
26
26
- const [ mapCenter , setMapCenter ] = useState < { lat : number ; lng : number } > ( {
27
- lat : 0 ,
28
- lng : 0 ,
29
- } ) ;
27
+ // State management with proper typing
28
+ const [ mapCenter , setMapCenter ] = useState < { lat : number ; lng : number } | null > ( null ) ;
29
+ const [ searchCenter , setSearchCenter ] = useState < { lat : number ; lng : number } | null > ( null ) ;
30
+ const [ findme , setFindme ] = useState < DeviceLocationType > ( DeviceLocationType . SearchCenter ) ;
31
+ const [ dbUserSettings , setDbUserSettings ] = useState < IUserSettings | null > ( null ) ;
30
32
const [ zoomLevel , setZoomLevel ] = useState ( 2 ) ;
31
33
const [ locationError , setLocationError ] = useState < string | null > ( null ) ;
32
34
const [ searchQuery , setSearchQuery ] = useState < string > ( '' ) ;
33
35
const [ isSearchClicked , setSearchClicked ] = useState ( false ) ;
34
36
const [ searchResults , setSearchResults ] = useState < any [ ] > ( [ ] ) ;
35
37
36
- const { isSigningInUser } = useContext ( AppContext ) ;
37
-
38
- // Default map center (example: New York City)
39
- const defaultMapCenter = { lat : 20 , lng : - 74.006 } ;
38
+ const { isSigningInUser, currentUser, autoLoginUser, reload, setReload } = useContext ( AppContext ) ;
40
39
41
40
useEffect ( ( ) => {
42
- const fetchLocationOnLoad = async ( ) => {
41
+ setReload ( false )
42
+ if ( ! currentUser ) {
43
+ logger . info ( "User not logged in; attempting auto-login.." ) ;
44
+ autoLoginUser ( ) ;
45
+ }
46
+
47
+ const getUserSettingsData = async ( ) => {
43
48
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
- } ) ;
49
+ const data = await fetchUserSettings ( ) ;
50
+ if ( data ) {
51
+ logger . info ( 'Fetched user settings data successfully:' , { data } ) ;
52
+ setDbUserSettings ( data ) ;
53
+ if ( data . search_map_center ?. coordinates ) {
54
+ setSearchCenter ( {
55
+ lat : data . search_map_center . coordinates [ 1 ] ,
56
+ lng : data . search_map_center . coordinates [ 0 ] ,
57
+ } ) ;
58
+ }
59
+ } else {
60
+ logger . warn ( 'User Settings not found.' ) ;
61
+ setDbUserSettings ( null ) ;
62
+ setSearchCenter ( null )
63
+ }
50
64
} catch ( error ) {
51
- logger . error ( 'Error getting location on initial load.' , { error } ) ;
52
- setMapCenter ( defaultMapCenter ) ;
53
- setZoomLevel ( 2 ) ;
65
+ logger . error ( 'Error fetching user settings data:' , { error } ) ;
54
66
}
55
67
} ;
56
68
57
- fetchLocationOnLoad ( ) ;
58
- } , [ isSigningInUser ] ) ;
69
+ getUserSettingsData ( ) ;
70
+ } , [ currentUser , reload ] ) ;
71
+
72
+ useEffect ( ( ) => {
73
+ const resolveLocation = async ( ) => {
74
+ if ( dbUserSettings && dbUserSettings . findme !== DeviceLocationType . SearchCenter ) {
75
+ const loc = await userLocation ( dbUserSettings ) ;
76
+ if ( loc ) {
77
+ setSearchCenter ( { lat : loc [ 0 ] , lng : loc [ 1 ] } ) ;
78
+ }
79
+ else {
80
+ setSearchCenter ( null )
81
+ }
82
+ }
83
+ } ;
84
+ resolveLocation ( ) ;
85
+ } , [ dbUserSettings ] ) ;
59
86
60
87
const handleLocationButtonClick = async ( ) => {
61
- try {
62
- const location = await fetchUserLocation ( ) ;
63
- setMapCenter ( location . origin ) ;
64
- setZoomLevel ( location . radius ) ;
65
- setLocationError ( null ) ;
66
- logger . info ( 'User location obtained successfully on button click:' , {
67
- location,
68
- } ) ;
69
- } catch ( error ) {
70
- logger . error ( 'Error getting location on button click.' , { error } ) ;
71
- setLocationError (
72
- t ( 'HOME.LOCATION_SERVICES.ENABLE_LOCATION_SERVICES_MESSAGE' ) ,
73
- ) ;
88
+ if ( dbUserSettings ) {
89
+ const loc = await userLocation ( dbUserSettings ) ;
90
+ if ( loc ) {
91
+ setSearchCenter ( { lat : loc [ 0 ] , lng : loc [ 1 ] } ) ;
92
+ logger . info ( 'User location obtained successfully on button click:' , { location } ) ;
93
+ }
94
+ else {
95
+ setSearchCenter ( null )
96
+ }
74
97
}
98
+ // try {
99
+ // setReload(true);
100
+ // setLocationError(null);
101
+ // logger.info('User location obtained successfully on button click:', { location });
102
+ // } catch (error) {
103
+ // setReload(false)
104
+ // logger.error('Error getting location on button click.', { error });
105
+ // setLocationError(t('HOME.LOCATION_SERVICES.ENABLE_LOCATION_SERVICES_MESSAGE'));
106
+ // }
107
+ // finally{
108
+ // setReload(false);
109
+ // }
75
110
} ;
76
111
77
- // handle search query update from SearchBar and associated results
112
+ // Handle search query update from SearchBar and associated results
78
113
const handleSearch = async ( query : string ) => {
79
114
setSearchQuery ( query ) ;
80
115
setSearchClicked ( true ) ;
@@ -95,15 +130,15 @@ export default function Index() {
95
130
return (
96
131
< >
97
132
< DynamicMap
98
- center = { [ mapCenter . lat , mapCenter . lng ] }
133
+ center = { searchCenter }
99
134
zoom = { zoomLevel }
100
135
mapRef = { mapRef }
101
136
searchQuery = { searchQuery }
102
137
isSearchClicked = { isSearchClicked }
103
138
searchResults = { searchResults || [ ] }
104
139
/>
105
140
< SearchBar page = { 'default' } onSearch = { handleSearch } />
106
- < div className = "absolute bottom-8 z-10 right-0 left-0 m-auto pointer-events-none" >
141
+ < div className = "absolute bottom-8 z-10 right-0 left-0 m-auto pointer-events-none" >
107
142
< div className = "w-[90%] lg:w-full lg:px-6 mx-auto flex items-center justify-between" >
108
143
{ /* Add Seller Button */ }
109
144
< div className = "pointer-events-auto" >
0 commit comments