Skip to content

Commit

Permalink
fix lesson and course hash to name
Browse files Browse the repository at this point in the history
  • Loading branch information
mfish33 committed Oct 25, 2020
1 parent ec39b27 commit fa46e33
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 76 deletions.
4 changes: 3 additions & 1 deletion src/app/app.component.ts
Expand Up @@ -2,14 +2,16 @@ import { Component, OnInit } from '@angular/core';
import { RouterHistoryService } from './core/services/router-history.service';
import { AuthService } from './core/services/auth.service';
import { ActivatedRoute } from "@angular/router";
import { ContentfulService } from './core/services/contentful.service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private history:RouterHistoryService, private auth:AuthService, private route: ActivatedRoute){}
// Pull in history and contentful services to get them to start at app launch
constructor(private history:RouterHistoryService, private auth:AuthService, private route: ActivatedRoute,private content:ContentfulService){}
title = 'MUNI';

ngOnInit() {
Expand Down
123 changes: 50 additions & 73 deletions src/app/core/services/contentful.service.ts
Expand Up @@ -4,8 +4,6 @@ import { Lesson, LessonLink, Course, CourseOrder, CourseOrdered, Profile, Profil
import { documentToHtmlString } from '@contentful/rich-text-html-renderer';
import { ProgressTrackerService } from './progress-tracker.service';

//TODO: Optimize calls by having a retrieve back end so it is not doing Object.entries on every call to async functions like getLesson


@Injectable({
providedIn: 'root'
Expand All @@ -26,34 +24,6 @@ export class ContentfulService {
this.ensureCourses()
}

public async ensureCourses(): Promise<void> {
if (Object.keys(this.content).length) {
return
}
try {
let res: contentful.EntryCollection<CourseOrder> = await this.client.getEntries({ content_type: 'courseOrder', include: 10 })
// put index of course onto course object
// Should only be one course order item at a time
let courseOrder = res.items[0]
this.content = courseOrder.fields.courses.reduce((acc, course, i) => {
course.fields.idx = i
acc[course.sys.id] = course.fields
return acc
}, {})

} catch (e) {
console.error(e)
}

}

public async getCoursesNoId(): Promise<Course[]> {
if (Object.keys(this.content).length == 0) {
await this.ensureCourses()
}
return Object.values(this.content)
}

public async getCoursesByOrder(): Promise<CourseOrdered[]> {
await this.ensureCourses()
return Object.entries(this.content).reduce((acc, val) => {
Expand All @@ -64,59 +34,40 @@ export class ContentfulService {
}, [])
}


public async getActiveCourses(): Promise<CourseOrdered[]> {
return (await this.getCoursesByOrder()).filter(c => c.isActive);
let courses = await this.getCoursesByOrder()
return courses.filter(c => c.isActive);
}

public getLessonLinks(courseId: string): LessonLink[] {
try {
return this.content[courseId].lessons
} catch (e) {
console.error('Could not find the specified lessons from givin Course', e)
}
}

public getNextLesson(courseId: string, lessonId: string): Lesson {
try {
let courseLength = this.content[courseId].lessons.length
let newIndex = this.content[courseId].lessons.map(lessonLink => lessonLink.fields.lesson.sys.id).indexOf(lessonId) + 1
return newIndex < courseLength ? this.content[courseId].lessons[newIndex].fields.lesson : null

} catch (e) {
console.error('Error in getNextArticle Function. Incorrect ids', e)
console.error('Could not find the specified lessons from given Course', e)
}
}

public getAllLessons(): Lesson[] {
return Object.values(this.content).map(course => course.lessons.map(lessonLink => lessonLink.fields.lesson)).reduce((allLessons, lessons) => {
for (let lesson of lessons) {
allLessons.push(lesson)
}
return allLessons
}, [])
}

public async getCourse(id: string): Promise<Course> {

public async getCourse(courseId: string): Promise<Course> {
await this.ensureCourses()
return this.content[id]
return this.content[courseId]
}

public async getLesson(cid: string, lid: string): Promise<Lesson> {
public async getLesson(courseId: string, lessonId: string): Promise<Lesson> {
await this.ensureCourses()
try {
let lessonIndex = this.content[cid].lessons.map(lessonLink => lessonLink.fields.lesson.sys.id).indexOf(lid)
if (lessonIndex == -1) {
throw new Error('Invalid Lesson Id')
}
let course = await this.getCourse(cid)
this.progress.hasVisited(course, lid)
return this.content[cid].lessons[lessonIndex].fields.lesson
} catch (e) {
console.error('Lesson Could not be found', e)
let lessonIndex = this.content[courseId].lessons.map(lessonLink => lessonLink.fields.lesson.fields.titleURLNormalized).indexOf(lessonId)
if (lessonIndex == -1) {
throw new Error('Invalid Lesson Id')
}
let course = await this.getCourse(courseId)
this.progress.hasVisited(course, lessonId)
return this.content[courseId].lessons[lessonIndex].fields.lesson
}

public getNextLesson(courseId: string, lessonId: string): Lesson {
let courseLength = this.content[courseId].lessons.length
let newIndex = this.content[courseId].lessons.map(lessonLink => lessonLink.fields.lesson.fields.titleURLNormalized).indexOf(lessonId) + 1
return newIndex < courseLength ? this.content[courseId].lessons[newIndex].fields.lesson : null
}


public convertRichText(doc): string {
return documentToHtmlString(doc)
Expand All @@ -130,6 +81,33 @@ export class ContentfulService {
return ''
}

public async getProfiles(): Promise<Profile[]> {
await this.ensureProfiles()
return this.aboutProfiles
}

private async ensureCourses(): Promise<void> {
if (Object.keys(this.content).length) {
return
}
try {
let res: contentful.EntryCollection<CourseOrder> = await this.client.getEntries({ content_type: 'courseOrder', include: 10 })
// put index of course onto course object
// Should only be one course order item at a time
let courseOrder = res.items[0]
this.content = courseOrder.fields.courses.reduce((acc, course, i) => {
course.fields.idx = i
course.fields.lessons.map(lesson => {
lesson.fields.lesson.fields.titleURLNormalized = this.normalizeURLNames(lesson.fields.lesson.fields.titleText)
})
acc[this.normalizeURLNames(course.fields.courseTitle)] = course.fields
return acc
}, {})
} catch (e) {
console.error('error fetching educational content',e)
}
}

private async ensureProfiles(): Promise<void> {
if (this.aboutProfiles.length != 0) {
return
Expand All @@ -139,14 +117,13 @@ export class ContentfulService {
// Should only be one personal list in conetentful
this.aboutProfiles = res.items[0].fields.people
} catch (e) {
console.error(e)
console.error('Could not fetch about us profiles',e)
}

}

public async getProfiles(): Promise<Profile[]> {
await this.ensureProfiles()
return this.aboutProfiles
private normalizeURLNames(input:string):string {
let custom = input.replace(/ /g,'_')
return encodeURI(custom)
}

}
Expand Up @@ -15,7 +15,7 @@
<div class="content" [innerHTML]="content.convertRichText(course.courseDescription)"></div>
</div>

<div [routerLink]="['/lessons', courseId, lesson.fields.lesson.sys.id]" class="lesson"
<div [routerLink]="['/lessons', courseId, lesson.fields.lesson.fields.titleURLNormalized]" class="lesson"
*ngFor="let lesson of course.lessons">
<app-course-bar-item imgUrl="{{content.getAsset(lesson.fields.lessonPreviewImg)}}"
title="{{lesson.fields.lessonTitle}}" description="{{content.convertRichText(lesson.fields.lessonDescription)}}">
Expand Down
Expand Up @@ -113,7 +113,7 @@ <h2 *ngIf="!nextLesson">Congrats You Have Finished the Course!</h2>
<div class="buttons">
<button [routerLink]="['/courses',courseId]">Course Home</button>
<button *ngIf="nextLesson"
[routerLink]="['/lessons',courseId,nextLesson.sys.id]">{{nextLesson.fields.titleText}}</button>
[routerLink]="['/lessons',courseId,nextLesson.fields.titleURLNormalized]">{{nextLesson.fields.titleText}}</button>
</div>

</div>
Expand Down
1 change: 1 addition & 0 deletions src/app/shared/models/contentfulTypes.ts
Expand Up @@ -44,6 +44,7 @@ export interface Lesson {
sys: Sys
fields: {
titleText: string
titleURLNormalized:string
titleGraphic: Asset
introduction: RichTextContent
introductionInfographic: Asset
Expand Down

0 comments on commit fa46e33

Please sign in to comment.