Skip to content

Commit

Permalink
feat: pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
btwlouis committed Apr 23, 2024
1 parent df2f215 commit 3b87c73
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions frontend/src/components/ServerList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@ const ServerList = ({ searchValue }) => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
const [currentPage, setCurrentPage] = useState(1);
const [itemsPerPage, setItemsPerPage] = useState(3);

useEffect(() => {
const fetchData = async () => {
try {
const data = await axios.get(process.env.REACT_APP_API_URL + "servers");
const response = await axios.get(
process.env.REACT_APP_API_URL + "servers"
);

if (data.status != 200) {
return setError("Error fetching data");
if (response.status !== 200) {
throw new Error("Error fetching data");
}

const response = data.data;

setData(response);
setData(response.data);
setItemsPerPage(response.data.length / 5);
setLoading(false);
} catch (error) {
setError(error);
setLoading(true);
setError(error.message);
setLoading(false);
}
};

Expand All @@ -47,20 +50,47 @@ const ServerList = ({ searchValue }) => {
navigate(`/server/${id}`);
};

const filteredData = data.filter((item) =>
// Calculate indexes for pagination
const indexOfLastItem = currentPage * itemsPerPage;
const indexOfFirstItem = indexOfLastItem - itemsPerPage;
const currentItems = data.slice(indexOfFirstItem, indexOfLastItem);

// Change page
const paginate = (pageNumber) => setCurrentPage(pageNumber);

const filteredData = currentItems.filter((item) =>
item.Data.hostname.toLowerCase().includes(searchValue.toLowerCase())
);

return (
<div>
{loading ? (
<Loading></Loading>
<Loading />
) : error ? (
<div>{error}</div>
) : (
<CContainer>
<CContainer id="top">
<h2 className="mt-3">Server List ({filteredData.length})</h2>

<nav className="d-flex justify-content-end">
<ul className="pagination">
{[...Array(Math.ceil(data.length / itemsPerPage)).keys()].map(
(number) => (
<li
key={number}
className={`page-item ${
currentPage === number + 1 ? "active" : ""
}`}>
<a
onClick={() => paginate(number + 1)}
href="#top"
className="page-link">
{number + 1}
</a>
</li>
)
)}
</ul>
</nav>
{filteredData.map((item, index) => (
<CRow className="mb-3" key={index}>
<CCol xs="12">
Expand Down

0 comments on commit 3b87c73

Please sign in to comment.