-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from atlp-rwanda/ft-browser-location
Ft browser location
- Loading branch information
Showing
7 changed files
with
307 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,96 @@ | ||
|
||
import React, { useState, useEffect } from "react"; | ||
import { HiMapPin } from "react-icons/hi2"; | ||
import { toast } from "react-toastify"; | ||
|
||
|
||
function LocationButton() { | ||
const [location, setLocation] = useState<{ | ||
latitude: number; | ||
longitude: number; | ||
} | null>(null); | ||
const address = typeof window !== "undefined" && localStorage?.getItem("delivery_address"); | ||
const [currentAddress, setCurrentAddress] = useState(address) || ""; | ||
|
||
const handleClick = () => { | ||
if ("geolocation" in navigator) { | ||
// console.log("i am here") | ||
navigator.permissions | ||
.query({ name: "geolocation" }) | ||
.then((result) => { | ||
if (result.state === "granted" || result.state === "prompt") { | ||
navigator.geolocation.getCurrentPosition( | ||
(position) => { | ||
const { latitude, longitude } = position.coords; | ||
setLocation({ latitude, longitude }); | ||
}, | ||
(error) => { | ||
toast.error(error.message); | ||
} | ||
); | ||
} else if (result.state === "denied") { | ||
toast.error("Allow the location access"); | ||
} | ||
}) | ||
.catch((error: any) => { | ||
console.error("Error checking the location permission:", error); | ||
}); | ||
} else { | ||
toast.error("Sorry! geolocation is not supported by this browser"); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (location) { | ||
const endpoint = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${location.latitude},${location.longitude}&result_type=street_address&key=${process.env.REACT_APP_GOOGLE_MAPS_API_KEY}`; | ||
fetch(endpoint) | ||
.then((response) => { | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
return response.json(); | ||
}) | ||
.then((data) => { | ||
console.log(data); | ||
if (data.results && data.results.length > 0) { | ||
const { formatted_address, address_components } = data.results[0]; | ||
const city = address_components.find((component: any) => | ||
component.types.includes("locality") | ||
)?.long_name; | ||
const street = address_components.find((component: any) => | ||
component.types.includes("route") | ||
)?.long_name; | ||
localStorage.setItem("delivery_address", formatted_address); | ||
localStorage.setItem("delivery_address_city", city); | ||
localStorage.setItem("delivery_address_street", street); | ||
setCurrentAddress(formatted_address); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.log("Fetch error: ", error.message); | ||
}); | ||
} | ||
}, [location]); | ||
|
||
|
||
|
||
return ( | ||
<> | ||
<button | ||
onClick={handleClick} | ||
className={`flex items-center px-6 py-3 bg-slate-200 rounded-full md:rounded-lg`} | ||
> | ||
<HiMapPin className="shrink-0 text-secondary" /> | ||
<span | ||
className={ | ||
"truncate max-w-[8rem] font-outfit text-sm text-gray-300 md:max-w-full" | ||
} | ||
> | ||
{currentAddress ? currentAddress : "Choose Your Delivery Address"} | ||
</span> | ||
</button> | ||
</> | ||
); | ||
} | ||
|
||
export default LocationButton; |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,14 @@ | ||
import React from 'react' | ||
import LocationBtn from '../Lib/locationButton'; | ||
|
||
const location = () => { | ||
return ( | ||
<div className="flex items-center justify-center mt-[120px]"> | ||
<LocationBtn /> | ||
</div> | ||
) | ||
} | ||
|
||
export default location; | ||
|
||
|