import * as log from 'loglevel' import { defineStore } from 'pinia' import type { LearningContent, LearningUnit, LearningUnitQuestion } from '@/types' import type { Circle } from '@/services/circle' import { itPost } from '@/fetchHelpers' import { useAppStore } from '@/stores/app' import { useLearningPathStore } from '@/stores/learningPath' export type CircleStoreState = { circle: Circle | undefined currentLearningContent: LearningContent | undefined currentSelfEvaluation: LearningUnit | undefined page: 'INDEX' | 'OVERVIEW' | 'LEARNING_CONTENT' | 'SELF_EVALUATION' } export const useCircleStore = defineStore({ id: 'circle', state: () => { return { circle: undefined, currentLearningContent: undefined, currentSelfEvaluation: undefined, page: 'INDEX', } as CircleStoreState; }, getters: { }, actions: { async loadCircle(learningPathSlug: string, circleSlug: string): Promise { 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); if (circle) { this.currentLearningContent = circle.flatLearningContents.find((learningContent) => { return learningContent.slug.endsWith(learningContentSlug); }); } if (!this.currentLearningContent) { throw `No learning content found with slug: ${learningContentSlug}`; } return this.currentLearningContent; }, async markCompletion(page: LearningContent | LearningUnitQuestion, flag = true) { try { page.completed = flag; const completionData = await itPost('/api/completion/circle/mark/', { page_key: page.translation_key, completed: page.completed, }); if (this.circle) { this.circle.parseCompletionData(completionData); } } catch (error) { log.error(error); return error } }, openLearningContent(learningContent: LearningContent) { this.currentLearningContent = 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) { this.page = 'SELF_EVALUATION'; const appStore = useAppStore(); appStore.showMainNavigationBar = false; this.currentSelfEvaluation = learningUnit; }, closeSelfEvaluation() { this.currentSelfEvaluation = undefined; const appStore = useAppStore(); appStore.showMainNavigationBar = true; this.page = 'INDEX'; }, calcSelfEvaluationStatus(learningUnit: LearningUnit) { if (learningUnit.children.length > 0) { if (learningUnit.children.every((q) => q.completed)) { return true; } if (learningUnit.children.some((q) => q.completed !== undefined)) { return false; } } return undefined; }, continueFromLearningContent() { if (this.currentLearningContent) { this.markCompletion(this.currentLearningContent, true); const nextLearningContent = this.currentLearningContent.nextLearningContent; const currentParent = this.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 (this.currentLearningContent.nextLearningContent) { if ( this.currentLearningContent.parentLearningSequence && this.currentLearningContent.parentLearningSequence.id === nextLearningContent?.parentLearningSequence?.id ) { this.openLearningContent(this.currentLearningContent.nextLearningContent); } else { this.closeLearningContent(); } } else { this.closeLearningContent(); } } else { log.error('currentLearningContent is undefined'); } }, continueFromSelfEvaluation() { 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'); } } } })