Skip to content

Question related to pending and getting next question. #1

Description

@cxw0520

The original code in startNextQuestion seems to only allow the user to play the quiz once, when I tried to start the same quiz again, the const nextQuestion value in pendingQustionRoute returns undefined.
I've tried to change the code so that the quizzes can be played mulitiple times, but always face some issue. I've change the startNextQuestion file to this:

import GameStore from '../store/GameStore';
import QuestionStore from '../store/QuestionStore';

const startNextQuestion = async (gameId, currentQuestionIndex = 0) => {
  const gameStore = new GameStore();
  const questionStore = new QuestionStore();

  let game;
  try {
    game = await gameStore.get(gameId);
  } catch (error) {
    // Handle error
  }

  if (game) {
    gameStore.update(gameId, { state: 'pendingQuestion' });

    if ('currentQuestion' in game) {
      return questionStore.get(game.currentQuestion);
    } else {
      const questionArray = await questionStore.list({
        gameId,
        from: currentQuestionIndex,
        limit: 1,
      });
      const question = questionArray[0];

      if (question) {
        gameStore.update(gameId, { currentQuestionId: question.id });
      } else {
        gameStore.update(gameId, { currentQuestionIndex: 0 });
        currentQuestionIndex = 0;
        question = await startNextQuestion(gameId, currentQuestionIndex);
      }

      return question;
    }
  } else {
    return;
  }
};

export default startNextQuestion;

But it only works normally when there's only 1 question in the quiz, when there's multiple quesiton in the quiz, it will only start the last question of the quiz and end after that.
Is there anything I am doing wrong here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions