Skip to content

Commit be7e933

Browse files
committed
submit inline questions in a sequence
This avoids a race condition where the API could not save multiple answers that were submitted at the same time for anonymous users. This is because, for anonymous users, when saving an answer, the API creates a lock using the user's ID, see: https://github.com/isaacphysics/isaac-api/blob/ 51296605cef5b532f6f6b90d9a9575001172d4a6/src/main/java/uk/ac/cam/cl/ dtg/isaac/quiz/PgQuestionAttempts.java#L92.
1 parent f5d95cf commit be7e933

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/app/components/content/IsaacInlineRegion.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ export const useInlineRegionPart = (pageQuestions: AppQuestionDTO[] | undefined)
8484
};
8585
};
8686

87-
export const submitInlineRegion = (inlineContext: ContextType<typeof InlineContext>, currentGameboard: GameboardDTO | undefined, currentUser: any, pageQuestions: AppQuestionDTO[] | undefined, dispatch: any, hidingAttempts : boolean) => {
87+
export const submitInlineRegion = async (inlineContext: ContextType<typeof InlineContext>, currentGameboard: GameboardDTO | undefined, currentUser: any, pageQuestions: AppQuestionDTO[] | undefined, dispatch: any, hidingAttempts : boolean) => {
8888
if (inlineContext && inlineContext.docId && pageQuestions) {
8989
inlineContext.setSubmitting(true);
9090
const inlineQuestions = pageQuestions.filter(q => inlineContext.docId && q.id?.startsWith(inlineContext.docId) && q.id.includes("|inline-question:"));
9191
// we submit all modified answers, and those with undefined values. we must submit this latter group to get a validation response at the same time as the other answers
9292
const modifiedInlineQuestions = inlineQuestions.filter(q => (q.id && inlineContext.modifiedQuestionIds.includes(q.id)) || (q.currentAttempt?.value === undefined && (q.bestAttempt === undefined || hidingAttempts)));
9393
for (const inlineQuestion of modifiedInlineQuestions) {
94-
submitCurrentAttempt(
94+
await submitCurrentAttempt(
9595
{currentAttempt: inlineQuestion.currentAttempt},
9696
inlineQuestion.id as string, inlineQuestion.type as string, currentGameboard, currentUser, dispatch, inlineContext
9797
);

src/app/services/questions.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,15 @@ export function useCurrentQuestionAttempt<T extends ChoiceDTO>(questionId: strin
182182
};
183183
}
184184

185-
export const submitCurrentAttempt = (questionPart: AppQuestionDTO | undefined, docId: string, questionType: string, currentGameboard: GameboardDTO | undefined, currentUser: any, dispatch: any, inlineContext?: ContextType<typeof InlineContext>) => {
185+
export const submitCurrentAttempt = (questionPart: AppQuestionDTO | undefined, docId: string, questionType: string, currentGameboard: GameboardDTO | undefined, currentUser: any, dispatch: any, inlineContext?: ContextType<typeof InlineContext>): Promise<void> => {
186186
if (questionPart?.currentAttempt) {
187187
// Notify Plausible that at least one question attempt has taken place today
188188
if (persistence.load(KEY.INITIAL_DAILY_QUESTION_ATTEMPT_TIME) == null || !wasTodayUTC(persistence.load(KEY.INITIAL_DAILY_QUESTION_ATTEMPT_TIME))) {
189189
persistence.save(KEY.INITIAL_DAILY_QUESTION_ATTEMPT_TIME, new Date().toString());
190190
trackEvent("question_attempted");
191191
}
192192

193-
dispatch(attemptQuestion(docId, questionPart?.currentAttempt, questionType, currentGameboard?.id, inlineContext));
193+
const attempt = dispatch(attemptQuestion(docId, questionPart?.currentAttempt, questionType, currentGameboard?.id, inlineContext));
194194

195195
if (isLoggedIn(currentUser) && isNotPartiallyLoggedIn(currentUser) && currentGameboard?.id && !currentGameboard.savedToCurrentUser) {
196196
dispatch(saveGameboard({
@@ -199,7 +199,10 @@ export const submitCurrentAttempt = (questionPart: AppQuestionDTO | undefined, d
199199
redirectOnSuccess: false
200200
}));
201201
}
202+
203+
return attempt;
202204
}
205+
return Promise.resolve();
203206
};
204207

205208
export const getMostRecentCorrectAttemptDate = (questions: AppQuestionDTO[] | undefined) => {

0 commit comments

Comments
 (0)