|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { |
| 3 | + Table, |
| 4 | + Thead, |
| 5 | + Tbody, |
| 6 | + Tr, |
| 7 | + Th, |
| 8 | + Td, |
| 9 | + TableCaption, |
| 10 | + TableContainer, |
| 11 | + HStack, |
| 12 | + Tag, |
| 13 | + Center, |
| 14 | + IconButton, |
| 15 | +} from "@chakra-ui/react"; |
| 16 | +import { Paginator } from "../Paginator/Paginator.component"; |
| 17 | +import { Question } from "../../models/Quesiton.model"; |
| 18 | +import { diffToScheme, isAdmin } from "../../helper/UIHelper"; |
| 19 | +import { DeleteIcon } from "@chakra-ui/icons"; |
| 20 | +import { deleteDummyQn } from "../../data/sampleqn"; |
| 21 | + |
| 22 | +export type TableProp = { |
| 23 | + filteredQn: Question[]; |
| 24 | + pageSize?: number; |
| 25 | +}; |
| 26 | + |
| 27 | +const QnEntry = (qn: Question) => { |
| 28 | + return ( |
| 29 | + <Tr> |
| 30 | + <Td>{qn.displayedQuestion}</Td> |
| 31 | + <Td> |
| 32 | + <HStack spacing={1}> |
| 33 | + {qn.categories.map((qntype) => ( |
| 34 | + <Tag colorScheme="blue">{qntype}</Tag> |
| 35 | + ))} |
| 36 | + </HStack> |
| 37 | + </Td> |
| 38 | + <Td isNumeric> |
| 39 | + <Tag colorScheme={diffToScheme(qn.difficulty)}>{qn.difficulty}</Tag> |
| 40 | + </Td> |
| 41 | + {isAdmin ? ( |
| 42 | + <Td> |
| 43 | + <IconButton |
| 44 | + aria-label="Delete Qn" |
| 45 | + colorScheme="red" |
| 46 | + icon={<DeleteIcon />} |
| 47 | + onClick={() => deleteDummyQn(qn.id)} |
| 48 | + /> |
| 49 | + </Td> |
| 50 | + ) : ( |
| 51 | + <></> |
| 52 | + )} |
| 53 | + </Tr> |
| 54 | + ); |
| 55 | +}; |
| 56 | + |
| 57 | +export const QnTable = (pp: TableProp) => { |
| 58 | + const { filteredQn, pageSize = 10 } = pp; |
| 59 | + const [pageNumber, changePage] = useState(1); |
| 60 | + |
| 61 | + const getCurrentPage = (x: number) => { |
| 62 | + return filteredQn.slice( |
| 63 | + (x - 1) * pageSize, |
| 64 | + Math.min(x * pageSize, filteredQn.length) |
| 65 | + ); |
| 66 | + }; |
| 67 | + |
| 68 | + const onPageChange = (x: number) => { |
| 69 | + changePage(x); |
| 70 | + }; |
| 71 | + |
| 72 | + return ( |
| 73 | + <> |
| 74 | + <TableContainer width="100%"> |
| 75 | + <Center> |
| 76 | + <Table |
| 77 | + variant="striped" |
| 78 | + backgroundColor={"white"} |
| 79 | + size="md" |
| 80 | + boxShadow="md" |
| 81 | + transition="box-shadow 0.2s" |
| 82 | + _hover={{ |
| 83 | + boxShadow: "xl", |
| 84 | + }} |
| 85 | + width="80%" |
| 86 | + sx={{ tableLayout: "fixed" }} |
| 87 | + > |
| 88 | + <TableCaption> |
| 89 | + <Center> |
| 90 | + <Paginator |
| 91 | + adjacent={1} |
| 92 | + page={pageNumber} |
| 93 | + totalPages={Math.ceil(filteredQn.length / pageSize)} |
| 94 | + onPageChange={onPageChange} |
| 95 | + /> |
| 96 | + </Center> |
| 97 | + </TableCaption> |
| 98 | + <Thead> |
| 99 | + <Tr boxShadow="base"> |
| 100 | + <Th>Questions</Th> |
| 101 | + <Th>Type</Th> |
| 102 | + <Th isNumeric>Difficulty</Th> |
| 103 | + {isAdmin ? <Th>Modify/Delete</Th> : <></>} |
| 104 | + </Tr> |
| 105 | + </Thead> |
| 106 | + <Tbody>{getCurrentPage(pageNumber).map((qn) => QnEntry(qn))}</Tbody> |
| 107 | + </Table> |
| 108 | + </Center> |
| 109 | + </TableContainer> |
| 110 | + </> |
| 111 | + ); |
| 112 | +}; |
0 commit comments