|
1 | 1 | import React, { useEffect, useState } from "react";
|
2 |
| -import { Table, Tag, Typography, Badge, Button, message, Empty } from 'antd'; |
| 2 | +import { Table, Tag, Typography, Badge, Button, message, Empty, Modal } from 'antd'; |
3 | 3 | import type { ColumnsType } from 'antd/es/table';
|
4 | 4 | import styles from "./RecentAttemptsTable.module.css";
|
5 | 5 | import { HistoryEntry } from "domain/entities/HistoryEntry";
|
6 | 6 | import { historyUseCases } from "domain/usecases/HistoryUseCases";
|
| 7 | +import { ReactMarkdown } from "./common/ReactMarkdown"; |
7 | 8 |
|
8 | 9 | export const RecentAttemptsTable: React.FC = () => {
|
9 | 10 | const [recentAttemptsData, setRecentAttemptsData] = useState<HistoryEntry[]>([]);
|
10 | 11 | const [loading, setLoading] = useState<boolean>(true);
|
11 | 12 | const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
|
| 13 | + const [isModalVisible, setIsModalVisible] = useState<boolean>(false); |
| 14 | + const [currentCode, setCurrentCode] = useState<string>(""); |
12 | 15 |
|
13 | 16 | useEffect(() => {
|
14 | 17 | fetchRecentAttempts();
|
@@ -71,12 +74,31 @@ export const RecentAttemptsTable: React.FC = () => {
|
71 | 74 | }
|
72 | 75 | };
|
73 | 76 |
|
| 77 | + const showModal = (code: string) => { |
| 78 | + setCurrentCode(code); |
| 79 | + setIsModalVisible(true); |
| 80 | + }; |
| 81 | + |
| 82 | + const handleModalClose = () => { |
| 83 | + setIsModalVisible(false); |
| 84 | + setCurrentCode(""); |
| 85 | + }; |
| 86 | + |
74 | 87 | const columns: ColumnsType<HistoryEntry> = [
|
75 | 88 | {
|
76 | 89 | title: 'Date',
|
77 | 90 | dataIndex: 'date',
|
78 | 91 | key: 'date',
|
79 | 92 | sorter: (a, b) => new Date(a.attemptCompletedAt).getTime() - new Date(b.attemptCompletedAt).getTime(),
|
| 93 | + render: (_text, record) => { |
| 94 | + const startDate = new Date(record.attemptStartedAt).toLocaleString(); |
| 95 | + const endDate = new Date(record.attemptCompletedAt).toLocaleString(); |
| 96 | + return ( |
| 97 | + <span> |
| 98 | + {startDate} - {endDate} |
| 99 | + </span> |
| 100 | + ); |
| 101 | + }, |
80 | 102 | },
|
81 | 103 | {
|
82 | 104 | title: 'Title',
|
@@ -118,6 +140,15 @@ export const RecentAttemptsTable: React.FC = () => {
|
118 | 140 | </>
|
119 | 141 | ),
|
120 | 142 | },
|
| 143 | + { |
| 144 | + title: 'Action', |
| 145 | + key: 'action', |
| 146 | + render: (_text, record) => ( |
| 147 | + <Button type="link" onClick={() => showModal(record.attemptCode)}> |
| 148 | + View Code |
| 149 | + </Button> |
| 150 | + ), |
| 151 | + }, |
121 | 152 | ];
|
122 | 153 |
|
123 | 154 | // Row Selection Configuration
|
@@ -152,7 +183,23 @@ export const RecentAttemptsTable: React.FC = () => {
|
152 | 183 | locale={{
|
153 | 184 | emptyText: <Empty description="No attempts yet" />,
|
154 | 185 | }}
|
| 186 | + rowKey="key" // Ensure that each row has a unique key |
155 | 187 | />
|
| 188 | + |
| 189 | + {/* Modal for Viewing Code */} |
| 190 | + <Modal |
| 191 | + title="Attempt Code" |
| 192 | + open={isModalVisible} |
| 193 | + onCancel={handleModalClose} |
| 194 | + footer={[ |
| 195 | + <Button key="close" onClick={handleModalClose}> |
| 196 | + Close |
| 197 | + </Button>, |
| 198 | + ]} |
| 199 | + width={800} |
| 200 | + > |
| 201 | + <ReactMarkdown isReadOnly value={currentCode} /> |
| 202 | + </Modal> |
156 | 203 | </div>
|
157 | 204 | );
|
158 | 205 | };
|
0 commit comments