Recode `useLearningPath` to use graphql query programatically

This commit is contained in:
Daniel Egger 2023-10-12 21:25:50 +02:00
parent 8544898bbf
commit 61dfdfda9d
2 changed files with 33 additions and 42 deletions

View File

@ -1,3 +1,4 @@
import { graphqlClient } from "@/graphql/client";
import { COURSE_SESSION_DETAIL_QUERY, LEARNING_PATH_QUERY } from "@/graphql/queries"; import { COURSE_SESSION_DETAIL_QUERY, LEARNING_PATH_QUERY } from "@/graphql/queries";
import { circleFlatChildren, circleFlatLearningContents } from "@/services/circle"; import { circleFlatChildren, circleFlatLearningContents } from "@/services/circle";
import { useCompletionStore } from "@/stores/completion"; import { useCompletionStore } from "@/stores/completion";
@ -137,16 +138,16 @@ export function flatCircles(learningPath: LearningPathType) {
return learningPath.topics.flatMap((t) => t.circles); return learningPath.topics.flatMap((t) => t.circles);
} }
export function useLearningPathQuery(courseSlug: string) { export function useLearningPath(courseSlug: string) {
const queryResult = useQuery({ const learningPath = ref<LearningPathType | undefined>(undefined);
query: LEARNING_PATH_QUERY,
variables: {
slug: `${courseSlug}-lp`,
},
});
const learningPath = computed(() => { // urql.useQuery is not meant to be used programmatically, so we use graphqlClient.query instead
return queryResult.data.value?.learning_path as LearningPathType | undefined; const resultPromise = graphqlClient
.query(LEARNING_PATH_QUERY, { slug: `${courseSlug}-lp` })
.toPromise();
resultPromise.then((result) => {
learningPath.value = result.data?.learning_path as LearningPathType;
}); });
const circles = computed(() => { const circles = computed(() => {
@ -162,20 +163,7 @@ export function useLearningPathQuery(courseSlug: string) {
}); });
} }
const dataLoaded = ref(false); return { resultPromise, learningPath, circles, findCircle };
function waitForData() {
return new Promise((resolve) => {
watchEffect(() => {
if (queryResult.data.value) {
dataLoaded.value = true;
resolve(queryResult.data.value);
}
});
});
}
return { ...queryResult, waitForData, learningPath, circles, findCircle };
} }
export function useLearningPathWithCompletion( export function useLearningPathWithCompletion(
@ -193,28 +181,20 @@ export function useLearningPathWithCompletion(
courseSessionId = useCurrentCourseSession().value.id; courseSessionId = useCurrentCourseSession().value.id;
} }
const lpQueryResult = useLearningPathQuery(courseSlug); const learningPathResult = useLearningPath(courseSlug);
const completionStore = useCompletionStore(); const completionStore = useCompletionStore();
const nextLearningContent = ref<LearningContentWithCompletion | null>(null); const nextLearningContent = ref<LearningContentWithCompletion | null>(null);
function updateCompletionData() { function updateCompletionData() {
if (userId && courseSessionId) { if (userId && courseSessionId) {
completionStore return completionStore.loadCourseSessionCompletionData(courseSessionId, userId);
.loadCourseSessionCompletionData(courseSessionId, userId)
.then(_parseCompletionData);
} }
return Promise.resolve([]);
} }
watchEffect(() => {
if (lpQueryResult.data.value) {
// load completion data when learning path data is loaded
updateCompletionData();
}
});
function _parseCompletionData(completionData: CourseCompletion[]) { function _parseCompletionData(completionData: CourseCompletion[]) {
if (lpQueryResult.circles.value) { if (learningPathResult.circles.value) {
lpQueryResult.circles.value.forEach((circle) => { learningPathResult.circles.value.forEach((circle) => {
circleFlatChildren(circle).forEach((lc) => { circleFlatChildren(circle).forEach((lc) => {
const pageIndex = completionData.findIndex((e) => { const pageIndex = completionData.findIndex((e) => {
return e.page_id === lc.id; return e.page_id === lc.id;
@ -229,9 +209,9 @@ export function useLearningPathWithCompletion(
} }
// FIXME calculate nextLearningContent // FIXME calculate nextLearningContent
if (lpQueryResult.circles.value?.length) { if (learningPathResult.circles.value?.length) {
nextLearningContent.value = circleFlatLearningContents( nextLearningContent.value = circleFlatLearningContents(
lpQueryResult.circles.value[0] learningPathResult.circles.value[0]
)[0]; )[0];
} }
} }
@ -251,8 +231,19 @@ export function useLearningPathWithCompletion(
} }
} }
async function _start() {
Promise.all([learningPathResult.resultPromise, updateCompletionData()]).then(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
([_queryResults, completionData]) => {
_parseCompletionData(completionData);
}
);
}
_start();
return { return {
...lpQueryResult, ...learningPathResult,
updateCompletionData, updateCompletionData,
markCompletion, markCompletion,
nextLearningContent, nextLearningContent,

View File

@ -1,4 +1,4 @@
import { useLearningPathQuery } from "@/composables"; import { useLearningPath } from "@/composables";
import { useUserStore } from "@/stores/user"; import { useUserStore } from "@/stores/user";
import type { CircleLight, CourseSessionUser, ExpertSessionUser } from "@/types"; import type { CircleLight, CourseSessionUser, ExpertSessionUser } from "@/types";
import log from "loglevel"; import log from "loglevel";
@ -59,8 +59,8 @@ async function courseCircles(
const userStore = useUserStore(); const userStore = useUserStore();
// Return all circles from learning path for admin users // Return all circles from learning path for admin users
if (userStore.is_superuser) { if (userStore.is_superuser) {
const lpQueryResult = useLearningPathQuery(courseSlug); const lpQueryResult = useLearningPath(courseSlug);
await lpQueryResult.waitForData(); await lpQueryResult.resultPromise;
return (lpQueryResult.circles.value ?? []).map((c) => { return (lpQueryResult.circles.value ?? []).map((c) => {
return { return {