diff --git a/src/Components/Home/HomePage.js b/src/Components/Home/HomePage.js index c714297..aa04319 100644 --- a/src/Components/Home/HomePage.js +++ b/src/Components/Home/HomePage.js @@ -1,4 +1,8 @@ +import { useEffect } from "react"; +import { collection, setDoc, doc } from "firebase/firestore"; +import { db } from "../../firebase"; import Button from "../../pageElements/Button"; +import { v4 as uuidv4 } from "uuid"; import arrow from "../../Assets/Images/Arrow.png"; import { useNavigate } from "react-router-dom"; import "../../styles/homepage.scss"; @@ -6,6 +10,55 @@ 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); + } + + 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 handleNavigate = () => { navigate("/signup"); };