From 7c5bf7dbfad33f49a7d21c273b6dc1482dc8ea64 Mon Sep 17 00:00:00 2001 From: dustinusey Date: Sat, 3 Feb 2024 00:06:44 -0600 Subject: [PATCH] added pagination functionality --- src/App.jsx | 3 + src/Components/ProjectTable.jsx | 58 +++++++++++------- src/Components/StatusDropdown.jsx | 2 +- src/Components/TablePagination.jsx | 95 ++++++++++++++++-------------- 4 files changed, 90 insertions(+), 68 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index c3064fc..727d0ae 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -9,6 +9,7 @@ export default function App() { const [loading, isLoading] = useState(false); const [currentProject, setCurrentProject] = useState(""); const [projects, setProjects] = useState([]); + const [currentPage, setCurrentPage] = useState(1); // initially sets theme in LS to false useEffect(() => { @@ -30,6 +31,8 @@ export default function App() { setCurrentProject, projects, setProjects, + currentPage, + setCurrentPage, }} > diff --git a/src/Components/ProjectTable.jsx b/src/Components/ProjectTable.jsx index 03b01c0..d59ceaf 100644 --- a/src/Components/ProjectTable.jsx +++ b/src/Components/ProjectTable.jsx @@ -1,6 +1,6 @@ import { createClient } from "@supabase/supabase-js"; import axios from "axios"; -import { useContext, useEffect, useState } from "react"; +import { useContext, useEffect } from "react"; import { AppState } from "../App"; import Alert from "./Alerts/Alert"; import Project from "./Project"; @@ -15,8 +15,17 @@ const supabase = createClient( ); export default function ProjectTable() { - const { theme, alert, setAlert, loading, isLoading, projects, setProjects } = - useContext(AppState); + const { + theme, + alert, + loading, + isLoading, + projects, + setProjects, + currentPage, + setCurrentPage, + setAlert, + } = useContext(AppState); useEffect(() => { fetchProjects(); @@ -31,6 +40,8 @@ export default function ProjectTable() { async function getProjects() { isLoading(true); const { data } = await supabase.from("projects").select(); + + setCurrentPage(1); return data; } @@ -46,7 +57,7 @@ export default function ProjectTable() { messages: [ { role: "user", - content: `I want to you to return a JSON object of an array named 'frontendProjects' of random, quick, beginner friendly frontend project ideas. ONLY return a JSON object. Please exclude any of these projects: Pomodoro timers, calculators, or any of these: ${projects.map( + content: `I want to you to return a JSON object of an array named 'frontendProjects' of random, quick, beginner friendly frontend project ideas. ONLY return a JSON object. Please exclude any of these projects: Portfolio websites, Pomodoro timers, calculators, calendars or any of these: ${projects.map( (proj) => proj.name )}`, }, @@ -134,24 +145,27 @@ export default function ProjectTable() { {/* table body */} - {projects.map((project) => { - const id = project.id.toString(); - const date = new Date(project.created_at); - const formattedDate = `${date.toLocaleString("default", { - month: "long", - })} ${date.getDate()}, ${date.getFullYear()}`; - return ( - - ); - })} + {/* only render the projects based off the currentPage and only wanting to show 5 per page */} + {projects + .slice((currentPage - 1) * 5, currentPage * 5) + .map((project) => { + const id = project.id.toString(); + const date = new Date(project.created_at); + const formattedDate = `${date.toLocaleString("default", { + month: "long", + })} ${date.getDate()}, ${date.getFullYear()}`; + return ( + + ); + })} diff --git a/src/Components/StatusDropdown.jsx b/src/Components/StatusDropdown.jsx index 9bef18f..d294623 100644 --- a/src/Components/StatusDropdown.jsx +++ b/src/Components/StatusDropdown.jsx @@ -30,7 +30,7 @@ export default function StatusDropdown(props) { > {props.status === "abandoned" ? "Abandoned" : "Completed"} - {props.stats === "abandoned" ? ( + {props.status === "abandoned" ? (
    {/* previous arrow */} -
  • +
  • { + currentPage > 1 && setCurrentPage(currentPage - 1); + }} + > Previous
  • -
  • - - 1 - -
  • -
  • - - 2 - -
  • -
  • - - 3 - -
  • -
  • - - 4 - -
  • -
  • - - 5 - -
  • + + {/* page numbers */} + {projects.map((project, index) => { + if (index % pagination.itemsPerPage === 0) { + return ( +
  • + setCurrentPage(index / pagination.itemsPerPage + 1) + } + key={index} + > + + {index / pagination.itemsPerPage + 1} + +
  • + ); + } + })} {/* next arrow */} -
  • +
  • { + currentPage < pagination.totalPages && + setCurrentPage(currentPage + 1); + }} + > Next