vbv/client/src/stores/learningPath.ts

64 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;
}
export const useLearningPathStore = defineStore({
id: 'learningPath',
state: () => {
return {
learningPath: undefined,
} as LearningPathStoreState;
},
getters: {
},
actions: {
async loadLearningPath(slug: string) {
try {
const learningPathData = await itGet(`/learnpath/api/learningpath/${slug}/`);
const completionData = await itGet(`/api/completion/learning_path/${learningPathData.translation_key}/`);
this.learningPath = learningPathData;
this.learningPath.topics = [];
const emptyTopic: Topic = {
id: 0,
title: '',
slug: '',
type: 'learnpath.Topic',
translation_key: '',
is_visible: false,
circles: []
}
let topic = Object.assign({}, emptyTopic);
this.learningPath.children.forEach((page) => {
if (page.type === 'learnpath.Topic') {
if (topic.id !== 0) {
this.learningPath.topics.push(topic);
}
topic = Object.assign({}, emptyTopic, page);
}
if (page.type === 'learnpath.Circle') {
const circle = Circle.fromJson(page);
circle.parseCompletionData(completionData);
topic.circles.push(circle);
}
this.learningPath.topics.push(topic);
})
console.log(this.learningPath);
return this.learningPath;
} catch (error) {
log.error(error);
return error
}
},
}
})