Skip to content

Commit

Permalink
Fixed random question nav (replace if no QID)
Browse files Browse the repository at this point in the history
  • Loading branch information
rimutaka committed Dec 3, 2024
1 parent b1eb9e5 commit ad60921
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions vue/src/views/QuestionView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ const topicName = computed(() => {
return (TOPICS.find((t) => t.id == route.query.topic))?.t;
});
// save the initial values to maintain state later because the topic and qid will change
const initialTopic = route.query.topic ? <string>route.query.topic : ANY_TOPIC;
const initialQid = route.query.qid ? <string>route.query.qid : undefined;
// this flag tells to not store pages that have no question ID in history because they redirect to a new
// random question catching the user in a loop
let replaceRouter = initialQid ? false : true;
// route.query.topic and .qid can potentially be an array, but it should not happen in this app,
// so it is safe to cast them into a string
Expand Down Expand Up @@ -63,8 +67,14 @@ watch(question, (newQuestion) => {
console.log("question topic/qid: ", topic.value, qid.value);
if (route.query.topic != topic.value || route.query.qid != qid.value) {
console.log("navigating to ", topic.value, qid.value);
router.push({ query: { topic: topic.value, qid: qid.value } });
if (replaceRouter) {
console.log("replace-navigating to ", topic.value, qid.value);
replaceRouter = false; // only replace the first time, subsequent calls should have qid
router.replace({ query: { topic: topic.value, qid: qid.value } });
} else {
console.log("push-navigating to ", topic.value, qid.value);
router.push({ query: { topic: topic.value, qid: qid.value } });
}
}
}
else {
Expand Down

0 comments on commit ad60921

Please sign in to comment.