Merge pull request #177 from kjannette/visitors

Visitors
This commit is contained in:
S Jannette
2024-03-20 02:44:16 -05:00
committed by GitHub

View File

@@ -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 Button from "../../pageElements/Button";
import { v4 as uuidv4 } from "uuid";
import arrow from "../../Assets/Images/Arrow.png"; import arrow from "../../Assets/Images/Arrow.png";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import "../../styles/homepage.scss"; import "../../styles/homepage.scss";
@@ -6,6 +10,38 @@ import "../../styles/homepage.scss";
const HomePage = () => { const HomePage = () => {
const navigate = useNavigate(); const navigate = useNavigate();
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 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 = () => { const handleNavigate = () => {
navigate("/signup"); navigate("/signup");
}; };