177 lines
5.4 KiB
TypeScript
177 lines
5.4 KiB
TypeScript
import * as log from "loglevel";
|
|
|
|
import { defineStore } from "pinia";
|
|
|
|
import { itPost } from "@/fetchHelpers";
|
|
import type { Circle } from "@/services/circle";
|
|
import { useLearningPathStore } from "@/stores/learningPath";
|
|
import type {
|
|
CourseCompletionStatus,
|
|
LearningContent,
|
|
LearningUnit,
|
|
LearningUnitPerformanceCriteria,
|
|
} from "@/types";
|
|
|
|
export type CircleStoreState = {
|
|
circle: Circle | undefined;
|
|
page: "INDEX" | "OVERVIEW";
|
|
};
|
|
|
|
function createLearningUnitHash(learningUnit: LearningUnit | undefined) {
|
|
const luSlug = learningUnit?.slug;
|
|
const circleSlug = learningUnit?.parentCircle?.slug;
|
|
if (luSlug && circleSlug) {
|
|
return "#" + luSlug.replace(`${circleSlug}-`, "");
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
export const useCircleStore = defineStore({
|
|
id: "circle",
|
|
state: () => {
|
|
return {
|
|
circle: undefined,
|
|
page: "INDEX",
|
|
} as CircleStoreState;
|
|
},
|
|
getters: {},
|
|
actions: {
|
|
async loadCircle(learningPathSlug: string, circleSlug: string): Promise<Circle> {
|
|
this.circle = undefined;
|
|
const learningPathStore = useLearningPathStore();
|
|
await learningPathStore.loadLearningPath(learningPathSlug);
|
|
if (learningPathStore.learningPath) {
|
|
this.circle = learningPathStore.learningPath.circles.find((circle) => {
|
|
return circle.slug.endsWith(circleSlug);
|
|
});
|
|
}
|
|
|
|
if (!this.circle) {
|
|
throw `No circle found with slug: ${circleSlug}`;
|
|
}
|
|
|
|
return this.circle;
|
|
},
|
|
async loadLearningContent(
|
|
learningPathSlug: string,
|
|
circleSlug: string,
|
|
learningContentSlug: string
|
|
) {
|
|
const circle = await this.loadCircle(learningPathSlug, circleSlug);
|
|
const result = circle.flatLearningContents.find((learningContent) => {
|
|
return learningContent.slug.endsWith(learningContentSlug);
|
|
});
|
|
|
|
if (!result) {
|
|
throw `No learning content found with slug: ${learningContentSlug}`;
|
|
}
|
|
|
|
return result;
|
|
},
|
|
async loadSelfEvaluation(
|
|
learningPathSlug: string,
|
|
circleSlug: string,
|
|
learningUnitSlug: string
|
|
) {
|
|
const circle = await this.loadCircle(learningPathSlug, circleSlug);
|
|
const learningUnit = circle.flatLearningUnits.find((child) => {
|
|
return child.slug.endsWith(learningUnitSlug);
|
|
});
|
|
|
|
if (!learningUnit) {
|
|
throw `No self evaluation found with slug: ${learningUnitSlug}`;
|
|
}
|
|
|
|
return learningUnit;
|
|
},
|
|
async markCompletion(
|
|
page: LearningContent | LearningUnitPerformanceCriteria,
|
|
completion_status: CourseCompletionStatus = "success"
|
|
) {
|
|
try {
|
|
page.completion_status = completion_status;
|
|
const completionData = await itPost("/api/course/completion/mark/", {
|
|
page_key: page.translation_key,
|
|
completion_status: page.completion_status,
|
|
});
|
|
if (this.circle) {
|
|
this.circle.parseCompletionData(completionData);
|
|
}
|
|
} catch (error) {
|
|
log.error(error);
|
|
return error;
|
|
}
|
|
},
|
|
openLearningContent(learningContent: LearningContent) {
|
|
this.router.push({
|
|
path: learningContent.frontend_url,
|
|
});
|
|
},
|
|
closeLearningContent(learningContent: LearningContent) {
|
|
this.router.push({
|
|
path: `${this.circle?.frontend_url}`,
|
|
hash: createLearningUnitHash(learningContent.parentLearningUnit),
|
|
});
|
|
},
|
|
openSelfEvaluation(learningUnit: LearningUnit) {
|
|
this.router.push({
|
|
path: learningUnit.frontend_url,
|
|
});
|
|
},
|
|
closeSelfEvaluation(learningUnit: LearningUnit) {
|
|
this.router.push({
|
|
path: `${this.circle?.frontend_url}`,
|
|
hash: createLearningUnitHash(learningUnit),
|
|
});
|
|
},
|
|
calcSelfEvaluationStatus(learningUnit: LearningUnit) {
|
|
if (learningUnit.children.length > 0) {
|
|
if (learningUnit.children.every((q) => q.completion_status === "success")) {
|
|
return true;
|
|
}
|
|
if (learningUnit.children.some((q) => q.completion_status === "fail")) {
|
|
return false;
|
|
}
|
|
}
|
|
return undefined;
|
|
},
|
|
continueFromLearningContent(currentLearningContent: LearningContent) {
|
|
if (currentLearningContent) {
|
|
this.markCompletion(currentLearningContent, "success");
|
|
|
|
const nextLearningContent = currentLearningContent.nextLearningContent;
|
|
const currentParent = currentLearningContent.parentLearningUnit;
|
|
const nextParent = nextLearningContent?.parentLearningUnit;
|
|
|
|
if (
|
|
currentParent &&
|
|
currentParent.id &&
|
|
currentParent.id !== nextParent?.id &&
|
|
currentParent.children.length > 0
|
|
) {
|
|
// go to self evaluation
|
|
this.openSelfEvaluation(currentParent);
|
|
} else if (currentLearningContent.nextLearningContent) {
|
|
if (
|
|
currentLearningContent.parentLearningUnit &&
|
|
currentLearningContent.parentLearningUnit.id ===
|
|
nextLearningContent?.parentLearningUnit?.id
|
|
) {
|
|
this.openLearningContent(currentLearningContent.nextLearningContent);
|
|
} else {
|
|
this.closeLearningContent(currentLearningContent);
|
|
}
|
|
} else {
|
|
this.closeLearningContent(currentLearningContent);
|
|
}
|
|
} else {
|
|
log.error("currentLearningContent is undefined");
|
|
}
|
|
},
|
|
continueFromSelfEvaluation(learningUnit: LearningUnit) {
|
|
this.closeSelfEvaluation(learningUnit);
|
|
},
|
|
},
|
|
});
|