Skip to content

Commit

Permalink
user page created
Browse files Browse the repository at this point in the history
  • Loading branch information
SH20RAJ authored Mar 18, 2024
1 parent 516d9e8 commit 0c79052
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/app/[slug]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"use client";
import { useState, useEffect } from "react";
import Link from "next/link";
import Nav from "../components/Nav";
import Footer from "../components/Footer";

export function generateRandomNumber(min, max) {
// Math.random() generates a random number between 0 and 1
// We multiply it by (max - min + 1) to include the max value,
// then add min to ensure the number falls within the desired range.
return Math.floor(Math.random() * (max - min + 1)) + min;
}

export default function ArticlesPage({params}) {
const [articles, setArticles] = useState([]);
const [page, setPage] = useState(1);
const [isLoading, setIsLoading] = useState(false);
const [filteredArticles, setFilteredArticles] = useState([]);
console.log(params.slug)
useEffect(() => {
async function fetchArticles() {
setIsLoading(true);
try {
const response = await fetch(
`https://dev.to/api/articles?username=${params.slug}&per_page=100&page=${page}`
);
const data = await response.json();
setArticles((prevArticles) => [...prevArticles, ...data]);
setIsLoading(false);
} catch (error) {
console.error("Error fetching articles:", error);
setIsLoading(false);
}
}

fetchArticles();
}, [page]);

useEffect(() => {
setFilteredArticles(articles);
}, [articles]);

useEffect(() => {
const handleScroll = () => {
const scrollHeight = document.documentElement.scrollHeight;
const scrollTop = document.documentElement.scrollTop;
const clientHeight = document.documentElement.clientHeight;

if (scrollTop + clientHeight >= scrollHeight) {
setPage((prevPage) => prevPage + 1);
}
};

window.addEventListener("scroll", handleScroll);

return () => window.removeEventListener("scroll", handleScroll);
}, []);

function handleSearch(event) {
const query = event.target.value;

const filtered = articles.filter((article) =>
article.title.toLowerCase().includes(query.toLowerCase())
);

setFilteredArticles(filtered);
}

return (
<>
<Nav />

<main className="postscontainer">
<h2>Latest Posts</h2>
<div className="container">
<div className="latestposts">
{filteredArticles.map((article) => (
<ArticleCard key={article.id} article={article} />
))}
</div>
</div>
</main>
{isLoading && (
<div className="text-center mt-8">
<p>Loading...</p>
</div>
)}
<Footer />
</>
);
}

// Child component to display article card
function ArticleCard({ article }) {
return (
<div className="card">
<Link href={`./${article.path}`}>
<img src={article.social_image || "https://samples-files.com/samples/Images/jpg/1920-1080-sample.jpg"} alt="Image" />
<div className="card-title">{article.title}</div>
<div className="line"></div>
<p className="text-gray-500 mb-4">{article.tag_list.join(", ")}</p>
</Link>{" "}
</div>
);
}

0 comments on commit 0c79052

Please sign in to comment.