@@ -11,7 +11,8 @@ import { useContext, useEffect, useState, useRef } from 'react';
11
11
import { Button } from '@/components/shared/Forms/Buttons/Buttons' ;
12
12
import SearchBar from '@/components/shared/SearchBar/SearchBar' ;
13
13
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' ;
15
16
16
17
import { AppContext } from '../../../context/AppContextProvider' ;
17
18
import logger from '../../../logger.config.mjs' ;
@@ -27,35 +28,66 @@ export default function Index() {
27
28
lat : 0 ,
28
29
lng : 0 ,
29
30
} ) ;
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 ) ;
30
37
const [ zoomLevel , setZoomLevel ] = useState ( 2 ) ;
31
38
const [ locationError , setLocationError ] = useState < string | null > ( null ) ;
32
39
const [ searchQuery , setSearchQuery ] = useState < string > ( '' ) ;
33
40
const [ isSearchClicked , setSearchClicked ] = useState ( false ) ;
34
41
const [ searchResults , setSearchResults ] = useState < any [ ] > ( [ ] ) ;
35
42
36
- const { isSigningInUser } = useContext ( AppContext ) ;
43
+ const { isSigningInUser, currentUser , autoLoginUser } = useContext ( AppContext ) ;
37
44
38
45
// Default map center (example: New York City)
39
46
const defaultMapCenter = { lat : 20 , lng : - 74.006 } ;
40
47
41
48
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 ( ) => {
43
55
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
+ }
50
66
} 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 } ) ;
54
68
}
55
69
} ;
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
+ // };
56
88
57
- fetchLocationOnLoad ( ) ;
58
- } , [ isSigningInUser ] ) ;
89
+ // fetchLocationOnLoad();
90
+ // }, [isSigningInUser]);
59
91
60
92
const handleLocationButtonClick = async ( ) => {
61
93
try {
@@ -95,7 +127,7 @@ export default function Index() {
95
127
return (
96
128
< >
97
129
< DynamicMap
98
- center = { [ mapCenter . lat , mapCenter . lng ] }
130
+ center = { searchCenter }
99
131
zoom = { zoomLevel }
100
132
mapRef = { mapRef }
101
133
searchQuery = { searchQuery }
0 commit comments