Skip to content

Commit 389b55b

Browse files
committed
pagination done
1 parent b075284 commit 389b55b

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

client/src/components/Home.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Skeleton from "./Skeletons/Skeleton";
1111
import HomeSkeleton from "./Skeletons/HomeSkeleton";
1212
import PromotedCard from "./PromotedCard";
1313
import { options } from "../utils/index"
14+
import Pagination from "./Pagination/Pagination";
1415

1516
export default function Home() {
1617
const [showStats, setShowStats] = useState(false);
@@ -83,9 +84,12 @@ export default function Home() {
8384
}
8485
};
8586

86-
const [selectedValue, setSelectedValue] = useState('Only Value');
87+
const [pageNo, setPageNo] = useState(1)
88+
const totalPages = Math.ceil(filteredData.length / 11)
89+
8790

8891

92+
const [selectedValue, setSelectedValue] = useState('Only Value');
8993
const [random, setRandom] = useState(Math.floor(Math.random() * options.length));
9094

9195

@@ -130,7 +134,7 @@ export default function Home() {
130134
filteredData.length === 0 ? (
131135
<p className="grid place-content-center ">No user found</p>
132136
) : (
133-
filteredData.map((data, index) => (
137+
filteredData.slice((pageNo * 11) - 11, pageNo * 11).map((data, index) => (
134138
<motion.div
135139
key={data.username}
136140
initial={{ opacity: 0, x: -50 }}
@@ -157,6 +161,7 @@ export default function Home() {
157161
</div>
158162

159163
)}
164+
<Pagination pageNo={pageNo} setPageNo={setPageNo} totalPages={totalPages} />
160165
</>
161166
)
162167
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
const items = [
3+
{ id: 1, title: 'Back End Developer', department: 'Engineering', type: 'Full-time', location: 'Remote' },
4+
{ id: 2, title: 'Front End Developer', department: 'Engineering', type: 'Full-time', location: 'Remote' },
5+
{ id: 3, title: 'User Interface Designer', department: 'Design', type: 'Full-time', location: 'Remote' },
6+
]
7+
8+
export default function Pagination({ pageNo, setPageNo, totalPages }) {
9+
return (
10+
<div className="flex items-center justify-between border-t border-gray-200 bg-transparent px-4 py-3 sm:px-6">
11+
12+
{/* For mobile devices */}
13+
<div className="flex flex-1 justify-between sm:hidden">
14+
<button
15+
href="#"
16+
className="relative inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
17+
>
18+
Previous
19+
</button>
20+
<button
21+
href="#"
22+
className="relative ml-3 inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
23+
>
24+
Next
25+
</button>
26+
</div>
27+
28+
{/* For larger devices */}
29+
<div className="hidden sm:flex sm:flex-1 sm:items-center sm:justify-center">
30+
<div>
31+
<nav className="isolate inline-flex -space-x-px rounded-md shadow-sm" aria-label="Pagination">
32+
<button onClick={() => {
33+
if (pageNo < 2) return;
34+
setPageNo((no) => no - 1)
35+
}} className="relative inline-flex items-center rounded-l-md px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-800 focus:z-20 focus:outline-offset-0">
36+
<span className="sr-only">Previous</span>
37+
◀️
38+
</button>
39+
40+
{
41+
Array.from({ length: totalPages }, (_, index) => index + 1).map((item) => {
42+
console.log(item)
43+
return (
44+
<button
45+
key={item}
46+
onClick={() => setPageNo(item)}
47+
aria-current={pageNo === item ? 'page' : undefined}
48+
className={`ring-1 ring-inset ring-gray-300 relative z-10 inline-flex items-center px-4 py-2 text-sm font-semibold hover:bg-gray-800 text-white focus:z-20 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 ${pageNo === item ? 'bg-gray-500 hover:bg-gray-500 ' : 'bg-transparent'}`}
49+
>
50+
{item}
51+
</button>
52+
)
53+
})
54+
}
55+
56+
<button onClick={() => {
57+
if (pageNo > totalPages - 1) return;
58+
setPageNo((no) => no + 1)
59+
}} className="ring-1 ring-inset ring-gray-300 relative inline-flex items-center rounded-r-md px-2 py-2 text-gray-400 hover:bg-gray-800 focus:z-20 focus:outline-offset-0">
60+
<span className="sr-only">Next</span>
61+
{/* <ChevronRightIcon className="h-5 w-5" aria-hidden="true" /> */}
62+
▶️
63+
</button>
64+
</nav>
65+
</div>
66+
</div>
67+
</div>
68+
)
69+
}

0 commit comments

Comments
 (0)