Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #87. Manejo de imagenes como respuesta #89

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions preguntas/generatorservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const templateData = require('./data/data.json');
const app = express();
const port = 8003;

//Constante para el tamaño de imagenes
const widthSize = 140;

// Connect to MongoDB
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/questiondb';

Expand Down Expand Up @@ -76,6 +79,14 @@ app.get('/generate-question', async (req, res) => {
const wrongAnswer2 = formattedResults[randomIndex2];
const wrongAnswer3 = formattedResults[randomIndex3];

//Cambio de URL. Imagen redimensionada
if (correctAnswer.rLabel.includes('upload.wikimedia.org')) {
correctAnswer.rLabel = getThumbUrl(correctAnswer.rLabel, widthSize);
wrongAnswer1.rLabel = getThumbUrl(wrongAnswer1.rLabel, widthSize);
wrongAnswer2.rLabel = getThumbUrl(wrongAnswer2.rLabel, widthSize);
wrongAnswer3.rLabel = getThumbUrl(wrongAnswer3.rLabel, widthSize);
}

//Creación de array desordenado con todas las respuestas
const allAnswersSorted = [correctAnswer.rLabel, wrongAnswer1.rLabel, wrongAnswer2.rLabel, wrongAnswer3.rLabel];
const allAnswers = shuffleArray(allAnswersSorted);
Expand Down Expand Up @@ -120,4 +131,18 @@ function shuffleArray(array) {
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}

function getThumbUrl(originalUrl, width) {
// Verifica si la URL es de Wikipedia
if (!originalUrl.includes('upload.wikimedia.org/wikipedia/commons/')) {
return "URL no válida";
}

// Reemplaza la parte de la URL para obtener la versión miniatura
let filename = originalUrl.split('/').pop();
let thumbnailUrl = originalUrl.replace(
"/commons/", "/commons/thumb/").replace(".svg", ".png") + "/"+ width +"px-" + filename;

return thumbnailUrl;
}
7 changes: 7 additions & 0 deletions webapp/src/components/QuizGame.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@

p {
color: #666;
}

button{
background: fixed;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
38 changes: 28 additions & 10 deletions webapp/src/components/QuizGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ const QuizGame = () => {
setAnswerSelected(true);
setButtonsDisabled(true);


if(isCorrect) {
toast.success('¡Respuesta correcta!', {
position: toast.POSITION.TOP_CENTER,
onClose: () => setIsToastVisible(false)
});
console.log(answeredQuestions)
} else {
toast.error('Respuesta incorrecta', {
position: toast.POSITION.TOP_CENTER,
Expand Down Expand Up @@ -110,14 +108,24 @@ const QuizGame = () => {
disabled={buttonsDisabled}
onClick={() => handleAnswer(answer)}
style={{
backgroundColor: answerSelected && selectedAnswer && selectedAnswer.answer === answer
? selectedAnswer.isCorrect
backgroundColor: answerSelected && selectedAnswer
? answer === currentQuestion.correctAnswer
? 'green'
: 'red'
: '#EE0E51'
: '#EE0E51',

color: answer.startsWith('http') && answerSelected && selectedAnswer
? answer === currentQuestion.correctAnswer
? 'green'
: 'red'
: undefined
}}
>
{answer}
{
answer.startsWith('http') ?
<img src={answer} alt="imagen" style={{ height: '110px' }} />
: answer
}
</Button>
)
))}
Expand All @@ -130,14 +138,24 @@ const QuizGame = () => {
disabled={buttonsDisabled}
onClick={() => handleAnswer(answer)}
style={{
backgroundColor: answerSelected && selectedAnswer && selectedAnswer.answer === answer
? selectedAnswer.isCorrect
backgroundColor: answerSelected && selectedAnswer
? answer === currentQuestion.correctAnswer
? 'green'
: 'red'
: '#EE0E51'
: '#EE0E51',

color: answer.startsWith('http') && answerSelected && selectedAnswer
? answer === currentQuestion.correctAnswer
? 'green'
: 'red'
: undefined
}}
>
{answer}
{
answer.startsWith('http') ?
<img src={answer} alt="imagen" style={{ height: '110px' }} />
: answer
}
</Button>
)
))}
Expand Down
Loading