|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { |
| 3 | + Table, |
| 4 | + Thead, |
| 5 | + Tbody, |
| 6 | + Tr, |
| 7 | + Th, |
| 8 | + Td, |
| 9 | + TableCaption, |
| 10 | + Center, |
| 11 | + TableContainer, |
| 12 | +} from "@chakra-ui/react"; |
| 13 | +import { Paginator } from "../Paginator/Paginator.component"; |
| 14 | +import { SolvedQuestion } from "../../models/SolvedQuestion.model"; |
| 15 | +import { fetchUserCompletedQuestions } from "../../api/user"; // Import your API function |
| 16 | + |
| 17 | +export type TableProp = { |
| 18 | + userId: String; |
| 19 | + pageSize?: number; |
| 20 | +}; |
| 21 | + |
| 22 | +export const SolvedTable = (props: TableProp) => { |
| 23 | + const { userId, pageSize = 10 } = props; |
| 24 | + const [currentPage, setCurrentPage] = useState(1); |
| 25 | + const [solvedQuestions, setSolvedQuestions] = useState<SolvedQuestion[]>([]); |
| 26 | + |
| 27 | + const onPageChange = (page: React.SetStateAction<number>) => { |
| 28 | + setCurrentPage(page); |
| 29 | + }; |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + const loadSolvedQuestions = async () => { |
| 33 | + if (userId) { |
| 34 | + try { |
| 35 | + const data = await fetchUserCompletedQuestions(userId); |
| 36 | + setSolvedQuestions(data); |
| 37 | + } catch (error) { |
| 38 | + console.error("Failed to fetch solved questions:", error); |
| 39 | + } |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + loadSolvedQuestions(); |
| 44 | + }, [userId]); |
| 45 | + |
| 46 | + const getQuestionsForPage = () => { |
| 47 | + const startIndex = (currentPage - 1) * pageSize; |
| 48 | + return solvedQuestions.slice(startIndex, startIndex + pageSize); |
| 49 | + }; |
| 50 | + |
| 51 | + return ( |
| 52 | + <TableContainer width="100%"> |
| 53 | + <Table |
| 54 | + variant="striped" |
| 55 | + backgroundColor={"white"} |
| 56 | + size="md" |
| 57 | + boxShadow="md" |
| 58 | + transition="box-shadow 0.2s" |
| 59 | + _hover={{ |
| 60 | + boxShadow: "xl", |
| 61 | + }} |
| 62 | + width="60%" |
| 63 | + sx={{ tableLayout: "fixed" }} |
| 64 | + > |
| 65 | + <TableCaption> |
| 66 | + <Center> |
| 67 | + <Paginator |
| 68 | + adjacent={1} |
| 69 | + page={currentPage} |
| 70 | + totalPages={Math.ceil(solvedQuestions.length / pageSize)} |
| 71 | + onPageChange={onPageChange} |
| 72 | + /> |
| 73 | + </Center> |
| 74 | + </TableCaption> |
| 75 | + <Thead> |
| 76 | + <Tr> |
| 77 | + <Th colSpan={4}>Completed Questions</Th> |
| 78 | + </Tr> |
| 79 | + <Tr> |
| 80 | + <Th>Question</Th> |
| 81 | + <Th>Topics</Th> |
| 82 | + <Th isNumeric>Difficulty</Th> |
| 83 | + <Th isNumeric>Solved Date</Th> |
| 84 | + </Tr> |
| 85 | + </Thead> |
| 86 | + <Tbody> |
| 87 | + {getQuestionsForPage().map((question) => ( |
| 88 | + <Tr key={question._id}> |
| 89 | + <Td>{question.title}</Td> |
| 90 | + <Td>{question.topics.join(", ")}</Td> |
| 91 | + <Td isNumeric>{question.difficulty}</Td> |
| 92 | + <Td isNumeric> |
| 93 | + {question.solvedDate |
| 94 | + ? new Date(question.solvedDate).toLocaleDateString() |
| 95 | + : "Not Available"} |
| 96 | + </Td> |
| 97 | + </Tr> |
| 98 | + ))} |
| 99 | + </Tbody> |
| 100 | + </Table> |
| 101 | + </TableContainer> |
| 102 | + ); |
| 103 | +}; |
0 commit comments