|
| 1 | +// InterviewQuestionsTable.tsx |
| 2 | + |
| 3 | +import React, { useEffect, useState } from 'react'; |
| 4 | +import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Select, MenuItem, TablePagination, SelectChangeEvent } from '@mui/material'; |
| 5 | +import { useData } from "../../data/data.context"; |
| 6 | + |
| 7 | +interface Question { |
| 8 | + title: string; |
| 9 | + tags: string[]; |
| 10 | + categories: string[]; |
| 11 | + constraints: string[]; |
| 12 | + difficulty: string; |
| 13 | + description: string; |
| 14 | +} |
| 15 | + |
| 16 | +const ITEMS_PER_PAGE_OPTIONS = [5, 10]; // Number of items to display per page |
| 17 | + |
| 18 | +const InterviewQuestionsTable: React.FC = () => { |
| 19 | + const [questionsData, setQuestions] = useState<Question[]>([]); |
| 20 | + const [currentPage, setCurrentPage] = useState<number>(1); |
| 21 | + const [itemsPerPage, setItemsPerPage] = useState<number>(ITEMS_PER_PAGE_OPTIONS[0]); |
| 22 | + const { questions, getQuestions } = useData(); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + async function getInterviewQuestions() { |
| 26 | + await getQuestions(); |
| 27 | + } |
| 28 | + getInterviewQuestions(); |
| 29 | + console.log("here"); |
| 30 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 31 | + }, []); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + setQuestions(questions); |
| 35 | + //console.log(questionsData); |
| 36 | + }, [questions]); |
| 37 | + |
| 38 | + const handlePageChange = (event: unknown, newPage: number) => { |
| 39 | + setCurrentPage(newPage); |
| 40 | + }; |
| 41 | + |
| 42 | + const handleChangeItemsPerPage = ( |
| 43 | + event: SelectChangeEvent<unknown>, |
| 44 | + ) => { |
| 45 | + setItemsPerPage(event.target.value as number); |
| 46 | + setCurrentPage(1); |
| 47 | + }; |
| 48 | + |
| 49 | + const indexOfLastQuestion = currentPage * itemsPerPage; |
| 50 | + const indexOfFirstQuestion = indexOfLastQuestion - itemsPerPage; |
| 51 | + const currentQuestions = questionsData.slice(indexOfFirstQuestion, indexOfLastQuestion); |
| 52 | + |
| 53 | + return ( |
| 54 | + <><div style={{ maxHeight: '300px', overflowY: 'auto', width: '80%' }}> |
| 55 | + <TableContainer component={Paper} style={{ margin: '10px', padding: '10px' }}> |
| 56 | + <Table style={{ minWidth: 650, fontSize: '14px' }}> |
| 57 | + <TableHead> |
| 58 | + <TableRow > |
| 59 | + <TableCell>Title</TableCell> |
| 60 | + <TableCell>Tags</TableCell> |
| 61 | + <TableCell>Categories</TableCell> |
| 62 | + <TableCell>Constraints</TableCell> |
| 63 | + <TableCell>Difficulty</TableCell> |
| 64 | + <TableCell>Description</TableCell> |
| 65 | + </TableRow> |
| 66 | + </TableHead> |
| 67 | + <TableBody> |
| 68 | + {currentQuestions.map((question, index) => ( |
| 69 | + <TableRow key={index}> |
| 70 | + <TableCell>{question.title}</TableCell> |
| 71 | + <TableCell>{question.tags.join(', ')}</TableCell> |
| 72 | + <TableCell>{question.categories.join(', ')}</TableCell> |
| 73 | + <TableCell>{question.constraints.join(', ')}</TableCell> |
| 74 | + <TableCell>{question.difficulty}</TableCell> |
| 75 | + <TableCell>{question.description}</TableCell> |
| 76 | + </TableRow> |
| 77 | + ))} |
| 78 | + </TableBody> |
| 79 | + </Table> |
| 80 | + </TableContainer> |
| 81 | + </div><div style={{ display: 'flex', alignItems: 'center' }}> |
| 82 | + <Select |
| 83 | + value={itemsPerPage} |
| 84 | + onChange={handleChangeItemsPerPage} |
| 85 | + style={{ marginTop: '10px' }} |
| 86 | + > |
| 87 | + {ITEMS_PER_PAGE_OPTIONS.map((option) => ( |
| 88 | + <MenuItem key={option} value={option}> |
| 89 | + {`${option} per page`} |
| 90 | + </MenuItem> |
| 91 | + ))} |
| 92 | + </Select> |
| 93 | + |
| 94 | + <TablePagination |
| 95 | + rowsPerPageOptions={[]} |
| 96 | + component="div" |
| 97 | + count={questionsData.length} |
| 98 | + rowsPerPage={itemsPerPage} |
| 99 | + page={currentPage - 1} |
| 100 | + onPageChange={handlePageChange} /> |
| 101 | + </div></> |
| 102 | + |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +export default InterviewQuestionsTable; |
0 commit comments