Skip to content

Commit

Permalink
Merge pull request #388 from Sawan-Kushwah/feat/totalVisitorCount
Browse files Browse the repository at this point in the history
Implemented CURD  backend Api for total visitor count #388
  • Loading branch information
Anuj3553 authored Nov 6, 2024
2 parents b5d7c15 + d72a90a commit ee72ad4
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 15 deletions.
26 changes: 11 additions & 15 deletions client/src/component/Footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down Expand Up @@ -98,7 +99,7 @@ const Footer = (props) => {
<div className="Footer" style={{ backgroundColor: props.mode === 'dark' ? '#0B192C' : 'white' }}>
<div className="container">
<div className='mb-4' data-aos="fade-up" data-aos-duration='1500'>
<h4 style={{color : props.mode ==='dark'? 'white' : 'black'}} className="text-3xl font-semibold text-center mb-4">Subscribe to our Newsletter</h4>
<h4 style={{ color: props.mode === 'dark' ? 'white' : 'black' }} className="text-3xl font-semibold text-center mb-4">Subscribe to our Newsletter</h4>
<form
className="flex flex-col items-center gap-4 md:flex-row md:justify-center"
onSubmit={handleSubscribe}
Expand All @@ -109,8 +110,8 @@ const Footer = (props) => {
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"

/>
<button
type="submit"
Expand All @@ -125,7 +126,7 @@ const Footer = (props) => {
</p>
)}
</div>
<div className="flex flex-col items-center space-y-4 mb-4" data-aos='fade-up' data-aos-duration='1500'>
<div className="flex justify-center items-center mb-4" data-aos='fade-up' data-aos-duration='1500'>
<a
href="https://github.com/Anuj3553/Bitbox"
target="_blank"
Expand All @@ -143,17 +144,12 @@ const Footer = (props) => {
<span>Star us ⭐ {stars !== null ? stars : '...'}</span>
</a>

<div className="flex flex-col items-center text-gray-800">
<div className="flex items-center space-x-2">
<span className="font-bold text-2xl">Total Visitors : </span>
<a href="https://github.com/Anuj3553/Bitbox" target="_blank" rel="noopener noreferrer">
<img
src="https://hitwebcounter.com/counter/counter.php?page=17062113&style=0006&nbdigits=5&type=ip&initCount=1000"
alt="Visit counter for websites"
className="inline-block"
/>
</a>
</div>
<div className="flex flex-col items-center text-gray-800 ml-4">

<a href="https://github.com/Anuj3553/Bitbox" target="_blank" rel="noopener noreferrer">
<VisitorCounter />
</a>

</div>
</div>

Expand Down
63 changes: 63 additions & 0 deletions client/src/component/Footers/VisitorCount.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
{visitorCount !== null ? (
<p className="font-bold text-2xl text-gray-300">Total Visitors: {visitorCount}</p>
) : (
<p>Loading visitor count...</p>
)}
</div>
);
};

export default VisitorCounter;
29 changes: 29 additions & 0 deletions server/Controllers/visitorController.js
Original file line number Diff line number Diff line change
@@ -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" });
}
};
11 changes: 11 additions & 0 deletions server/Models/Visitor.js
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
11 changes: 11 additions & 0 deletions server/routes/visitor.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit ee72ad4

Please sign in to comment.