import { useState } from "react"; import { Box, Button, CircularProgress, Paper, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Typography, Pagination, Dialog, DialogTitle, DialogContent, DialogActions, } from "@mui/material"; import { useQuestions } from "../../hooks/questions/useQuestions"; import { useDeleteQuestion } from "../../hooks/questions/useDeleteQuestion"; import Container from "../../components/shared/Container"; import { useNavigate } from "react-router-dom"; import type { QuestionType } from "../../components/shared/types/QuestionTypes"; import { useAuth } from "../../context/AuthContext"; const QuestionsPage = () => { const [currentPage, setCurrentPage] = useState(1); const [deleteQuestionId, setDeleteQuestionId] = useState(null); const { data, isLoading, isError } = useQuestions({ page: currentPage }); const deleteMutation = useDeleteQuestion(); const navigate = useNavigate(); const { user } = useAuth(); const handleCreateQuestion = () => { navigate("/dashboard/questions/create"); }; const handleUpdateQuestion = (id: number) => { navigate(`/dashboard/questions/${id}/update`); }; const handleViewQuestion = (id: number) => { navigate(`/questions/${id}`); }; const handleDeleteQuestion = () => { if (deleteQuestionId !== null) { deleteMutation.mutate(deleteQuestionId, { onSuccess: () => setDeleteQuestionId(null), }); } }; if (isLoading) return ; if (isError) return Error loading questions.; return ( Questions ID Title Type Difficulty Category Author Actions {data?.data.map((question: QuestionType) => ( {question.id} {question.title} {question.type} {question.difficulty} {question.category?.name ?? "—"} {question.author.username} {(user?.type === "admin" || user?.id == question.author.id) && ( <> )} ))}
setCurrentPage(value)} sx={{ mt: 3, mb: 3, display: "flex", justifyContent: "center" }} /> setDeleteQuestionId(null)} > Confirm Delete Are you sure you want to delete this question?
); }; export default QuestionsPage;