From c7a8b739d9ef399f62f70ca61fe648c328748f34 Mon Sep 17 00:00:00 2001 From: Jiahan William Wen Date: Sun, 16 Jun 2024 17:27:43 -0400 Subject: [PATCH] implemented pagination for FAQs display. Dark Mode and Mobile View are untested --- client/src/pages/FAQ/FAQ.jsx | 68 ++++++++++++++++++++++++++++++----- client/src/pages/FAQ/FAQ.scss | 27 ++++++++++++++ 2 files changed, 87 insertions(+), 8 deletions(-) diff --git a/client/src/pages/FAQ/FAQ.jsx b/client/src/pages/FAQ/FAQ.jsx index f1829cde..c099ba93 100644 --- a/client/src/pages/FAQ/FAQ.jsx +++ b/client/src/pages/FAQ/FAQ.jsx @@ -1,4 +1,4 @@ -import { React, useState, useEffect, useContext } from 'react'; +import React, { useState, useEffect, useContext } from 'react'; import PropTypes from 'prop-types'; import { getQuestions } from './functions'; import './FAQ.scss'; @@ -15,7 +15,7 @@ import { SnackbarContext } from '../../util/SnackbarProvider'; import LoadingAnimation from '../../components/misc/LoadingAnimation/LoadingAnimation'; const PageFAQ = () => { - const { darkMode, setDarkModeStatus } = useContext(DarkModeContext); + const { darkMode } = useContext(DarkModeContext); const [activeIndex, setActiveIndex] = useState(0); const [isSearch, setIsSearch] = useState(false); const [isMultiSearch, setIsMultiSearch] = useState(false); @@ -31,6 +31,10 @@ const PageFAQ = () => { const [errorLoading, setErrorLoading] = useState(false); const { setSnackbar } = useContext(SnackbarContext); + // Pagination state + const [currentPage, setCurrentPage] = useState(1); + const itemsPerPage = 5; + const loadQuestions = async () => { const data = await getQuestions(setSnackbar); if (!data) { @@ -78,6 +82,29 @@ const PageFAQ = () => { loadQuestions(); }, []); + useEffect(() => { + // Reset current page to 1 when category (activeIndex) changes + setCurrentPage(1); + }, [activeIndex]); + + // Calculate total pages + const totalPages = Math.ceil((allQuestions[activeIndex]?.length || 0) / itemsPerPage); + + // Handle page change + const handlePageChange = (page) => { + setCurrentPage(page); + }; + + // Get current page questions + const currentQuestions = allQuestions[activeIndex]?.slice( + (currentPage - 1) * itemsPerPage, + currentPage * itemsPerPage, + ); + + console.log('Current Page:', currentPage); + console.log('Total Pages:', totalPages); + console.log('Current Questions:', currentQuestions); + return (
@@ -128,10 +155,15 @@ const PageFAQ = () => { }`} > +
{ - const { darkMode, setDarkModeStatus } = useContext(DarkModeContext); + const { darkMode } = useContext(DarkModeContext); const filterQuestions = (questions, query) => { if (!query) { return questions; @@ -311,9 +343,9 @@ FAQButtons.propTypes = { questionCategories: PropTypes.array.isRequired, }; -const FAQCategoryAccordions = ({ allQuestions, activeIndex }) => { - if (allQuestions[activeIndex] === undefined) return <>; - const questionsAccordion = allQuestions[activeIndex].map((question, index) => ( +const FAQCategoryAccordions = ({ currentQuestions, activeIndex }) => { + if (!currentQuestions) return <>; + const questionsAccordion = currentQuestions.map((question, index) => (
@@ -322,7 +354,7 @@ const FAQCategoryAccordions = ({ allQuestions, activeIndex }) => { }; FAQCategoryAccordions.propTypes = { - allQuestions: PropTypes.array.isRequired, + currentQuestions: PropTypes.array.isRequired, activeIndex: PropTypes.number.isRequired, }; @@ -421,4 +453,24 @@ FAQDisplayAllSearchQuestion.propTypes = { questions: PropTypes.array.isRequired, }; +const PaginationControls = ({ currentPage, totalPages, handlePageChange }) => ( +
+ {Array.from({ length: totalPages }, (_, i) => ( + + ))} +
+); + +PaginationControls.propTypes = { + currentPage: PropTypes.number.isRequired, + totalPages: PropTypes.number.isRequired, + handlePageChange: PropTypes.func.isRequired, +}; + export { PageFAQ }; diff --git a/client/src/pages/FAQ/FAQ.scss b/client/src/pages/FAQ/FAQ.scss index 2069ea39..0f1eb475 100644 --- a/client/src/pages/FAQ/FAQ.scss +++ b/client/src/pages/FAQ/FAQ.scss @@ -284,3 +284,30 @@ text-align: center; color: var(--text-dynamic); } + +.pagination-controls { + display: flex; + justify-content: center; + margin-top: 20px; + + button { + margin: 0 5px; + padding: 10px 15px; + border: none; + border-radius: 5px; + background-color: var(--purple); + color: white; + cursor: pointer; + transition: background-color 0.3s, color 0.3s; + + &:hover { + background-color: var(--light-purple); + color: var(--purple-shades-dark); + } + + &.active { + background-color: var(--purple-shades-dark); + color: white; + } + } +}