Skip to content

Commit

Permalink
Merge pull request #53 from Arquisoft/Marina
Browse files Browse the repository at this point in the history
Pull Request para el Issue: #26 (Temporizador)
  • Loading branch information
UO288559 authored Mar 8, 2024
2 parents d0c1dbf + 4266269 commit 633c80d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 114 deletions.
114 changes: 0 additions & 114 deletions README.md

This file was deleted.

29 changes: 29 additions & 0 deletions timerservice/timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});

app.get('/timer', async (req, res) => {

const time = parseInt(req.params.time);
startTimer(time,() =>{
res.send("Time up");
})

});

function startTimer(time,func){
this.timer = setTimeout(()=>{
func();
},time)
}

var server = app.listen(port, () => {
console.log(`Questions Generation Service listening at http://localhost:${port}`);
});

module.exports = server
25 changes: 25 additions & 0 deletions webapp/src/components/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,40 @@ const Game = () => {
const [error, setError] = useState('');
const [selectedOption, setSelectedOption] = useState('');
const [openSnackbar, setOpenSnackbar] = useState(false);
const [elapsedTime,setElapsedTime] = useState(30);

useEffect(() => {
getQuestion();
}, []);

useEffect(() => {

const timerId = setTimeout(()=>{
setElapsedTime(time => time - 1);
},1000);

if(elapsedTime<=0){
timeUp();
}

return () => {
clearTimeout(timerId);
}
}, [elapsedTime]);

function timeUp(){
alert("Time up");
getQuestion();
}

const getQuestion = async () => {
try {
const response = await axios.get(`${apiEndpoint}/generateQuestion`, { });
setQuestion(response.data.responseQuestion);
setOptions(response.data.responseOptions);
setCorrectOption(response.data.responseCorrectOption);
setOpenSnackbar(true);
setElapsedTime(30);
} catch (error) {
setError(error.response.data.error);
}
Expand All @@ -43,6 +65,9 @@ const Game = () => {

return (
<Container component="main" maxWidth="xs" sx={{ marginTop: 4 }}>
<Typography variant="body1" sx={{ textAlign: 'center' }}>
Tiempo restante: {elapsedTime} segundos
</Typography>
<Typography component="h1" variant="h5" sx={{ textAlign: 'center' }}>
{question}
</Typography>
Expand Down

0 comments on commit 633c80d

Please sign in to comment.