|
| 1 | +// AttemptHistory.js |
| 2 | +import React, { useState, useEffect } from 'react'; |
| 3 | +import HistoryAttempt from './HistoryAttempt'; |
| 4 | +import historyService from "../../services/history"; |
| 5 | +import { getUserFromToken } from "./utils/authUtils"; |
| 6 | +// import {jwtDecode} from "jwt-decode"; |
| 7 | +// import userService from "../../services/users"; |
| 8 | + |
| 9 | +// export async function getUserFromToken() { |
| 10 | +// const jwtToken = sessionStorage.getItem('jwt_token'); |
| 11 | +// if (jwtToken) { |
| 12 | +// const decodedToken = jwtDecode(jwtToken); |
| 13 | +// try { |
| 14 | +// // decodedToken has an id field in auth-controller.js |
| 15 | +// // user fetched by id has a username field in user-model.js |
| 16 | +// const id = decodedToken.id; |
| 17 | +// const user = await userService.getUser( |
| 18 | +// decodedToken.id, {headers: {Authorization: `Bearer ${jwtToken}`}}) |
| 19 | +// // getUser return an Object with data, message, and type |
| 20 | +// // The user data is nested in Object.data and hence we need a double .data to access it |
| 21 | +// return {id: id, username: user.data.data.username}; |
| 22 | +// } catch (error) { |
| 23 | +// console.error(error); |
| 24 | +// return "No User"; |
| 25 | +// } |
| 26 | +// } |
| 27 | +// return "No User"; |
| 28 | +// } |
| 29 | + |
| 30 | + |
| 31 | +const HistoryTable = ({userID}) => { |
| 32 | + // const [userID, setUserID] = useState(null); |
| 33 | + const [history, setHistory] = useState([]); |
| 34 | + |
| 35 | + // useEffect(() => { |
| 36 | + // async function fetchData() { |
| 37 | + // try { |
| 38 | + // const user = await getUserFromToken(); |
| 39 | + // if (user) { |
| 40 | + // setUserID(user.userId); // asynchronous |
| 41 | + // const result = await historyService.getHistoryByUserId(user.userId); |
| 42 | + // setHistory(result.data); |
| 43 | + // console.log(`I have gotten the user id: ${user.userId}`) |
| 44 | + // } |
| 45 | + // } catch (error) { |
| 46 | + // console.error('Error fetching attempt history:', error); |
| 47 | + // } |
| 48 | + // } |
| 49 | + // fetchData(); |
| 50 | + // }, []); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + async function fetchData() { |
| 54 | + try { |
| 55 | + if (userID) { |
| 56 | + const result = await historyService.getHistoryByUserId(userID); |
| 57 | + setHistory(result.data); |
| 58 | + console.log(`I have gotten the user id in table: ${userID}`) |
| 59 | + } |
| 60 | + } catch (error) { |
| 61 | + console.error('Error fetching attempt history:', error); |
| 62 | + } |
| 63 | + } |
| 64 | + fetchData(); |
| 65 | + }, []); |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className="history-table"> |
| 69 | + <h2>Attempt History</h2> |
| 70 | + <table className="table table-striped"> |
| 71 | + <thead> |
| 72 | + <tr> |
| 73 | + <th>Matched User</th> |
| 74 | + <th>Question</th> |
| 75 | + <th>Start Time</th> |
| 76 | + <th>Duration</th> |
| 77 | + <th>Code</th> |
| 78 | + </tr> |
| 79 | + </thead> |
| 80 | + <tbody> |
| 81 | + {history.map((attempt, index) => ( |
| 82 | + <HistoryAttempt key={index} attempt={attempt} /> |
| 83 | + ))} |
| 84 | + </tbody> |
| 85 | + </table> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +export default HistoryTable; |
0 commit comments