Skip to content

Commit

Permalink
Merge branch 'webapp_interface' of https://github.com/Arquisoft/wiq_es6c
Browse files Browse the repository at this point in the history
 into webapp_interface
  • Loading branch information
alegarman2002 committed Apr 29, 2024
2 parents f35edf3 + 9f7d507 commit 5bd48c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
22 changes: 6 additions & 16 deletions webapp/src/components/AddUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,20 @@ const AddUser = () => {
setError('Las contraseñas no coinciden.');
} else {
try {
const isAvailable = await checkUsernameAvailability(username, password);
if (isAvailable != false) {
try {
await axios.post(`${apiEndpoint}/login`, { username, password });
setError('Usuario ya registrado.');
return;
setOpenSnackbar(false);
} catch (error) {
await axios.post(`${apiEndpoint}/adduser`, { username, password });
setOpenSnackbar(true);
}

await axios.post(`${apiEndpoint}/adduser`, { username, password });
setOpenSnackbar(true);
} catch (error) {
setError(error.response.data.error);
}
}
};

const checkUsernameAvailability = async (username, password) => {
try {
const response = await axios.post(`${apiEndpoint}/check-username`, { username });
return response.data;
} catch (error) {
console.error("Error al comprobar la disponibilidad del nombre de usuario:", error);
return false;
}
}

const handleCloseSnackbar = () => {
setOpenSnackbar(false);
};
Expand Down
10 changes: 9 additions & 1 deletion webapp/src/components/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('AddUser component', () => {
const usernameInput = screen.getByLabelText(/Usuario/i);
const passwordInput = screen.getAllByLabelText(/Contraseña/i)[0];
const confirmPasswordInput = screen.getByLabelText(/Repetir contraseña/i);
const addUserButton = document.getElementsByClassName('inner')[0]
const addUserButton = document.getElementsByClassName('inner')[0];

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);
Expand All @@ -38,6 +38,14 @@ describe('AddUser component', () => {
await waitFor(() => {
expect(screen.getByText(/Usuario añadido correctamente/i)).toBeInTheDocument();
});

/*
fireEvent.click(addUserButton);
// Wait for the Snackbar to be open
await waitFor(() => {
expect(screen.getByText(/Usuario ya registrado./i)).toBeInTheDocument();
});
*/
});

it('try to add user but not introduce name', async () => {
Expand Down
49 changes: 31 additions & 18 deletions webapp/src/components/game/Calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ let points = 0;
const apiEndpoint = process.env.REACT_APP_API_ENDPOINT|| 'http://localhost:8000';
let answeredQuestions = [];

let savedGame = false;

const Calculator = () => {

//let questionIndex = -1
Expand All @@ -25,8 +27,6 @@ const Calculator = () => {
const [remTime, setRemTime] = useState(0);
const [totalTime, setTotalTime] = useState(0);

let id = generateGameId;

if(questions.length === 0)
generateQuestion();

Expand All @@ -38,12 +38,11 @@ const Calculator = () => {
setTotalTime(totalTime + progress/10)
console.log("Despues", totalTime)

//TODO: ALMACENAR JUEGO
gameStore();

return 0;
}
const diff = 5;
const diff = 0.5;
return load? Math.min(progress + diff, 100) : progress;
});
}, 400);
Expand All @@ -61,8 +60,12 @@ const Calculator = () => {
console.log(totalTime)
var avgtime = totalTime/answeredQuestions.length
console.log(avgtime)
const response = await axios.post(`${apiEndpoint}/storeGame`, { id, username, points, questions: answeredQuestions, avgtime});
console.log(response)
const id = await generateGameId();
if(!savedGame){
savedGame = true;
const response = await axios.post(`${apiEndpoint}/storeGame`, { id, username, points, questions: answeredQuestions, avgtime});
console.log(response)
}
} catch (error) {
console.error(error)
} finally {
Expand Down Expand Up @@ -111,11 +114,11 @@ const Calculator = () => {
}
}

shuffleArray(option);
let shuffled = shuffleArray(option);
questions.push(
{
q: `${num1} ${operator} ${num2}`,
options: option,
options: shuffled,
correctAnswer: correctAnswer
}
);
Expand All @@ -124,22 +127,22 @@ const Calculator = () => {

//CAMBIAR ESTO EN FUNCIÓN DE CÓMO QUERAMOS QUE SEA EL JUEGO
const handleOptionClick = async (selectedAnswer) => {
load = false;
const numberAnswer = questions[questionIndex].options.indexOf(questions[questionIndex].correctAnswer);
const choiceNumber = questions[questionIndex].options.indexOf(selectedAnswer);

console.log(numberAnswer)
console.log(choiceNumber)

//console.log(numberAnswer)
const botonCorrecta = document.getElementById('option-' + numberAnswer);
let botonIncorrecta = null;
botonCorrecta.style.backgroundColor = 'green';
answeredQuestions.push({
pregunta: 'a',
respuesta_correcta: 'a',
respuestas_incorrectas: [
'a',
'a',
'a'
]
});
/*title: allQuestions[currentQuestionIndex].question,
answers: allQuestions[currentQuestionIndex].options,
ansIndex: indexAnswers*/
storeQuestion(questions[questionIndex].q, questions[questionIndex].options, [choiceNumber, numberAnswer]);
if (selectedAnswer !== questions[questionIndex].correctAnswer) {

botonIncorrecta = document.getElementById('option-' + questions[questionIndex].options.indexOf(selectedAnswer));
botonIncorrecta.style.backgroundColor = 'red';
} else {
Expand All @@ -156,7 +159,17 @@ const Calculator = () => {
if(botonIncorrecta != null){
botonIncorrecta.style.backgroundColor = previousBackgroundColor;
}

load = true;
};

function storeQuestion(title, answers, ansIndex){
answeredQuestions.push({
title: title,
answers: answers,
ansIndex: ansIndex
});
}


return (
Expand Down

0 comments on commit 5bd48c4

Please sign in to comment.