Refactor learningPathStore to use setup syntax
This commit is contained in:
parent
26b986b732
commit
a0f9e0dfee
|
|
@ -38,9 +38,9 @@ onMounted(async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const learningPath = computed(() => {
|
const learningPath = computed(() => {
|
||||||
if (userStore.loggedIn && learningPathStore.learningPaths.size > 0) {
|
if (userStore.loggedIn && learningPathStore.state.learningPaths.size > 0) {
|
||||||
const learningPathKey = `${props.courseSlug}-lp-${userStore.id}`;
|
const learningPathKey = `${props.courseSlug}-lp-${userStore.id}`;
|
||||||
return learningPathStore.learningPaths.get(learningPathKey);
|
return learningPathStore.state.learningPaths.get(learningPathKey);
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,10 @@ import { useUserStore } from "@/stores/user";
|
||||||
import type { CourseCompletion } from "@/types";
|
import type { CourseCompletion } from "@/types";
|
||||||
import cloneDeep from "lodash/cloneDeep";
|
import cloneDeep from "lodash/cloneDeep";
|
||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
|
import { computed, reactive } from "vue";
|
||||||
|
|
||||||
export type LearningPathStoreState = {
|
export type LearningPathStoreState = {
|
||||||
learningPaths: Map<string, LearningPath>;
|
learningPaths: Map<string, LearningPath>;
|
||||||
|
|
||||||
page: "INDEX" | "OVERVIEW";
|
page: "INDEX" | "OVERVIEW";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -22,83 +22,80 @@ function getLearningPathKey(
|
||||||
return `${slug}-${userId}`;
|
return `${slug}-${userId}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useLearningPathStore = defineStore({
|
export const useLearningPathStore = defineStore("learningPath", () => {
|
||||||
id: "learningPath",
|
const state: LearningPathStoreState = reactive({
|
||||||
state: () => {
|
learningPaths: new Map<LearningPathKey, LearningPath>(),
|
||||||
return {
|
page: "INDEX",
|
||||||
learningPaths: new Map<LearningPathKey, LearningPath>(),
|
});
|
||||||
page: "INDEX",
|
|
||||||
loading: false,
|
|
||||||
} as LearningPathStoreState;
|
|
||||||
},
|
|
||||||
getters: {
|
|
||||||
learningPathForUser: (state) => {
|
|
||||||
return (courseSlug: string, userId: string | number | undefined) => {
|
|
||||||
if (state.learningPaths.size > 0) {
|
|
||||||
const learningPathKey = getLearningPathKey(`${courseSlug}-lp`, userId);
|
|
||||||
return state.learningPaths.get(learningPathKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
const learningPathForUser = computed(() => {
|
||||||
};
|
return (courseSlug: string, userId: string | number | undefined) => {
|
||||||
},
|
if (state.learningPaths.size > 0) {
|
||||||
},
|
const learningPathKey = getLearningPathKey(`${courseSlug}-lp`, userId);
|
||||||
actions: {
|
return state.learningPaths.get(learningPathKey);
|
||||||
async loadCompletionData(
|
|
||||||
courseSlug: string,
|
|
||||||
userId: number | undefined = undefined
|
|
||||||
) {
|
|
||||||
const completionStore = useCompletionStore();
|
|
||||||
|
|
||||||
let completionData: CourseCompletion[] = [];
|
|
||||||
if (userId) {
|
|
||||||
const courseSessionsStore = useCourseSessionsStore();
|
|
||||||
const courseSession = courseSessionsStore.courseSessionForCourse(courseSlug);
|
|
||||||
if (courseSession) {
|
|
||||||
completionData = await completionStore.loadCompletionData(
|
|
||||||
courseSession.id,
|
|
||||||
userId
|
|
||||||
);
|
|
||||||
return completionData;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return undefined;
|
||||||
},
|
};
|
||||||
async loadLearningPath(
|
});
|
||||||
slug: string,
|
|
||||||
userId: number | undefined = undefined,
|
async function loadCompletionData(
|
||||||
reload = false,
|
courseSlug: string,
|
||||||
fail = true
|
userId: number | undefined = undefined
|
||||||
) {
|
) {
|
||||||
if (!userId) {
|
const completionStore = useCompletionStore();
|
||||||
const userStore = useUserStore();
|
|
||||||
userId = userStore.id;
|
let completionData: CourseCompletion[] = [];
|
||||||
|
if (userId) {
|
||||||
|
const courseSessionsStore = useCourseSessionsStore();
|
||||||
|
const courseSession = courseSessionsStore.courseSessionForCourse(courseSlug);
|
||||||
|
if (courseSession) {
|
||||||
|
completionData = await completionStore.loadCompletionData(
|
||||||
|
courseSession.id,
|
||||||
|
userId
|
||||||
|
);
|
||||||
|
return completionData;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const key = getLearningPathKey(slug, userId);
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
if (this.learningPaths.has(key) && !reload) {
|
async function loadLearningPath(
|
||||||
return this.learningPaths.get(key);
|
slug: string,
|
||||||
}
|
userId: number | undefined = undefined,
|
||||||
|
reload = false,
|
||||||
|
fail = true
|
||||||
|
) {
|
||||||
|
if (!userId) {
|
||||||
|
const userStore = useUserStore();
|
||||||
|
userId = userStore.id;
|
||||||
|
}
|
||||||
|
|
||||||
const learningPathData = await itGetCached(`/api/course/page/${slug}/`);
|
const key = getLearningPathKey(slug, userId);
|
||||||
if (!learningPathData && fail) {
|
|
||||||
throw `No learning path found with: ${slug}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const completionData = await this.loadCompletionData(
|
if (state.learningPaths.has(key) && !reload) {
|
||||||
learningPathData.course.slug,
|
return state.learningPaths.get(key);
|
||||||
userId
|
}
|
||||||
);
|
|
||||||
|
|
||||||
const learningPath = LearningPath.fromJson(
|
const learningPathData = await itGetCached(`/api/course/page/${slug}/`);
|
||||||
cloneDeep(learningPathData),
|
if (!learningPathData && fail) {
|
||||||
completionData,
|
throw `No learning path found with: ${slug}`;
|
||||||
userId
|
}
|
||||||
);
|
|
||||||
this.learningPaths.set(key, learningPath);
|
const completionData = await loadCompletionData(
|
||||||
return learningPath;
|
learningPathData.course.slug,
|
||||||
},
|
userId
|
||||||
},
|
);
|
||||||
|
|
||||||
|
const learningPath = LearningPath.fromJson(
|
||||||
|
cloneDeep(learningPathData),
|
||||||
|
completionData,
|
||||||
|
userId
|
||||||
|
);
|
||||||
|
state.learningPaths.set(key, learningPath);
|
||||||
|
return learningPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { state, learningPathForUser, loadCompletionData, loadLearningPath };
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue