|
1 | 1 | // app/page.tsx
|
2 |
| - |
3 | 2 | "use client";
|
| 3 | + |
4 | 4 | import { useEffect, useState } from 'react';
|
5 |
| -import { fetchTopStories, fetchItemById } from '../../utils/api'; |
| 5 | +import { fetchTopStories, fetchItemById } from '../../utils/api'; |
6 | 6 | import Link from 'next/link';
|
| 7 | +import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/24/solid'; |
7 | 8 |
|
8 | 9 | type Story = {
|
9 | 10 | id: number;
|
10 | 11 | title: string;
|
11 | 12 | url?: string;
|
12 | 13 | by: string;
|
| 14 | + score: number; |
| 15 | + time: number; |
13 | 16 | };
|
14 | 17 |
|
15 | 18 | export default function Home() {
|
16 | 19 | const [stories, setStories] = useState<Story[]>([]);
|
| 20 | + const [currentPage, setCurrentPage] = useState(1); |
| 21 | + const [totalPages, setTotalPages] = useState(0); |
| 22 | + const storiesPerPage = 10; // Number of stories per page |
17 | 23 |
|
18 | 24 | useEffect(() => {
|
19 | 25 | const getStories = async () => {
|
20 | 26 | const storyIds = await fetchTopStories();
|
21 |
| - const storiesData = await Promise.all(storyIds.map((id: number) => fetchItemById(id))); |
| 27 | + setTotalPages(Math.ceil(storyIds.length / storiesPerPage)); |
| 28 | + |
| 29 | + const paginatedStoryIds = storyIds.slice( |
| 30 | + (currentPage - 1) * storiesPerPage, |
| 31 | + currentPage * storiesPerPage |
| 32 | + ); |
| 33 | + |
| 34 | + const storiesData = await Promise.all(paginatedStoryIds.map((id: number) => fetchItemById(id))); |
22 | 35 | setStories(storiesData);
|
23 | 36 | };
|
24 | 37 |
|
25 | 38 | getStories();
|
26 |
| - }, []); |
| 39 | + }, [currentPage]); |
| 40 | + |
| 41 | + const handleNextPage = () => { |
| 42 | + if (currentPage < totalPages) { |
| 43 | + setCurrentPage(currentPage + 1); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + const handlePrevPage = () => { |
| 48 | + if (currentPage > 1) { |
| 49 | + setCurrentPage(currentPage - 1); |
| 50 | + } |
| 51 | + }; |
27 | 52 |
|
28 | 53 | return (
|
29 |
| - <div className="container mx-auto p-4"> |
30 |
| - <h1 className="text-3xl font-bold">Hacker News - Top Stories</h1> |
31 |
| - <ul className="mt-4"> |
| 54 | + <div className="container mx-auto p-6"> |
| 55 | + <h1 className="text-4xl font-bold text-center mb-6">Hacker News - Top Stories</h1> |
| 56 | + <ul className="space-y-6"> |
32 | 57 | {stories.map((story) => (
|
33 |
| - <li key={story.id} className="mb-4"> |
34 |
| - <Link href={`/story/${story.id}`} className="text-blue-600 hover:underline"> |
35 |
| - {story.title} |
36 |
| - </Link> |
37 |
| - <p className="text-sm text-gray-600">by {story.by}</p> |
| 58 | + <li key={story.id} className="bg-white shadow-md p-4 rounded-lg"> |
| 59 | + <div className="flex items-start justify-between"> |
| 60 | + <div> |
| 61 | + <Link href={`/story/${story.id}`} className="text-lg font-semibold text-blue-600 hover:underline"> |
| 62 | + {story.title} |
| 63 | + </Link> |
| 64 | + <p className="text-sm text-gray-600">by {story.by}</p> |
| 65 | + </div> |
| 66 | + <p className="text-gray-500 text-sm"> |
| 67 | + {story.score} points | {new Date(story.time * 1000).toLocaleDateString()} |
| 68 | + </p> |
| 69 | + </div> |
38 | 70 | </li>
|
39 | 71 | ))}
|
40 | 72 | </ul>
|
| 73 | + |
| 74 | + {/* Pagination Controls */} |
| 75 | + <div className="flex justify-center items-center mt-6"> |
| 76 | + <button |
| 77 | + className={`mr-2 p-2 rounded-md bg-gray-200 ${ |
| 78 | + currentPage === 1 ? 'opacity-50 cursor-not-allowed' : 'hover:bg-gray-300' |
| 79 | + }`} |
| 80 | + onClick={handlePrevPage} |
| 81 | + disabled={currentPage === 1} |
| 82 | + > |
| 83 | + <ChevronLeftIcon className="w-5 h-5 text-gray-600" /> |
| 84 | + </button> |
| 85 | + <span className="text-lg font-semibold"> |
| 86 | + {currentPage} / {totalPages} |
| 87 | + </span> |
| 88 | + <button |
| 89 | + className={`ml-2 p-2 rounded-md bg-gray-200 ${ |
| 90 | + currentPage === totalPages ? 'opacity-50 cursor-not-allowed' : 'hover:bg-gray-300' |
| 91 | + }`} |
| 92 | + onClick={handleNextPage} |
| 93 | + disabled={currentPage === totalPages} |
| 94 | + > |
| 95 | + <ChevronRightIcon className="w-5 h-5 text-gray-600" /> |
| 96 | + </button> |
| 97 | + </div> |
41 | 98 | </div>
|
42 | 99 | );
|
43 | 100 | }
|
0 commit comments