vbv/client/src/stores/learningPath.ts

122 lines
3.9 KiB
TypeScript

import * as log from 'loglevel';
import {defineStore} from 'pinia'
import * as _ from 'lodash';
import type {LearningPath, Topic} from '@/types'
import {itGet} from '@/fetchHelpers';
import {Circle} from '@/services/circle';
import learningPathDiagram from "@/components/circle/LearningPathDiagram.vue";
export type LearningPathStoreState = {
learningPath: LearningPath | undefined;
}
function getLastCompleted(completionData: any) {
return _.filter(_.orderBy(completionData, ['updated_at'], 'desc'), 'completed')[0]
}
function getFirstLearningContent(lastCopleted, learningPathData) {
const circles = _.filter(learningPathData.children, {'type': 'learnpath.Circle'})
let currentCircle = Circle.fromJson(circles[0])
const currentLearningUnit = currentCircle.flatChildren[0]
let currentLearningSequence = currentLearningUnit.parentLearningSequence
return [currentCircle, currentLearningSequence, currentLearningUnit]
}
function getNextLearningContent(lastCopleted, learningPathData) {
let currentCircle, currentLearningSequence, currentLearningUnit
currentLearningUnit = getFirstLearningContent(lastCopleted, learningPathData)
if (lastCopleted) {
const circles = _.filter(learningPathData.children, {'type': 'learnpath.Circle'})
_.forEach(circles, circle => {
_.forEach(Circle.fromJson(circle).learningSequences, learningSequence => {
currentLearningSequence = learningSequence
_.forEach(learningSequence.learningUnits, learningUnit => {
_.forEach(learningUnit.learningContents, content => {
console.log(lastCopleted, content)
if (lastCopleted.page_key === content.translation_key) {
currentCircle = Circle.fromJson(circle)
currentLearningSequence = learningSequence
currentLearningUnit = content
}
})
})
})
})
currentLearningUnit = [currentCircle, currentLearningSequence, currentLearningUnit]
}
return currentLearningUnit
}
export const useLearningPathStore = defineStore({
id: 'learningPath',
state: () => {
return {
learningPath: undefined,
} as LearningPathStoreState;
},
getters: {},
actions: {
async loadLearningPath(slug: string, reload = false) {
if (this.learningPath && !reload) {
return this.learningPath;
}
try {
const learningPathData = await itGet(`/learnpath/api/page/${slug}/`);
const completionData = await itGet(`/api/completion/learning_path/${learningPathData.translation_key}/`);
this.learningPath = learningPathData;
if (this.learningPath) {
this.learningPath.lastCompleted = getLastCompleted(completionData)
const nextLearningContent = getNextLearningContent(this.learningPath.lastCompleted, learningPathData)
console.log('nextLearningContent', nextLearningContent)
this.learningPath.nextCircle = nextLearningContent[0]
this.learningPath.nextLearningSequence = nextLearningContent[1]
this.learningPath.nextLearningUnit = nextLearningContent[2]
this.learningPath.topics = [];
this.learningPath.circles = [];
let topic: Topic | undefined;
this.learningPath.children.forEach((page) => {
if (page.type === 'learnpath.Topic') {
if (topic) {
this.learningPath.topics.push(topic);
}
topic = Object.assign(page, {circles: []});
}
if (page.type === 'learnpath.Circle') {
const circle = Circle.fromJson(page);
circle.parseCompletionData(completionData);
if (topic) {
topic.circles.push(circle);
}
this.learningPath.circles.push(circle);
}
})
this.learningPath.topics.push(topic);
}
return this.learningPath;
} catch (error) {
log.error(error);
return error
}
},
}
})