-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7be65ca
commit 7af86d2
Showing
8 changed files
with
105 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
{ | ||
"lesson-1": [] | ||
} | ||
["lesson-1"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,71 @@ | ||
const rootDirectory = location.origin + "/lesson"; | ||
const rootIndex = rootDirectory + "/index.json"; | ||
const rootIndex = `${rootDirectory}/index.json`; | ||
|
||
export class LessonMetadata { | ||
export class Lesson { | ||
constructor( | ||
private readonly _id: string, | ||
public readonly name: string, | ||
public readonly require: LessonMetadata[], | ||
public readonly description: string, | ||
public readonly questions: Map<string, Question>, | ||
) {} | ||
|
||
async load(): Promise<LessonData> { | ||
const rawLesson = await LessonMetadata.rawLessonDetails(this._id); | ||
|
||
return new LessonData( | ||
rawLesson.title, | ||
rawLesson.description, | ||
rawLesson.summary, | ||
LessonMetadata.parseQuestions(rawLesson.questions), | ||
); | ||
} | ||
|
||
protected static async rawLessonDetails(id: string): Promise<any> { | ||
return await (await fetch(rootDirectory + "/" + id + "/index.json")).json(); | ||
} | ||
|
||
protected static parseQuestions(questions: any): Question[] { | ||
let parsedQuestions: Question[] = []; | ||
|
||
for (let question in questions) { | ||
parsedQuestions.push(Question.parse(questions[question])); | ||
} | ||
|
||
return parsedQuestions; | ||
} | ||
} | ||
|
||
export class LessonData { | ||
export class Question { | ||
constructor( | ||
public readonly title: string, | ||
public readonly description: string, | ||
public readonly questions: Question[], | ||
public readonly information: Information[], | ||
public readonly question: string, | ||
public readonly answers: string[], | ||
public readonly correct: number, | ||
) {} | ||
|
||
static parse(question: any): Question { | ||
return new Question( | ||
Information.parseList(question.lesson), | ||
question.question, | ||
question.answers, | ||
question.correct, | ||
); | ||
} | ||
} | ||
|
||
export class LessonContent { | ||
export class Information { | ||
constructor( | ||
public readonly type: "title" | "paragraph" | "image", | ||
public readonly content: string, | ||
) {} | ||
|
||
static parse(content: any): LessonContent { | ||
static parse(content: any): Information { | ||
if (content.type === "image") { | ||
return new LessonContent( | ||
return new Information( | ||
content.type, | ||
rootDirectory + "/image/" + content.content, | ||
`${rootDirectory}/image/${content.content}`, | ||
); | ||
} | ||
|
||
return new LessonContent(content.type, content.content); | ||
return new Information(content.type, content.content); | ||
} | ||
|
||
static parseList(content: any): LessonContent[] { | ||
let parsedContent: LessonContent[] = []; | ||
static parseList(content: any): Information[] { | ||
let parsedContent: Information[] = []; | ||
|
||
for (let c of content) { | ||
parsedContent.push(LessonContent.parse(c)); | ||
parsedContent.push(Information.parse(c)); | ||
} | ||
|
||
return parsedContent; | ||
} | ||
} | ||
|
||
export class Question { | ||
constructor( | ||
public readonly lesson: LessonContent[], | ||
public readonly question: string, | ||
public readonly answers: string[], | ||
private readonly _indexCorrect: number, | ||
) {} | ||
|
||
get correct(): string { | ||
return this.answers[this._indexCorrect]; | ||
} | ||
|
||
static parse(question: any): Question { | ||
return new Question( | ||
LessonContent.parseList(question.lesson), | ||
question.question, | ||
question.answers, | ||
question.correct, | ||
); | ||
} | ||
} | ||
|
||
export async function avaibleLessons(): Promise<Map<string, LessonMetadata>> { | ||
const lessonIndex = await (await fetch(rootIndex)).json(); | ||
|
||
let lessons = new Map<string, LessonMetadata>(); | ||
|
||
for (let rawLesson of lessonIndex) { | ||
let require = parseLessonRequeriments(rawLesson.require, lessons); | ||
|
||
lessons.set( | ||
rawLesson.id, | ||
new LessonMetadata(rawLesson.id, rawLesson.name, require), | ||
); | ||
} | ||
|
||
return lessons; | ||
export async function avaibleLessons(): Promise<string[]> { | ||
return await (await fetch(rootIndex)).json(); | ||
} | ||
|
||
function parseLessonRequeriments( | ||
require: string[], | ||
lessons: Map<string, LessonMetadata>, | ||
): LessonMetadata[] { | ||
let requeriments: LessonMetadata[] = []; | ||
export async function loadLesson(id: string): Promise<Lesson> { | ||
const lesson = await (await fetch(`${rootDirectory}/${id}.json`)).json(); | ||
lesson.questions = new Map(Object.entries(lesson.questions)); | ||
|
||
for (let req of require) { | ||
for (let [id, lesson] of lessons) { | ||
if (req === id) { | ||
requeriments.push(lesson); | ||
} | ||
} | ||
for (let [_, question] of lesson.questions) { | ||
question.information = Information.parseList(question.information); | ||
} | ||
|
||
return requeriments; | ||
return lesson; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.