66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import * as log from 'loglevel';
|
|
|
|
import {defineStore} from 'pinia'
|
|
|
|
import type {LearningPath, Topic} from '@/types'
|
|
import {itGet} from '@/fetchHelpers';
|
|
import {Circle} from '@/services/circle';
|
|
|
|
export type LearningPathStoreState = {
|
|
learningPath: LearningPath | undefined;
|
|
}
|
|
|
|
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.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
|
|
}
|
|
},
|
|
}
|
|
})
|