Skip to content

Commit

Permalink
Fixed userhistory webapp error display
Browse files Browse the repository at this point in the history
  • Loading branch information
dononitram committed Apr 17, 2024
1 parent 7df11cb commit 5552389
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gatewayservice/routes/jordiRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (app, axios) => {
.post(`${historyService}/add/${saveId}`,
{last, statement, options, answer: iAnswer, correct: iCorrect, time, points})
.then(() => res.json({answer: question.answer, points}))
.catch(() => next({ error: "An error occured while fetching the categories"}));
.catch(() => res.json({answer: question.answer, points, error: "An error occured while saving the answer"}));
})
.catch(() => next({error: "An error occured while fetching the answer"}));
});
Expand Down
30 changes: 20 additions & 10 deletions webapp/src/views/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,14 @@ const Game = () => {
const [totalTime, setTotalTime] = useState(0)
const { category} = useParams()

const handleNextQuestion = () => {
const handleNextQuestion = async () => {
clearInterval(interval.current)
interval.current = setInterval(() => {
setTime((prevTimer) => prevTimer - 1);
}, 1000);

if(!saveId.current) await createSave()
if(!saveId.current) return
axios
.post("/game/answer", {
token: getUser().token,
Expand All @@ -177,6 +180,9 @@ const Game = () => {
options: questions[current].options
})
.then(response => {
const { error } = response.data
setHistorialError(error)

setPoints(points + response.data.points)
const correctAnswer = response.data.answer

Expand All @@ -198,21 +204,25 @@ const Game = () => {
setAnswer(null)
}, 500)
})
.catch(e => setHistorialError({ error: e.response.data.error }))
.catch(e => setHistorialError(e.response.data.error))
}

const createSave = () => {
axios
.post("/history/create", {
token: getUser().token,
category: category,
userId: getUser().userId
})
.then(response => saveId.current = response.data.id)
.catch(err => setHistorialError(err.response.data.error))
}

useEffect( () => {
fetchQuestions(category, getUser().token)
.then(data => setQuestions(data))
.catch(err => setError({error: err.response.data.error, status: err.response.status}));
axios
.post("/history/create", {
token: getUser().token,
category: category,
userId: getUser().userId
})
.then(response => saveId.current = response.data.id)
.catch(err => setHistorialError({error: err.response.data.error, status: err.response.status}))
createSave()
//eslint-disable-next-line
}, []);

Expand Down

0 comments on commit 5552389

Please sign in to comment.