Skip to content

Commit

Permalink
Updated error msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtan2000 committed Sep 23, 2024
1 parent 36e4d95 commit 73b1535
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
55 changes: 30 additions & 25 deletions app/(main)/quiz/history/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,37 +77,40 @@ const QuizHistory: React.FC = () => {
try {
const responseData = await QuizService.getQuizCompleted();
if (responseData.message === 'Quiz not found') {
setError('Quiz not found');
setError('No history to display');
setLoading(false);
return;
}
setQuiz(responseData);

// Process the data to calculate counts and extract unique topics and skills
const processedData = responseData.quizzes.map((quiz: Quiz.QuizTaken) => {
const topicsMap = new Map<number, Topic>();
const skillsMap = new Map<number, Skill>();

quiz.mcqs.forEach((mcq: Quiz.Mcq) => {
mcq.topics.forEach((topic) => topicsMap.set(topic.id, topic));
mcq.skills.forEach((skill) => skillsMap.set(skill.id, skill));
});

return {
attemptOn: quiz.mcqs[0].attemptOn,
id: quiz.id,
topicsCount: topicsMap.size,
skillsCount: skillsMap.size,
topics: Array.from(topicsMap.values()),
skills: Array.from(skillsMap.values()),
points: quiz.points,
mcqsCount: quiz.mcqs.length
};
}).filter(quiz => quiz.attemptOn && !isNaN(new Date(quiz.attemptOn).getTime())).sort((a, b) => new Date(b.attemptOn).getTime() - new Date(a.attemptOn).getTime()); // Sort by attemptOn in descending order
const processedData = responseData.quizzes
.map((quiz: Quiz.QuizTaken) => {
const topicsMap = new Map<number, Topic>();
const skillsMap = new Map<number, Skill>();

quiz.mcqs.forEach((mcq: Quiz.Mcq) => {
mcq.topics.forEach((topic) => topicsMap.set(topic.id, topic));
mcq.skills.forEach((skill) => skillsMap.set(skill.id, skill));
});

return {
attemptOn: quiz.mcqs[0].attemptOn,
id: quiz.id,
topicsCount: topicsMap.size,
skillsCount: skillsMap.size,
topics: Array.from(topicsMap.values()),
skills: Array.from(skillsMap.values()),
points: quiz.points,
mcqsCount: quiz.mcqs.length
};
})
.filter((quiz) => quiz.attemptOn && !isNaN(new Date(quiz.attemptOn).getTime()))
.sort((a, b) => new Date(b.attemptOn).getTime() - new Date(a.attemptOn).getTime()); // Sort by attemptOn in descending order

setProcessedQuizzes(processedData);
} catch (error) {
setError('Error fetching data');
setError('No history to display');
} finally {
setLoading(false);
}
Expand All @@ -127,13 +130,15 @@ const QuizHistory: React.FC = () => {
const dateBodyTemplate = (rowData: ProcessedQuiz) => {
const date = new Date(rowData.attemptOn);

const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${getDayOfWeek(date.getDay())} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}`;
const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${getDayOfWeek(date.getDay())} ${String(date.getHours()).padStart(2, '0')}:${String(
date.getMinutes()
).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}`;

return <Button key={rowData.attemptOn.toString()} label={formattedDate} onClick={() => handleQuizClick(rowData.id)} />;
};

const getDayOfWeek = (dayIndex: number) => {
const daysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
return daysOfWeek[dayIndex];
};

Expand All @@ -142,7 +147,7 @@ const QuizHistory: React.FC = () => {
}

if (error) {
return <div>Error: {error}</div>;
return <div className="card">{error}</div>;
}

return (
Expand Down
2 changes: 1 addition & 1 deletion app/(main)/quiz/results/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ const ResultsPage: React.FC = () => {
);
};

export default ResultsPage;
export default ResultsPage;
4 changes: 1 addition & 3 deletions app/(main)/test/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ const QuizPage: React.FC = () => {
<div>
{/* <b>{selectedQuestionCount ? selectedQuestionCount : generatedQuestionCount} question(s) will be generated.</b> */}
<div>
<div className="col-12 md:col-6 mb-5">
Your tutor hasn&apos;t created a test yet.
</div>
<div className="col-12 md:col-6 mb-5">Your tutor hasn&apos;t created a test yet.</div>
<div className="col-12 md:col-6 mb-5"></div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion components/ResultsTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function ResultsTopComponent({ onQuestionClick, currentQuestionIn
const fetchData = async () => {
try {
if (!quizId) {
setError('Quiz ID not found, please select a Quiz from History');
setError('Please select a Quiz from History');
setLoading(false);
return;
}
Expand Down

0 comments on commit 73b1535

Please sign in to comment.