vbv/client/src/stores/circle.ts

166 lines
5.6 KiB
TypeScript

import * as log from 'loglevel'
import { defineStore } from 'pinia'
import type { CourseCompletionStatus, LearningContent, LearningUnit, LearningUnitQuestion } from '@/types'
import type { Circle } from '@/services/circle'
import { itPost } from '@/fetchHelpers'
import { useLearningPathStore } from '@/stores/learningPath'
export type CircleStoreState = {
circle: Circle | undefined
page: 'INDEX' | 'OVERVIEW'
}
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 | LearningUnitQuestion, 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) {
const shortSlug = learningContent.slug.replace(`${this.circle?.slug}-lc-`, '');
this.router.push({
path: `${this.circle?.getUrl()}/${shortSlug}`,
});
},
closeLearningContent() {
this.router.push({
path: `${this.circle?.getUrl()}`
});
},
openSelfEvaluation(learningUnit: LearningUnit) {
const shortSlug = learningUnit.slug.replace(`${this.circle?.slug}-lu-`, '');
this.router.push({
path: `${this.circle?.getUrl()}/evaluate/${shortSlug}`,
});
},
closeSelfEvaluation() {
this.router.push({
path: `${this.circle?.getUrl()}`
});
},
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);
this.closeLearningContent();
} else if (currentLearningContent.nextLearningContent) {
if (
currentLearningContent.parentLearningSequence &&
currentLearningContent.parentLearningSequence.id === nextLearningContent?.parentLearningSequence?.id
) {
this.openLearningContent(currentLearningContent.nextLearningContent);
} else {
this.closeLearningContent();
}
} else {
this.closeLearningContent();
}
} else {
log.error('currentLearningContent is undefined');
}
},
continueFromSelfEvaluation() {
this.closeSelfEvaluation()
// if (this.currentSelfEvaluation) {
// const nextContent = this.currentSelfEvaluation.learningContents[this.currentSelfEvaluation.learningContents.length - 1].nextLearningContent;
//
// if (nextContent) {
// if (this.currentSelfEvaluation?.parentLearningSequence?.id === nextContent?.parentLearningSequence?.id) {
// this.openLearningContent(nextContent);
// } else {
// this.closeSelfEvaluation();
// }
// } else {
// this.closeSelfEvaluation();
// }
// } else {
// log.error('currentSelfEvaluation is undefined');
// }
}
}
})