@@ -46,31 +46,46 @@ export default function SignupForm() {
4646
4747 const { getCurrentLocation, fetchLocationName, isLoading, getLocationNameFromLatLng } = useLocation ( ) ;
4848
49+ function setLatLng ( data ?: { lat ?: any ; lng ?: any } ) {
50+ if ( ! data ) return ;
51+
52+ const latNum = Number ( data . lat ) ;
53+ const lngNum = Number ( data . lng ) ;
54+
55+ if ( ! isNaN ( latNum ) ) setValue ( "lat" , latNum ) ;
56+ if ( ! isNaN ( lngNum ) ) setValue ( "lng" , lngNum ) ;
57+ }
58+
4959 const handleGetLocation = async ( ) => {
5060 try {
51- const { lat , lng } = await getCurrentLocation ( ) ;
61+ const data = await getCurrentLocation ( ) ;
5262 const location = await fetchLocationName ( lat , lng ) ;
53- console . log ( " lat, lng " , lat , lng )
54- setValue ( "lat" , lat ) ;
55- setValue ( "lng" , lng ) ;
63+ setLatLng ( data ) ;
5664 setValue ( "location" , location || "Location found but could not get name" ) ;
5765 } catch ( err : any ) {
5866 setValue ( "location" , err . message || "Failed to get location" ) ;
5967 }
6068 } ;
6169
6270 const { location, lat, lng} = watch ( ) ;
63-
71+ const handleLocationChange = ( location : string ) => {
72+ if ( location . length > 3 && ! lng ) {
73+ try {
74+ setTimeout ( async ( ) => {
75+ const data = await getLocationNameFromLatLng ( location ) ;
76+ setLatLng ( data ) ;
77+ } , 500 )
78+ } catch ( error ) {
79+ console . error ( "Geocoding failed:" , error ) ;
80+ }
81+ }
82+ } ;
6483 useEffect ( ( ) => {
65- async function getAddress ( ) {
66- if ( location && ! lat && ! lng ) {
67- const data = await getLocationNameFromLatLng ( location )
68- setValue ( "lat" , data ?. lat ) ;
69- setValue ( "lng" , data ?. lng ) ;
84+ if ( location ) {
85+ console . log ( "locations" )
86+ handleLocationChange ( location ) ;
7087 }
71- }
72- setTimeout ( ( ) => getAddress ( ) , 1000 ) ;
73- } , [ location ] )
88+ } , [ location ] ) ;
7489
7590 const { mutate, isPending, isSuccess, isError } = useMutation ( {
7691 mutationFn : signupUser ,
@@ -86,10 +101,14 @@ export default function SignupForm() {
86101 } ,
87102 } ) ;
88103
89- const onSubmit = ( data : SignUpFormData ) => {
90- console . log ( data , "data signup" )
91- mutate ( data ) ;
92- } ;
104+ const onSubmit = async ( data : SignUpFormData ) => {
105+ try {
106+ mutate ( data ) ;
107+ } catch ( error :any ) {
108+ console . error ( "Form submission error:" , error ) ;
109+ setErrorMsg ( error . message || "Failed to submit form" ) ;
110+ }
111+ } ;
93112
94113 return (
95114 < >
0 commit comments