Skip to content

Commit

Permalink
Merge branch 'test-updated-merge' of https://github.com/CS3219-AY2324…
Browse files Browse the repository at this point in the history
…S1/ay2324s1-course-assessment-g16 into test-updated-merge
  • Loading branch information
Jai2501 committed Nov 15, 2023
2 parents 1e76297 + 3b29015 commit 2b40d14
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 204 deletions.
39 changes: 28 additions & 11 deletions Frontend/src/Components/Collaboration/CollaborationWindow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Popup from "reactjs-popup";
import "reactjs-popup/dist/index.css";
import {getUserId} from "../../User/UserState";
import {useLocation} from "react-router-dom";
import {addUserAttempt} from "../../History/HistoryServiceAPI";
import CommunicationWindow from "./CommunicationWindow";

import axios from "axios";
Expand All @@ -25,13 +26,14 @@ const CollaborationWindow = () => {
const navigate = useNavigate();
const socket = useRef(null);
const [canExtend, setCanExtend] = useState(false);
const [otherUserId, setOtherUserId] = useState("");
const location = useLocation();
const {sessionId, collaboratorId} = location.state || {};

useEffect(() => {
const questionStored = JSON.parse(sessionStorage.getItem("savedQuestion"));
if (questionStored) {
setQuestion(questionStored);
setQuestion(questionStored);
}

if (sessionId && collaboratorId) {
Expand All @@ -53,11 +55,12 @@ const CollaborationWindow = () => {
socket.current.on("user-joined", (userId) => {
console.log(`User ${userId} joined the session`);
showToast("Get ready to collaborate and solve the challenge!");
setOtherUserId(userId);
// Handle new user joining
});

socket.current.on('recv-question', (receivedQuestion) => {
console.log('Received question: ', receivedQuestion);
socket.current.on("recv-question", (receivedQuestion) => {
console.log("Received question: ", receivedQuestion);
setQuestion(receivedQuestion);
});

Expand Down Expand Up @@ -87,8 +90,9 @@ const CollaborationWindow = () => {
});

socket.current.on("system-terminate", (sessionId) => {
showToast("Session ended, redirecting to home page...");
setTimeout(() => navigate('/landing'), 1500); // Navigate to home or another route
showToast("Session ended, redirecting to home page...");
addUserAttempt(userId);
setTimeout(() => navigate("/landing"), 1500); // Navigate to home or another route
});

socket.current.on("user-reconnected", (userId) => {
Expand Down Expand Up @@ -119,7 +123,7 @@ const CollaborationWindow = () => {

useEffect(() => {
if (question) {
sessionStorage.setItem("savedQuestion", JSON.stringify(question));
sessionStorage.setItem("savedQuestion", JSON.stringify(question));
}
}, [question]);

Expand All @@ -142,11 +146,18 @@ const CollaborationWindow = () => {
// }
// };

const handleEndSession = () => {
const handleEndSession = async () => {
// Emit a 'terminate-session' event to the server
socket.current.emit("user-terminate"); // Replace 'session-id' with the actual session ID

showToast("Session terminated");
const result = await addUserAttempt(
userId,
otherUserId,
sessionId,
question._id
);
console.log(result);

// to home or another route after a short delay
setTimeout(() => navigate("/landing"), 1500);
Expand All @@ -168,9 +179,15 @@ const CollaborationWindow = () => {
.padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
};

const handleSubmit = () => {
const handleSubmit = async () => {
socket.current.emit("user-terminate", {sessionId: "session-id"});

const result = await addUserAttempt(
userId,
otherUserId,
sessionId,
question._id
);
console.log(result);
showToast("Your code has been submitted");
setTimeout(() => navigate("/landing"), 1500);
};
Expand Down Expand Up @@ -267,8 +284,8 @@ const CollaborationWindow = () => {
<div className="question-section">
{question ? (
<>
<h2>{question.title}</h2>
<p>{question.description}</p>
<h2>{question.title}</h2>
<p>{question.description}</p>
</>
) : (
<p>Loading question...</p>
Expand Down
50 changes: 24 additions & 26 deletions Frontend/src/Components/LandingPage/Attempt.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import React from "react";
import React, {useEffect, useState} from "react";
import "./Attempt.css";
import Utility from "../../Utility/Utility";

export const Attempt = ({ i, attempt, setSelectedAttempt, setIsList }) => {
let tagClass = Utility.setDifficultyTag("user-q-tag", attempt.complexity);

console.log(attempt);
return (
<div
className="attempt-container"
onClick={() => {
setSelectedAttempt(attempt);
setIsList(false);
}}
>
<div className="attempt-name">
<div className="attempt-id"> #{i + 1} </div>
<div className="attempt-title">{attempt.title}</div>
</div>
<div className="attempt-tags">
{/* to be replaced with date passed as component parameter */}
<div className="user-q-tag attempt-date">31/10/2023</div>
<div className={tagClass}>{attempt.complexity}</div>
<div className="user-q-tag">{attempt.category}</div>
</div>
</div>
);
};
export const Attempt = ({i, attempt, setSelectedAttempt, setIsList}) => {
let tagClass = Utility.setDifficultyTag("user-q-tag", question.complexity);

return (
<div
className="attempt-container"
onClick={() => {
setSelectedAttempt(attempt);
setIsList(false);
}}
>
<div className="attempt-name">
<div className="attempt-id"> #{i + 1} </div>
<div className="attempt-title">{attempt.title}</div>
</div>
<div className="attempt-tags">
<div className="user-q-tag attempt-date">{question.attempt_time}</div>
<div className={tagClass}>{question.complexity}</div>
<div className="user-q-tag">{question.category}</div>
</div>
</div>
);
};
68 changes: 37 additions & 31 deletions Frontend/src/Components/LandingPage/AttemptHistory.jsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
import React, {useState, useEffect} from 'react'
import {Attempt} from "./Attempt"
import {AttemptView} from "./AttemptView"
import './AttemptHistory.css'
import { getUserAttempts } from '../../History/HistoryServiceAPI'
import {getUserId} from "../../User/UserState"
import React, {useState, useEffect} from "react";
import {Attempt} from "./Attempt";
import {AttemptView} from "./AttemptView";
import "./AttemptHistory.css";
import {getUserAttempts} from "../../History/HistoryServiceAPI";
import {getUserId} from "../../User/UserState";

export const AttemptHistory = ({isList, setIsList}) => {
const [attempts, setAttempts] = useState([]);
const [selectedAttempt, setSelectedAttempt] = useState(null);
const [attempts, setAttempts] = useState([]);
const [selectedAttempt, setSelectedAttempt] = useState(null);

// to be replaced with actual attempt fetch
const fetchAttempts = async () => {
setAttempts(getUserAttempts(getUserId()));
console.log(attempts);
};

useEffect(() => {
fetchAttempts();
}, []);
const fetchAttempts = async () => {
const results = await getUserAttempts(getUserId());
setAttempts(results);
};

return (
<div className="attempt-history-container">
{isList?
<div className="attempt-list-container">
{/* to be replaced by mapping over actual stored attempts */}
{/* {attempts.map((a, index) => (
<Attempt key = {index} attempt = {a} i = {index}
setSelectedAttempt = {setSelectedAttempt} setIsList = {setIsList}/>))} */}
</div>:
<AttemptView attempt = {selectedAttempt} setIsList = {setIsList}/>
}
</div>
)
}
useEffect(() => {
fetchAttempts();
}, []);

return (
<div className="attempt-history-container">
{isList ? (
<div className="attempt-list-container">
{/* to be replaced by mapping over actual stored attempts */}
{attempts.map((a, index) => (
<Attempt
key={index}
attempt={a}
i={index}
setSelectedAttempt={setSelectedAttempt}
setIsList={setIsList}
/>
))}
</div>
) : (
<AttemptView attempt={selectedAttempt} setIsList={setIsList} />
)}
</div>
);
};
7 changes: 7 additions & 0 deletions Frontend/src/Components/Matching/MatchPopup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import './MatchPopup.css'; // Ensure this path is correct
import { LoadPopup } from './LoadPopup';
import { getUserId } from '../../User/UserState';
import { useNavigate } from 'react-router-dom';
import { NotSuccessOutput } from './NotSuccessOutput';

const MatchPopup = ({ isOpen, isClose }) => {
const [goToLoadPopup, setGoToLoadPopup] = useState(false);
const [showSuccessOutput, setShowSuccessOutput] = useState(false);
const [collaboratorId, setCollaboratorId] = useState(null);
const navigate = useNavigate();
const [showNotSuccessOutput, setShowNotSuccessOutput] = useState(false);

// States for the matching criteria
const [chosenDifficulty, setChosenDifficulty] = useState("None");
Expand Down Expand Up @@ -56,12 +58,15 @@ const MatchPopup = ({ isOpen, isClose }) => {
} else {
console.log("No match found");
setShowSuccessOutput(false); // SuccessOutput popup does not show
setShowNotSuccessOutput(true); // Triggers the NotSuccessOutput popup

}
// setGoToLoadPopup(false); // Close the loading popup
})
.catch(error => {
console.error("Error finding a match: ", error);
setGoToLoadPopup(false); // Close the loading popup
setShowNotSuccessOutput(true); // Triggers the NotSuccessOutput popup

});
};
Expand Down Expand Up @@ -120,6 +125,8 @@ const MatchPopup = ({ isOpen, isClose }) => {

{showSuccessOutput &&
<SuccessOutput isOpen={true} isClose={() => setShowSuccessOutput(false)} />}
{showNotSuccessOutput &&
<NotSuccessOutput isOpen={true} isClose={() => setShowNotSuccessOutput(false)} />}
</div>
);
}
Expand Down
22 changes: 22 additions & 0 deletions Frontend/src/Components/Matching/NotSuccessOutput.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import './Output.css';
import error_icon from '../Assets/cross.png';

export const NotSuccessOutput = ({ isOpen, isClose }) => {
if (isOpen) {
return (
<div className="output-overlay">
<div className="output-popup">
<h3>Not Match Found!</h3>
<img src={error_icon} className="image" alt="Error icon" />
<p>Try Again!</p>
<div>
<button onClick={isClose} className="complete-matching">Okay</button>
</div>
</div>
</div>
);
}
return null;
};

38 changes: 19 additions & 19 deletions Frontend/src/History/HistoryServiceAPI.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import {
getUserAttemptsFromHistoryDatabase,
addUserAttemptToHistoryDatabase,
getUserAttemptsFromHistoryDatabase,
addUserAttemptToHistoryDatabase,
} from "./HistoryServiceController";

async function getUserAttempts(userId) {
const result = await getUserAttemptsFromHistoryDatabase(userId);
const result = await getUserAttemptsFromHistoryDatabase(userId);
console.log(result);
if (result !== null && result.status === 200) {
return result.data.result;
}

if (result !== null && result.status === 200) {
return result.data.result;
}

return null;
return null;
}

async function addUserAttempt(userId1, userId2, sessionId, questionId) {
const result = await addUserAttemptToHistoryDatabase(
userId1,
userId2,
sessionId,
questionId
);
const result = await addUserAttemptToHistoryDatabase(
userId1,
userId2,
sessionId,
questionId
);

if (result !== null && result.status === 201) {
return true;
}
if (result !== null && result.status === 201) {
return true;
}

return false;
return false;
}

export { addUserAttempt, getUserAttempts };
export {addUserAttempt, getUserAttempts};
Loading

0 comments on commit 2b40d14

Please sign in to comment.