Load completion data for user

This commit is contained in:
Daniel Egger 2022-12-02 15:15:44 +01:00
parent 218756e3dc
commit c9ddefd82e
4 changed files with 32 additions and 18 deletions

View File

@ -1,5 +1,6 @@
import { itGetCached } from "@/fetchHelpers"; import { itGetCached } from "@/fetchHelpers";
import { useCompletionStore } from "@/stores/completion"; import { useCompletionStore } from "@/stores/completion";
import { useUserStore } from "@/stores/user";
import type { import type {
CircleLight, CircleLight,
CompetencePage, CompetencePage,
@ -109,10 +110,13 @@ export const useCompetenceStore = defineStore({
return this.competenceProfilePage; return this.competenceProfilePage;
}, },
async parseCompletionData() { async parseCompletionData() {
if (this.competenceProfilePage) { const userStore = useUserStore();
if (this.competenceProfilePage && userStore.loggedIn) {
const completionStore = useCompletionStore(); const completionStore = useCompletionStore();
const completionData = await completionStore.loadCompletionData( const completionData = await completionStore.loadCompletionData(
this.competenceProfilePage.course.id this.competenceProfilePage.course.id,
userStore.id
); );
if (completionData) { if (completionData) {

View File

@ -3,41 +3,46 @@ import type { BaseCourseWagtailPage, CourseCompletion } from "@/types";
import { defineStore } from "pinia"; import { defineStore } from "pinia";
export type CompletionStoreState = { export type CompletionStoreState = {
completionData: CourseCompletion[] | undefined; completionStore: Map<number, CourseCompletion[]>;
}; };
export const useCompletionStore = defineStore({ export const useCompletionStore = defineStore({
id: "completion", id: "completion",
state: () => { state: () => {
return { return {
completionData: undefined, completionStore: new Map(),
} as CompletionStoreState; } as CompletionStoreState;
}, },
getters: {}, getters: {},
actions: { actions: {
async loadCompletionData(courseId: number, reload = false) { async loadCompletionData(courseId: number, userId: number, reload = false) {
this.completionData = await itGetCached(`/api/course/completion/${courseId}/`, { const userCompletionData = await itGetCached(
reload: reload, `/api/course/completion/${courseId}/${userId}/`,
}); {
reload: reload,
}
);
if (!this.completionData) { if (userCompletionData === undefined) {
throw `No completionData found with: ${courseId}`; throw `No completionData found with: ${courseId}, ${userId}`;
} }
return this.completionData || []; this.completionStore.set(userId, userCompletionData);
return this.completionStore.get(userId) || [];
}, },
async markPage(page: BaseCourseWagtailPage) { async markPage(page: BaseCourseWagtailPage, userId: number) {
const completionData = await itPost("/api/course/completion/mark/", { const completionData = await itPost("/api/course/completion/mark/", {
page_key: page.translation_key, page_key: page.translation_key,
completion_status: page.completion_status, completion_status: page.completion_status,
}); });
if (completionData && completionData.length > 0) { if (completionData && completionData.length > 0) {
this.completionData = completionData; this.completionStore.set(userId, completionData);
bustItGetCache(`/api/course/completion/${completionData[0].course}/`); bustItGetCache(`/api/course/completion/${completionData[0].course}/`);
} }
return this.completionData || []; return this.completionStore.get(userId) || [];
}, },
}, },
}); });

View File

@ -1,6 +1,7 @@
import { itGetCached } from "@/fetchHelpers"; import { itGetCached } from "@/fetchHelpers";
import { LearningPath } from "@/services/learningPath"; import { LearningPath } from "@/services/learningPath";
import { useCompletionStore } from "@/stores/completion"; import { useCompletionStore } from "@/stores/completion";
import { useUserStore } from "@/stores/user";
import { defineStore } from "pinia"; import { defineStore } from "pinia";
export type LearningPathStoreState = { export type LearningPathStoreState = {
@ -35,10 +36,12 @@ export const useLearningPathStore = defineStore({
throw `No learning path found with: ${slug}`; throw `No learning path found with: ${slug}`;
} }
if (learningPathData) { const userStore = useUserStore();
if (learningPathData && userStore.loggedIn) {
lastSlug = slug; lastSlug = slug;
const completionData = await completionStore.loadCompletionData( const completionData = await completionStore.loadCompletionData(
learningPathData.course.id learningPathData.course.id,
userStore.id
); );
this.learningPath = LearningPath.fromJson(learningPathData, completionData); this.learningPath = LearningPath.fromJson(learningPathData, completionData);

View File

@ -9,6 +9,7 @@ const logoutRedirectUrl = import.meta.env.VITE_LOGOUT_REDIRECT;
// typed state https://stackoverflow.com/questions/71012513/when-using-pinia-and-typescript-how-do-you-use-an-action-to-set-the-state // typed state https://stackoverflow.com/questions/71012513/when-using-pinia-and-typescript-how-do-you-use-an-action-to-set-the-state
export type UserState = { export type UserState = {
id: number;
first_name: string; first_name: string;
last_name: string; last_name: string;
email: string; email: string;
@ -18,6 +19,7 @@ export type UserState = {
}; };
const initialUserState: UserState = { const initialUserState: UserState = {
id: 0,
email: "", email: "",
first_name: "", first_name: "",
last_name: "", last_name: "",
@ -56,9 +58,9 @@ export const useUserStore = defineStore({
} }
}, },
handleLogout() { handleLogout() {
itPost("/api/core/logout/", {}).then((data) => { Object.assign(this, initialUserState);
Object.assign(this, initialUserState);
itPost("/api/core/logout/", {}).then((data) => {
let redirectUrl; let redirectUrl;
if (logoutRedirectUrl !== "") { if (logoutRedirectUrl !== "") {