diff --git a/src/Components/Home/HomePage.js b/src/Components/Home/HomePage.js index aa04319..4d31d1d 100644 --- a/src/Components/Home/HomePage.js +++ b/src/Components/Home/HomePage.js @@ -10,53 +10,36 @@ import "../../styles/homepage.scss"; const HomePage = () => { const navigate = useNavigate(); - async function saveVisit(visitor, visitId) { - console.log("visitor, visitId", visitor, visitId); - const latitude = `${visitor.latitude}`; - const collecRef = collection(db, "homevisitors"); - await setDoc(doc(collecRef, `${visitId}`), visitor, latitude); + async function saveData(data) { + console.log("save data", data); + const id = uuidv4(); + const visitRef = collection(db, "homevisitors"); + await setDoc(doc(visitRef, `${id}`), data); + } + + function getPosition() { + return new Promise((res, rej) => { + navigator.geolocation.getCurrentPosition(res, rej); + }); } useEffect(() => { - /* if (window.location.href === "http://localhost:3000/") { return; } - */ - const visitor = {}; - const options = { - enableHighAccuracy: true, - timeout: 5000, - maximumAge: 0, - }; - - function success(position) { - const latitude = position.coords.latitude; - const longitude = position.coords.longitude; - const accuracy = position.coords.accuracy; - visitor["latitude"] = `{latitude}`; - visitor["longitude"] = longitude; - visitor["positionAccuracy"] = `~${accuracy} meters`; - } - - function error() { - const error = "unable to ret locaction"; - return error; - } - - navigator?.geolocation?.getCurrentPosition(success, error, options); - - visitor["userAgent"] = window?.navigator.userAgent; - const now = new Date(); - visitor["dateTime"] = now; - const visitId = uuidv4(); - - try { - saveVisit(visitor, visitId); - } catch (error) { - console.log(`Error saving new user to db: ${error}`); - } + const userAgent = window?.navigator.userAgent; + getPosition().then((data) => { + const dateTime = new Date(); + const currentLat = data.coords.latitude; + const currentLong = data.coords.longitude; + const values = {}; + values["userAgent"] = userAgent; + values["latitude"] = currentLat; + values["longitude"] = currentLong; + values["dateTime"] = String(dateTime); + saveData(values); + }); }, []); const handleNavigate = () => {