Skip to content

Commit

Permalink
Merge pull request #703 from bounswe/MOBILE-702
Browse files Browse the repository at this point in the history
feat(mobile): Quiz Recommendation According to Solved Quiz Level
  • Loading branch information
YavizGuldalf authored Nov 25, 2024
2 parents 7186b01 + c2896d6 commit 4c1a8ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
5 changes: 3 additions & 2 deletions mobile/bulingo/app/(tabs)/quizzes/quizCreationInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Keyboard, StyleSheet, Text, TextInput, View, TouchableWithoutFeedback,
import { router, useLocalSearchParams } from 'expo-router';

const QuizCreationInfo = () => {
const [question, setQuestion] = useState('Pasta');
const [question, setQuestion] = useState('');
const [answers, setAnswers] = useState(['', '', '', '']);
const [showButtonIndex, setShowButtonIndex] = useState(null); // Track which tile should show the button
const [newAnswer, setNewAnswer] = useState('');
Expand Down Expand Up @@ -123,13 +123,14 @@ const QuizCreationInfo = () => {
styles.answerBox,
correctAnswerIndex === answerIndex
? styles.correctAnswer
: null, // Highlight correct answer
: null,
selectedAnswerIndex === answerIndex
? styles.selectedAnswer
: null,
]}
onPress={() => handleAnswerClick(answerIndex)}
onLongPress={() => handleLongPress(answerIndex)}
key={answerIndex}
>
<Text style={styles.answerText}
numberOfLines={1}
Expand Down
33 changes: 15 additions & 18 deletions mobile/bulingo/app/(tabs)/quizzes/quizResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const QuizResults = () => {

useEffect(() => {
const fetchQuizResult = async () => {
setLoading(true);
try {
const response = await TokenManager.authenticatedFetch(resultUrl, {
method: 'GET',
Expand All @@ -60,7 +61,6 @@ const QuizResults = () => {
} catch (err: any) {
setError(err.message || 'Unknown error occurred');
} finally {
setLoading(false);
}
};

Expand All @@ -71,44 +71,41 @@ const QuizResults = () => {
useEffect(() => {
const fetchQuizzes = async () => {
try {
const response = await TokenManager.authenticatedFetch(`/feed/quiz/`, {
const response = await TokenManager.authenticatedFetch(`/quiz/recommend/` + quizId, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});

const data = await response.json();
if (response.ok) {
const formattedResults = data.map((quiz: any) => ({
const quiz = await response.json();
if (response.ok && quiz) {
const formattedResults = {
id: Number(quiz.id),
title: quiz.title,
description: quiz.description,
author: quiz.author.username,
level: quiz.level,
likes: quiz.like_count,
liked: quiz.is_liked,
}));
let randomInt = 0
let retryCount = 3;

while(formattedResults[randomInt].id === Number(quizId) && retryCount > 0) {
randomInt = Math.floor(Math.random() * formattedResults.length);
retryCount--;
};

setRecommendedQuiz(formattedResults[randomInt]);
setRecommendedQuiz(formattedResults);
setError(null);
} else {
setError('Failed to fetch quizzes. Please try again. Error: ' + data.message || JSON.stringify(data));
setRecommendedQuiz(null);
}
} catch (error: any) {
setError('An error occurred while fetching quizzes. Please try again. Error: ' + (error.message || 'Unknown error'));
}
finally {
setLoading(false);
}
};

fetchQuizzes();
}, []);

fetchQuizzes();
}, []);


return (
<View style={styles.container}>
Expand Down

0 comments on commit 4c1a8ba

Please sign in to comment.