|
| 1 | +"use client"; |
| 2 | +import Header from "@/components/Header/header"; |
| 3 | +import { Layout, message, PaginationProps, Row, Table, Tag } from "antd"; |
| 4 | +import { Content } from "antd/es/layout/layout"; |
| 5 | +import { HistoryOutlined } from "@ant-design/icons"; |
| 6 | +import "./styles.scss"; |
| 7 | +import { useEffect, useState } from "react"; |
| 8 | +import React from "react"; |
| 9 | +import { useRouter } from "next/navigation"; |
| 10 | +import { GetUserHistories, History } from "@/app/services/history"; |
| 11 | +import { ValidateUser, VerifyTokenResponseType } from "@/app/services/user"; |
| 12 | + |
| 13 | +interface TablePagination { |
| 14 | + totalCount: number; |
| 15 | + totalPages: number; |
| 16 | + currentPage: number; |
| 17 | + limit: number; |
| 18 | +} |
| 19 | + |
| 20 | +export default function QuestionPage() { |
| 21 | + // Message States |
| 22 | + const [messageApi, contextHolder] = message.useMessage(); |
| 23 | + |
| 24 | + const error = (message: string) => { |
| 25 | + messageApi.open({ |
| 26 | + type: "error", |
| 27 | + content: message, |
| 28 | + }); |
| 29 | + }; |
| 30 | + |
| 31 | + const router = useRouter(); |
| 32 | + |
| 33 | + const [username, setUsername] = useState<string | undefined>(undefined); |
| 34 | + const [userQuestionHistories, setUserQuestionHistories] = |
| 35 | + useState<History[]>(); |
| 36 | + const [isHistoryLoading, setIsHistoryLoading] = useState<boolean>(true); |
| 37 | + const [paginationParams, setPaginationParams] = useState<TablePagination>({ |
| 38 | + totalCount: 0, |
| 39 | + totalPages: 0, |
| 40 | + currentPage: 1, |
| 41 | + limit: 10, |
| 42 | + }); |
| 43 | + |
| 44 | + // Handler for change in page jumper |
| 45 | + const onPageJump: PaginationProps["onChange"] = (pageNumber) => { |
| 46 | + setPaginationParams((prev) => { |
| 47 | + loadQuestionHistories(pageNumber, paginationParams.limit); |
| 48 | + return { ...paginationParams, currentPage: pageNumber }; |
| 49 | + }); |
| 50 | + }; |
| 51 | + |
| 52 | + // Handler for show size change for pagination |
| 53 | + const onShowSizeChange: PaginationProps["onShowSizeChange"] = ( |
| 54 | + current, |
| 55 | + pageSize |
| 56 | + ) => { |
| 57 | + setPaginationParams((prev) => { |
| 58 | + loadQuestionHistories(current, pageSize); |
| 59 | + return { ...paginationParams, currentPage: current, limit: pageSize }; |
| 60 | + }); |
| 61 | + }; |
| 62 | + |
| 63 | + async function loadQuestionHistories(currentPage: number, limit: number) { |
| 64 | + if (username === undefined) return; |
| 65 | + setIsHistoryLoading(true); |
| 66 | + GetUserHistories(username, currentPage, limit) |
| 67 | + .then((data: any) => { |
| 68 | + setUserQuestionHistories(data.histories); |
| 69 | + setPaginationParams({ |
| 70 | + ...paginationParams, |
| 71 | + totalCount: data.totalCount, |
| 72 | + totalPages: data.totalPages, |
| 73 | + currentPage: data.currentPage, |
| 74 | + limit: data.limit, |
| 75 | + }); |
| 76 | + }) |
| 77 | + .finally(() => { |
| 78 | + setIsHistoryLoading(false); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + loadQuestionHistories(paginationParams.currentPage, paginationParams.limit); |
| 84 | + }, [username]); |
| 85 | + |
| 86 | + useEffect(() => { |
| 87 | + ValidateUser().then((data: VerifyTokenResponseType) => { |
| 88 | + setUsername(data.data.username); |
| 89 | + }); |
| 90 | + }, []); |
| 91 | + |
| 92 | + const columns = [ |
| 93 | + { |
| 94 | + title: "Title", |
| 95 | + dataIndex: "title", |
| 96 | + key: "title", |
| 97 | + }, |
| 98 | + { |
| 99 | + title: "Categories", |
| 100 | + dataIndex: "questionTopics", |
| 101 | + key: "questionTopics", |
| 102 | + render: (categories: string[]) => |
| 103 | + categories.map((category) => <Tag key={category}>{category}</Tag>), |
| 104 | + }, |
| 105 | + { |
| 106 | + title: "Difficulty", |
| 107 | + dataIndex: "questionDifficulty", |
| 108 | + key: "questionDifficulty", |
| 109 | + render: (difficulty: string) => { |
| 110 | + let color = ""; |
| 111 | + if (difficulty === "easy") { |
| 112 | + color = "#2DB55D"; |
| 113 | + } else if (difficulty === "medium") { |
| 114 | + color = "orange"; |
| 115 | + } else if (difficulty === "hard") { |
| 116 | + color = "red"; |
| 117 | + } |
| 118 | + return ( |
| 119 | + <div style={{ color }}> |
| 120 | + {difficulty.charAt(0).toUpperCase() + difficulty.slice(1)} |
| 121 | + </div> |
| 122 | + ); |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + title: "Submitted at", |
| 127 | + dataIndex: "createdAt", |
| 128 | + key: "createdAt", |
| 129 | + render: (date: string) => { |
| 130 | + return new Date(date).toLocaleString(); |
| 131 | + }, |
| 132 | + }, |
| 133 | + { |
| 134 | + title: "Language", |
| 135 | + dataIndex: "language", |
| 136 | + key: "language", |
| 137 | + }, |
| 138 | + { |
| 139 | + title: "Matched with", |
| 140 | + dataIndex: "matchedUser", |
| 141 | + key: "matchedUser", |
| 142 | + }, |
| 143 | + ]; |
| 144 | + |
| 145 | + const handleRowClick = (h: History) => { |
| 146 | + // Link to page |
| 147 | + // questionId is just read as "history", as only the doc ref id is involved in requests |
| 148 | + // If the question database is reloaded, then the questionDocRefId may not be correct |
| 149 | + router.push( |
| 150 | + `/question/history?data=${h.questionDocRefId}&history=${h.historyDocRefId}` |
| 151 | + ); |
| 152 | + }; |
| 153 | + |
| 154 | + return ( |
| 155 | + <div> |
| 156 | + {contextHolder} |
| 157 | + <Layout className="layout"> |
| 158 | + <Header selectedKey={["0"]} /> |
| 159 | + <Content className="content"> |
| 160 | + <div className="content-card"> |
| 161 | + <div className="content-row-1" style={{ marginBottom: "20px" }}> |
| 162 | + <div className="content-title">Submission History</div> |
| 163 | + </div> |
| 164 | + <div className="content-table"> |
| 165 | + <Table |
| 166 | + rowKey="id" |
| 167 | + dataSource={userQuestionHistories} |
| 168 | + columns={columns} |
| 169 | + onRow={(record: any) => { |
| 170 | + return { |
| 171 | + onClick: () => handleRowClick(record), |
| 172 | + style: { cursor: "pointer" }, |
| 173 | + }; |
| 174 | + }} |
| 175 | + loading={isHistoryLoading} |
| 176 | + pagination={{ |
| 177 | + current: paginationParams.currentPage, |
| 178 | + total: paginationParams.totalCount, |
| 179 | + pageSize: paginationParams.limit, |
| 180 | + onChange: onPageJump, |
| 181 | + showSizeChanger: true, |
| 182 | + onShowSizeChange: onShowSizeChange, |
| 183 | + }} |
| 184 | + /> |
| 185 | + </div> |
| 186 | + </div> |
| 187 | + </Content> |
| 188 | + </Layout> |
| 189 | + </div> |
| 190 | + ); |
| 191 | +} |
0 commit comments