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