-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathMain.jsx
More file actions
194 lines (171 loc) · 6.48 KB
/
Main.jsx
File metadata and controls
194 lines (171 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import React, { useState, useMemo, useCallback } from "react";
import classes from "./Main.module.css";
import Card from "../common/Card/Card";
import { motion } from 'framer-motion'
import { fadeIn, paraAnim } from "../Animation/motion";
import { nextData, getPageNoData } from "../../Server/getButtons";
import { getUsersData } from "../../Server/getUsersData";
import { toast } from "react-hot-toast";
export default function Main({ modeToggle, modeToggleFunc, buttonsData, setButtonsData, totalBtns }) {
const [currentPage, setCurrentPage] = useState(
parseInt(localStorage.getItem("current_page")) || 1
);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const itemsPerPage = 9;
// Memoized calculations
const indexOfLastItem = useMemo(() => currentPage * itemsPerPage, [currentPage]);
const indexOfFirstItem = useMemo(() => indexOfLastItem - itemsPerPage, [indexOfLastItem]);
const totalPages = useMemo(() => Math.ceil(totalBtns / itemsPerPage), [totalBtns]);
const isActive = useCallback((pageIndex) => (
currentPage === pageIndex + 1 ? classes.active : ""
), [currentPage]);
const currentItems = useMemo(() => {
if (!Array.isArray(buttonsData)) return [];
return buttonsData.slice(indexOfFirstItem, indexOfLastItem);
}, [buttonsData, indexOfFirstItem, indexOfLastItem]);
const handlePageChange = useCallback(async (pageNumber) => {
if (pageNumber < 1 || pageNumber > totalPages || pageNumber === currentPage || isLoading) {
return;
}
setIsLoading(true);
setError(null);
try {
let newData = null;
if (pageNumber > currentPage) {
if (pageNumber - currentPage === 1) {
newData = await nextData(pageNumber);
} else {
newData = await getPageNoData(pageNumber);
}
if (newData && newData.length > 0) {
await getUsersData(newData);
setButtonsData(prevData => [...prevData, ...newData]);
}
}
setCurrentPage(pageNumber);
localStorage.setItem("current_page", pageNumber.toString());
// Smooth scroll to top of buttons section
setTimeout(() => {
window.scrollTo({ top: 500, behavior: "smooth" });
}, 100);
} catch (error) {
console.error("Error changing page:", error);
setError("Failed to load page. Please try again.");
toast.error("Failed to load page");
} finally {
setIsLoading(false);
}
}, [currentPage, totalPages, isLoading, setButtonsData]);
const pageNavigationButtons = useMemo(() => {
if (totalPages <= 1) return null;
return (
<ul className={classes.paginationList}>
{/* Previous button */}
<li
className={`${classes.paginationItem} ${currentPage === 1 ? classes.disabled : ''}`}
onClick={() => currentPage > 1 && handlePageChange(currentPage - 1)}
style={{ cursor: currentPage === 1 ? 'not-allowed' : 'pointer' }}
>
{"<"}
</li>
{/* Page numbers */}
{Array(totalPages)
.fill()
.map((_, index) => {
const pageNumber = index + 1;
// Show: first page, current page ± 1, last page
if (
pageNumber === 1 ||
pageNumber === currentPage ||
(pageNumber >= currentPage - 1 && pageNumber <= currentPage + 1) ||
pageNumber === totalPages
) {
return (
<li
key={pageNumber}
className={`${classes.paginationItem} ${isActive(index)} ${isLoading ? classes.loading : ''}`}
onClick={() => !isLoading && handlePageChange(pageNumber)}
style={{ cursor: isLoading ? 'not-allowed' : 'pointer' }}
>
{pageNumber}
</li>
);
} else if (
(pageNumber === currentPage - 2 && pageNumber > 1) ||
(pageNumber === currentPage + 2 && pageNumber < totalPages)
) {
return (
<li
key={`ellipsis-${pageNumber}`}
className={`${classes.paginationItem} ${classes.ellipsis}`}
>
...
</li>
);
}
return null;
})}
{/* Next button */}
<li
className={`${classes.paginationItem} ${currentPage === totalPages ? classes.disabled : ''}`}
onClick={() => currentPage < totalPages && handlePageChange(currentPage + 1)}
style={{ cursor: currentPage === totalPages ? 'not-allowed' : 'pointer' }}
>
{">"}
</li>
</ul>
);
}, [totalPages, currentPage, isActive, handlePageChange, isLoading]);
return (
<div className={classes.main_container}>
{error && (
<div className={classes.error_message}>
<p>{error}</p>
<button onClick={() => setError(null)}>Dismiss</button>
</div>
)}
{totalBtns === 0 ? (
<motion.h1
variants={paraAnim}
initial="hidden"
whileInView="visible"
viewport={{ once: true }}
transition={{ type: "spring", stiffness: 60 }}
className={classes.wait}
>
We Are Constantly Working To Provide You With The Best Possible Experience ...<br /><br />
Thank You For Your Patience 🫠
</motion.h1>
) : (
<div>
<h1 style={{ textAlign: "center", marginTop: "30px" }}>
Total number of Buttons added {totalBtns}
</h1>
{isLoading && (
<div className={classes.loading_indicator}>
Loading more buttons...
</div>
)}
<div className={classes.btns_container}>
{currentItems.map((button, index) => (
<motion.div
key={`${button.id}-${currentPage}`}
variants={fadeIn}
initial="hidden"
whileInView="visible"
viewport={{ once: true }}
transition={{ duration: 0.5, delay: (index % 3) * 0.3 }}
>
<Card modeToggle={modeToggle} button={button} />
</motion.div>
))}
</div>
<div className={classes.pagination}>
{totalPages > 1 && pageNavigationButtons}
</div>
</div>
)}
</div>
);
}