1+ import { useLoadScript } from '@react-google-maps/api' ;
12import { GetServerSidePropsContext } from 'next' ;
23import { useRouter } from 'next/router' ;
34import React , { useState } from 'react' ;
5+ import PlacesAutocomplete , {
6+ geocodeByAddress ,
7+ getLatLng ,
8+ } from 'react-places-autocomplete' ;
49import Layout from '../components/Layout' ;
510import { User } from '../util/database' ;
611import { Errors } from '../util/types' ;
@@ -15,14 +20,40 @@ const EditUser = (props: Props) => {
1520 const [ newMail , setNewMail ] = useState ( '' ) ;
1621 const [ newAddress , setNewAddress ] = useState ( '' ) ;
1722 const [ errors ] = useState < Errors > ( [ ] ) ;
23+ const [ newLat , setNewLat ] = useState ( 0 ) ;
24+ const [ newLng , setNewLng ] = useState ( 0 ) ;
1825
1926 const router = useRouter ( ) ;
2027
28+ const libraries = [ 'places' ] ;
29+
30+ const { isLoaded, loadError } = useLoadScript ( {
31+ googleMapsApiKey : 'AIzaSyDSSIEFPSWv8mx85eU7wqywyKB97k0Lsno' ,
32+ libraries,
33+ } ) ;
34+
35+ if ( loadError ) return 'Error loading maps' ;
36+ if ( ! isLoaded ) return 'Loading Maps' ;
37+
38+ const handleChange = ( value : string ) => {
39+ setNewAddress ( value ) ;
40+ } ;
41+
42+ const handleSelect = async ( value : string ) => {
43+ const results = await geocodeByAddress ( value ) ;
44+ const latLng = await getLatLng ( results [ 0 ] ) ;
45+ setNewAddress ( value ) ;
46+ setNewLat ( latLng . lat ) ;
47+ setNewLng ( latLng . lng ) ;
48+ } ;
49+
2150 const updateUser = async (
2251 id : number ,
2352 name : string ,
2453 mail : string ,
2554 address : string ,
55+ lat : number ,
56+ lng : number ,
2657 ) => {
2758 await fetch ( `/api/users/${ id } ` , {
2859 method : 'PATCH' ,
@@ -31,6 +62,8 @@ const EditUser = (props: Props) => {
3162 name : name ,
3263 mail : mail ,
3364 address : address ,
65+ lat : lat ,
66+ lng : lng ,
3467 } ) ,
3568 } ) ;
3669 } ;
@@ -77,19 +110,46 @@ const EditUser = (props: Props) => {
77110 </ div >
78111 </ div >
79112 < div className = "mb-10" >
80- < label
81- htmlFor = "address"
82- className = "block text-dark text-normal font-bold mb-2"
83- >
113+ < div className = "block text-dark text-normal font-bold mb-2" >
84114 Address
85- </ label >
86- < input
87- className = "shadow appearance-none border rounded w-full py-2 px-3 text-dark leading-tight focus:outline-none focus:shadow-outline"
88- id = "itemName"
89- placeholder = "Name"
90- required
91- onChange = { ( e ) => setNewAddress ( e . currentTarget . value ) }
92- />
115+ < div >
116+ < PlacesAutocomplete
117+ value = { newAddress }
118+ onChange = { handleChange }
119+ onSelect = { handleSelect }
120+ >
121+ { ( {
122+ getInputProps,
123+ suggestions,
124+ getSuggestionItemProps,
125+ loading,
126+ } ) => (
127+ < div >
128+ < input
129+ className = "shadow appearance-none border rounded w-full py-2 px-3 text-dark leading-tight focus:outline-none focus:shadow-outline"
130+ { ...getInputProps ( { placeholder : 'Address' } ) }
131+ />
132+ < div >
133+ { loading && < div > Loading...</ div > }
134+ { suggestions . map ( ( suggestion ) => {
135+ const style = suggestion . active
136+ ? { backgroundColor : '#BBE1FA' , cursor : 'pointer' }
137+ : { backgroundColor : '#fff' , cursor : 'pointer' } ;
138+ return (
139+ < div
140+ key = { `li-suggestion-${ suggestion . placeId } ` }
141+ { ...getSuggestionItemProps ( suggestion , { style } ) }
142+ >
143+ { suggestion . description }
144+ </ div >
145+ ) ;
146+ } ) }
147+ </ div >
148+ </ div >
149+ ) }
150+ </ PlacesAutocomplete >
151+ </ div >
152+ </ div >
93153 < div >
94154 Address now:{ ' ' }
95155 < span className = "font-bold" > { props . user . address } </ span >
@@ -106,7 +166,14 @@ const EditUser = (props: Props) => {
106166 { newName . length && newMail . length && newAddress . length > 0 ? (
107167 < button
108168 onClick = { ( ) =>
109- updateUser ( props . user . id , newName , newMail , newAddress )
169+ updateUser (
170+ props . user . id ,
171+ newName ,
172+ newMail ,
173+ newAddress ,
174+ newLat ,
175+ newLng ,
176+ )
110177 }
111178 className = "w-full bg-blue shadow-lg text-bright text-xl font-bold py-2 px-10 rounded hover:bg-blue-light hover:text-dark"
112179 >
0 commit comments