Skip to content

Commit

Permalink
ES Linting Fixes for Quiz Attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtan2000 committed Aug 26, 2024
1 parent 533f37d commit 0061ab5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
29 changes: 19 additions & 10 deletions app/(main)/quiz/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const QuizPage: React.FC = () => {

const [quiz, setQuiz] = useState<Quiz.ApiResponse | null>(null);
const [isQuizOngoing, setIsQuizOngoing] = useState<boolean>(true);
const [selectedOptions, setSelectedOptions] = useState<{ [key: number]: number | null }>({});
const [selectedOptions, setSelectedOptions] = useState<{ [key: number]: number | 0 }>({});
const [currentQuestionIndex, setCurrentQuestionIndex] = useState<number>(0);
const [isDisabled, setIsDisabled] = useState(false);

Expand All @@ -50,9 +50,9 @@ const QuizPage: React.FC = () => {
}
setQuiz(responseData);
// Initialize selectedOptions with keys for each mcq.id set to null
const initialSelectedOptions: { [key: number]: number | null } = {};
const initialSelectedOptions: { [key: number]: number | 0 } = {};
responseData.mcqs.forEach((mcq) => {
initialSelectedOptions[mcq.id] = null;
initialSelectedOptions[mcq.id] = 0;
});
setSelectedOptions(initialSelectedOptions);
} catch (error) {
Expand Down Expand Up @@ -96,9 +96,9 @@ const QuizPage: React.FC = () => {
status: 'abandoned' // Assuming 'status' is a field in your quiz object
}));
// Reset selected options
const initialSelectedOptions: { [key: number]: number | null } = {};
const initialSelectedOptions: { [key: number]: number | 0 } = {};
quiz.mcqs.forEach((mcq: any) => {
initialSelectedOptions[mcq.id] = null;
initialSelectedOptions[mcq.id] = 0;
});
setSelectedOptions(initialSelectedOptions);
} catch (error) {
Expand Down Expand Up @@ -348,16 +348,25 @@ const QuizPage: React.FC = () => {
<div className="flex flex-wrap gap-2">
{currentQuestionIndex < quiz.mcqs.length - 1 ? (
<Button label="Next Question" onClick={() => {
submitAttempt(quiz.id, currentQuestionIndex, selectedOptions[currentQuestion.id]);
handleNextQuestion();
if (quiz.id !== undefined && selectedOptions[currentQuestion.id] !== null) {
submitAttempt(quiz.id, currentQuestionIndex + 1, selectedOptions[currentQuestion.id]);
handleNextQuestion();
} else {
console.error("Quiz ID is undefined or selected option is null");
}
console.log(`Next question: ${selectedOptions[currentQuestion.id]}`);
}}></Button>
) : (
<>
<div>
<p>No more questions in this quiz. Do you want to submit?</p>
<Button label="Submit Quiz" onClick={() =>
submitAttempt(quiz.id, currentQuestionIndex, selectedOptions[currentQuestion.id])
}></Button>
<Button label="Submit Quiz" onClick={() => {
if (quiz.id !== undefined && selectedOptions[currentQuestion.id] !== null) {
submitAttempt(quiz.id, currentQuestionIndex + 1, selectedOptions[currentQuestion.id]);
} else {
console.error("Quiz ID is undefined or selected option is null");
}
}}></Button>
</div>
</>
)}
Expand Down
4 changes: 2 additions & 2 deletions service/QuizService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const QuizService = {
}
},

getQuizCompleted: async (): Promise<Quiz.ApiResponse> => {
getQuizCompleted: async (): Promise<Quiz.CompletedResponse> => {
try {
console.log('Fetching data from API...');
const response = await fetch(
Expand All @@ -65,7 +65,7 @@ export const QuizService = {
credentials: "include"
}
);
const responseData: Quiz.ApiResponse = await response.json();
const responseData: Quiz.CompletedResponse = await response.json();
console.log('Data fetched successfully:', responseData);
return responseData;
} catch (error) {
Expand Down

0 comments on commit 0061ab5

Please sign in to comment.