-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: movido a componentes reutilizables y a ts
- Loading branch information
Roberto Milla Martinez
committed
Nov 6, 2024
1 parent
0a87c8c
commit 7436b89
Showing
3 changed files
with
105 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,11 @@ | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
|
||
// @ts-expect-error | ||
import GeoLocationMap from '@/components/map/geolocationMap'; | ||
import AddressMap, { AddressAndTown } from '@/components/AddressMap'; | ||
|
||
export default function TryingPage() { | ||
const [address, setAddress] = useState(''); | ||
const [town, setTown] = useState(''); | ||
|
||
const onNewPosition = async ([lon, lat]: [string, string]) => { | ||
if (address !== '') { | ||
return; | ||
} | ||
|
||
const response = await fetch('/api/address', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
longitude: lon, | ||
latitude: lat, | ||
}), | ||
}).then((res) => res.json()); | ||
|
||
setAddress(response.address); | ||
setTown(response.town); | ||
const onNewAddress = (addressAndTown: AddressAndTown) => { | ||
console.log(addressAndTown); | ||
}; | ||
|
||
return ( | ||
<> | ||
<GeoLocationMap onNewPositionCallback={onNewPosition} /> | ||
<div className="bg-white rounded-lg p-6 w-full relative flex flex-col gap-6 mt-7"> | ||
<div className="space-y-6 max-h-[65vh] overflow-y-auto p-2"> | ||
{/* Town */} | ||
<div className="grid gap-4"> | ||
<div> | ||
<label className="block text-sm font-medium text-gray-700 mb-1">Town</label> | ||
<input | ||
type="text" | ||
value={town} | ||
onChange={(e) => setTown(e.target.value)} | ||
className="w-full p-2 border rounded focus:ring-2 focus:ring-green-500 focus:border-green-500" | ||
/> | ||
</div> | ||
</div> | ||
{/* Address */} | ||
<div className="grid gap-4"> | ||
<div> | ||
<label className="block text-sm font-medium text-gray-700 mb-1">Address</label> | ||
<input | ||
type="text" | ||
value={address} | ||
onChange={(e) => setAddress(e.target.value)} | ||
className="w-full p-2 border rounded focus:ring-2 focus:ring-green-500 focus:border-green-500" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
return <AddressMap onNewAddressCallback={onNewAddress} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use client'; | ||
|
||
import GeoLocationMap, { LngLat } from '@/components/map/GeolocationMap'; | ||
import { useState } from 'react'; | ||
|
||
export type AddressAndTown = { address: string; town: string }; | ||
export type AddressAndTownCallback = (addressAndTown: AddressAndTown) => void; | ||
export type AddressMapProps = { | ||
onNewAddressCallback: AddressAndTownCallback; | ||
}; | ||
|
||
export default function AddressMap({ onNewAddressCallback }: AddressMapProps) { | ||
const [address, setAddress] = useState(''); | ||
const [town, setTown] = useState(''); | ||
|
||
const onNewPosition = async (lngLat: LngLat) => { | ||
if (address !== '') { | ||
return; | ||
} | ||
|
||
const response = await fetch('/api/address', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
longitude: lngLat.lng, | ||
latitude: lngLat.lat, | ||
}), | ||
}).then((res) => res.json()); | ||
|
||
setAddress(response.address); | ||
setTown(response.town); | ||
if (typeof onNewAddressCallback === 'function') { | ||
onNewAddressCallback({ address: response.address, town: response.town }); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<GeoLocationMap onNewPositionCallback={onNewPosition} /> | ||
<div className="bg-white rounded-lg p-6 w-full relative flex flex-col gap-6 mt-7"> | ||
<div className="space-y-6 max-h-[65vh] overflow-y-auto p-2"> | ||
{/* Town */} | ||
<div className="grid gap-4"> | ||
<div> | ||
<label className="block text-sm font-medium text-gray-700 mb-1">Town</label> | ||
<input | ||
disabled | ||
type="text" | ||
value={town} | ||
onChange={(e) => setTown(e.target.value)} | ||
className="w-full p-2 border rounded focus:ring-2 focus:ring-green-500 focus:border-green-500" | ||
/> | ||
</div> | ||
</div> | ||
{/* Address */} | ||
<div className="grid gap-4"> | ||
<div> | ||
<label className="block text-sm font-medium text-gray-700 mb-1">Address</label> | ||
<input | ||
disabled | ||
type="text" | ||
value={address} | ||
onChange={(e) => setAddress(e.target.value)} | ||
className="w-full p-2 border rounded focus:ring-2 focus:ring-green-500 focus:border-green-500" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters