Files
classroomio/apps/dashboard/src/lib/mocks/index.ts
T

92 lines
2.2 KiB
TypeScript
Raw Normal View History

import { calculateTotalPoints, shuffleOptions } from './utils';
import type { ExerciseTemplate } from '$lib/utils/types';
2023-10-04 08:18:33 +01:00
2024-09-23 01:21:05 +01:00
// Rule to add new template
// The key and value must match
// The template should have both `{VALUE}_IDS` and `{VALUE}_TEMPLATES`
2023-10-04 08:18:33 +01:00
export const TAGS = {
HTML: 'HTML',
CSS: 'CSS',
JS: 'JS',
Typescript: 'Typescript',
ReactJS: 'ReactJS',
VueJS: 'VueJS',
NodeJS: 'NodeJS',
Python: 'Python',
PHP: 'PHP',
GIT: 'GIT'
2023-10-04 08:18:33 +01:00
};
2024-09-23 01:21:05 +01:00
export interface GeneratedTemplates {
[key: string]: LangTemplate[];
}
interface LangTemplate {
id: string;
title: string;
questions: number;
points: number;
data: ExerciseTemplate;
}
async function generateTemplateForTag(
keys: { [key: string]: string },
2024-09-23 01:21:05 +01:00
template: {
[x: string]: () => ExerciseTemplate;
}
) {
2024-09-23 01:21:05 +01:00
const templates: LangTemplate[] = [];
for (const key in keys) {
const id = keys[key];
2024-09-23 01:21:05 +01:00
const data = await template[id]();
data.questionnaire.questions = data.questionnaire.questions.map((q) => {
q.options = shuffleOptions(q.options);
return q;
});
2024-09-23 01:21:05 +01:00
templates.push({
id,
2024-09-23 01:21:05 +01:00
title: data.title,
questions: data.questionnaire.questions.length,
points: calculateTotalPoints(data),
data: data
});
}
2024-09-23 01:21:05 +01:00
return templates;
}
2024-09-23 01:21:05 +01:00
export const getAllTemplates = async (): Promise<GeneratedTemplates> => {
const templateModules = {
HTML: await import('./html'),
CSS: await import('./css'),
JS: await import('./js'),
Typescript: await import('./typescript'),
ReactJS: await import('./react'),
VueJS: await import('./vue'),
NodeJS: await import('./node'),
Python: await import('./python'),
PHP: await import('./php'),
GIT: await import('./git')
};
const templates: GeneratedTemplates = {};
for (const tag in templateModules) {
try {
const module = templateModules[tag];
const ids = module[`${tag.toUpperCase()}_IDS`];
const templatesData = module[`${tag.toUpperCase()}_TEMPLATES`];
if (!templatesData) {
throw 'Templates not found';
}
templates[tag] = await generateTemplateForTag(ids, templatesData);
} catch (error) {
console.error('error finding template', error);
}
}
return templates;
2023-10-04 08:18:33 +01:00
};