Skip to content

Commit 4ea3fcc

Browse files
committed
Angular Core Deep Dive
1 parent ab15c70 commit 4ea3fcc

File tree

6 files changed

+30
-58
lines changed

6 files changed

+30
-58
lines changed

server/get-courses.route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {COURSES} from '../src/db-data';
55
export function getAllCourses(req: Request, res: Response) {
66

77

8-
res.status(200).json({payload: Object.values(COURSES)});
8+
res.status(200).json(Object.values(COURSES));
99

1010

1111
}
@@ -20,4 +20,4 @@ export function getCourseById(req: Request, res: Response) {
2020
const course = courses.find(course => course.id == courseId);
2121

2222
res.status(200).json(course);
23-
}
23+
}

src/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<div>
99

10-
<div class="courses">
10+
<div class="courses" *ngIf="courses$ | async as courses">
1111

1212
<course-card *ngFor="let course of courses" (courseChanged)="saveCourse($event)"
1313
[course]="course">

src/app/app.component.ts

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,37 @@ import {Observable} from 'rxjs';
1111
templateUrl: './app.component.html',
1212
styleUrls: ['./app.component.css']
1313
})
14-
export class AppComponent implements AfterViewInit, OnInit {
14+
export class AppComponent implements OnInit {
1515

16-
courses = COURSES;
1716

18-
@ViewChild(CourseCardComponent, {read:HighlightedDirective})
19-
highlighted: HighlightedDirective;
17+
courses$: Observable<Course[]>;
2018

21-
@ViewChildren(CourseCardComponent, {read: ElementRef})
22-
cards : QueryList<ElementRef>;
2319

24-
courses$: Observable<Course[]>;
2520

21+
constructor(private coursesService: CoursesService) {
2622

27-
constructor(private coursesService: CoursesService) {
23+
}
2824

29-
}
25+
ngOnInit() {
26+
this.courses$ = this.coursesService.loadCourses();
27+
}
3028

31-
ngOnInit() {
32-
this.courses$ = this.coursesService.loadCourses();
33-
}
29+
onToggle(isHighlighted: boolean) {
3430

35-
onToggle(isHighlighted:boolean) {
31+
console.log(isHighlighted);
3632

37-
console.log(isHighlighted);
33+
}
3834

39-
}
35+
saveCourse(course: Course) {
4036

37+
console.log('new course', course);
4138

42-
ngAfterViewInit() {
39+
this.coursesService.saveCourse(course)
40+
.subscribe(
41+
() => console.log('Course Saved!'),
42+
console.error
43+
);
4344

44-
console.log(this.highlighted);
45-
46-
47-
}
48-
49-
saveCourse(course:Course) {
50-
51-
console.log('new course', course);
52-
53-
}
45+
}
5446

5547
}

src/app/course-card/course-card.component.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {CourseImageComponent} from '../course-image/course-image.component';
1919
templateUrl: './course-card.component.html',
2020
styleUrls: ['./course-card.component.css']
2121
})
22-
export class CourseCardComponent implements OnInit, AfterViewInit, AfterContentInit {
22+
export class CourseCardComponent implements OnInit {
2323

2424
@Input()
2525
course: Course;
@@ -30,47 +30,22 @@ export class CourseCardComponent implements OnInit, AfterViewInit, AfterContentI
3030
@Output('courseChanged')
3131
courseEmitter = new EventEmitter<Course>();
3232

33-
@ContentChildren(CourseImageComponent, {read: ElementRef})
34-
images: QueryList<ElementRef>;
3533

3634
constructor() {
3735

3836
}
3937

40-
ngAfterViewInit() {
41-
42-
}
43-
44-
ngAfterContentInit() {
45-
46-
}
47-
4838
ngOnInit() {
4939

5040
}
5141

52-
isImageVisible() {
53-
return this.course && this.course.iconUrl;
54-
}
5542

5643
onSaveClicked(description:string) {
5744

5845
this.courseEmitter.emit({...this.course, description});
5946

6047
}
6148

62-
cardClasses() {
63-
if (this.course.category == 'BEGINNER') {
64-
return 'beginner';
65-
}
66-
}
67-
68-
cardStyles() {
69-
return {
70-
'background-image': 'url(' + this.course.iconUrl + ')'
71-
72-
};
73-
}
7449

7550

7651

src/app/services/courses.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Injectable } from '@angular/core';
1+
import {Injectable} from '@angular/core';
22
import {HttpClient} from '@angular/common/http';
33
import {Course} from '../model/course';
4+
import {map} from 'rxjs/operators';
45

56
@Injectable({
67
providedIn: 'root'
@@ -16,4 +17,8 @@ export class CoursesService {
1617
return this.http.get<Course[]>('/api/courses');
1718
}
1819

20+
saveCourse(course: Course) {
21+
return this.http.put(`/api/courses/${course.id}`, course);
22+
}
23+
1924
}

src/db-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ export const COURSES: any = [
8383

8484

8585
export function findCourseById(courseId:number) {
86-
return COURSES.find(course => course.id === courseId);
87-
}
86+
return COURSES.find(course => course.id == courseId);
87+
}

0 commit comments

Comments
 (0)