diff --git a/client/src/component/Footer.jsx b/client/src/component/Footer.jsx index b504d3c..f5987e2 100644 --- a/client/src/component/Footer.jsx +++ b/client/src/component/Footer.jsx @@ -4,6 +4,7 @@ import { Link } from 'react-router-dom'; import { FaXTwitter } from "react-icons/fa6"; import { FaGithub, FaLinkedin, FaYoutube } from 'react-icons/fa'; import { useEffect, useState } from 'react'; +import VisitorCounter from './Footers/VisitorCount'; const Footer = (props) => { const [email, setEmail] = useState(""); @@ -98,7 +99,7 @@ const Footer = (props) => {
-

Subscribe to our Newsletter

+

Subscribe to our Newsletter

{ value={email} onChange={(e) => setEmail(e.target.value)} required - className="px-4 py-2 border border-gray-300 rounded-md w-full max-w-xs focus:outline-none" - + className="px-4 py-2 border border-gray-300 rounded-md w-full max-w-xs focus:outline-none" + />
-
+
{ Star us ⭐ {stars !== null ? stars : '...'} -
-
- Total Visitors : - - Visit counter for websites - -
+
+ + + + +
diff --git a/client/src/component/Footers/VisitorCount.jsx b/client/src/component/Footers/VisitorCount.jsx new file mode 100644 index 0000000..0b90516 --- /dev/null +++ b/client/src/component/Footers/VisitorCount.jsx @@ -0,0 +1,63 @@ +import { useEffect, useState } from "react"; + +// Function to increment the visitor count +async function incrementVisitorCount() { + try { + const response = await fetch("http://localhost:5000/api/visitor/increment", { + method: "POST", + }); + const data = await response.json(); + console.log("Visitor count incremented:", data.count); + return data.count; + } catch (error) { + console.error("Error incrementing visitor count:", error); + return null; + } +} + +// Function to get the current visitor count +async function getVisitorCount() { + try { + const response = await fetch("http://localhost:5000/api/visitor/count", { + method: "GET", + }); + const data = await response.json(); + return data.count; + } catch (error) { + console.error("Error retrieving visitor count:", error); + return null; + } +} + +const VisitorCounter = () => { + const [visitorCount, setVisitorCount] = useState(null); + + useEffect(() => { + // Increment and retrieve visitor count on component mount + const fetchAndIncrementVisitorCount = async () => { + // First, increment the visitor count + const newCount = await incrementVisitorCount(); + if (newCount !== null) { + setVisitorCount(newCount); + } else { + // If increment fails, fetch the latest count + const currentCount = await getVisitorCount(); + setVisitorCount(currentCount); + } + }; + + fetchAndIncrementVisitorCount(); + }, []); + + return ( +
+ {visitorCount !== null ? ( +

Total Visitors: {visitorCount}

+ ) : ( +

Loading visitor count...

+ )} +
+ ); +}; + +export default VisitorCounter; diff --git a/server/Controllers/visitorController.js b/server/Controllers/visitorController.js new file mode 100644 index 0000000..404afb1 --- /dev/null +++ b/server/Controllers/visitorController.js @@ -0,0 +1,29 @@ +const VisitorCount = require("../Models/Visitor.js"); + +// Increment visitor count +exports.incrementVisitorCount = async (req, res) => { + try { + let visitorCount = await VisitorCount.findOne({}); + if (!visitorCount) { + visitorCount = new VisitorCount({ count: 1 }); + } else { + visitorCount.count += 1; + } + await visitorCount.save(); + res.json({ count: visitorCount.count }); + } catch (error) { + console.error("Error incrementing visitor count:", error); + res.status(500).json({ error: "Error incrementing visitor count" }); + } +}; + +// Get current visitor count +exports.getVisitorCount = async (req, res) => { + try { + const visitorCount = await VisitorCount.findOne({}); + res.json({ count: visitorCount ? visitorCount.count : 0 }); + } catch (error) { + console.error("Error retrieving visitor count:", error); + res.status(500).json({ error: "Error retrieving visitor count" }); + } +}; diff --git a/server/Models/Visitor.js b/server/Models/Visitor.js new file mode 100644 index 0000000..2fb8e7a --- /dev/null +++ b/server/Models/Visitor.js @@ -0,0 +1,11 @@ +// models/VisitorCount.js +const mongoose = require("mongoose"); + +const visitorCountSchema = new mongoose.Schema({ + count: { + type: Number, + default: 0, + }, +}); + +module.exports = mongoose.model("VisitorCount", visitorCountSchema); diff --git a/server/index.js b/server/index.js index 45a3872..4fbf7f8 100644 --- a/server/index.js +++ b/server/index.js @@ -47,6 +47,7 @@ app.use("/api/profile", require("./routes/profile")); app.use("/api/feedback", require("./routes/feedback")); app.use("/api/contact", require("./routes/contact")); app.use("/api/blog", require("./routes/blog")); +app.use("/api/visitor", require("./routes/visitor")); // Socket.io connection handling const users = {}; diff --git a/server/routes/visitor.js b/server/routes/visitor.js new file mode 100644 index 0000000..48963f6 --- /dev/null +++ b/server/routes/visitor.js @@ -0,0 +1,11 @@ +const express = require("express"); +const router = express.Router(); +const visitorController = require("../Controllers/visitorController"); + +// Route to get current visitor count +router.get("/count", visitorController.getVisitorCount); + +// Route to increment visitor count +router.post("/increment", visitorController.incrementVisitorCount); + +module.exports = router;