Skip to content

Commit 66994ce

Browse files
committed
Angular NgRx Course
1 parent 932ca2e commit 66994ce

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

server/get-courses.route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ export function getAllCourses(req: Request, res: Response) {
99

1010
console.log("Retrieving courses data ...");
1111

12-
res.status(200).json({payload:Object.values(COURSES)});
12+
setTimeout(() => {
13+
14+
res.status(200).json({payload:Object.values(COURSES)});
15+
16+
}, 1500);
17+
18+
1319

1420
}
1521

src/app/courses/home/home.component.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ h2 {
3232
}
3333

3434

35+
36+
.spinner-container {
37+
margin-top: 100px;
38+
}

src/app/courses/home/home.component.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ <h2 class="title">All Courses</h2>
1414
<p>In Promo: {{promoTotal$ | async}}</p>
1515
</div>
1616

17+
<div class="spinner-container" *ngIf="!(loading$ | async )">
18+
19+
<mat-spinner></mat-spinner>
20+
21+
</div>
22+
1723
<mat-tab-group>
1824

1925
<mat-tab label="Beginners">

src/app/courses/home/home.component.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {Component, OnInit} from '@angular/core';
2-
import {Course} from "../model/course";
2+
import {compareCourses, Course} from '../model/course';
33
import {Observable} from "rxjs";
44
import {defaultDialogConfig} from '../shared/default-dialog-config';
55
import {EditCourseDialogComponent} from '../edit-course-dialog/edit-course-dialog.component';
66
import {MatDialog} from '@angular/material';
7-
import {map} from 'rxjs/operators';
7+
import {map, shareReplay} from 'rxjs/operators';
8+
import {CoursesHttpService} from '../services/courses-http.service';
89

910

1011

@@ -17,18 +18,41 @@ export class HomeComponent implements OnInit {
1718

1819
promoTotal$: Observable<number>;
1920

21+
loading$: Observable<boolean>;
22+
2023
beginnerCourses$: Observable<Course[]>;
2124

2225
advancedCourses$: Observable<Course[]>;
2326

2427

2528
constructor(
26-
private dialog: MatDialog) {
29+
private dialog: MatDialog,
30+
private coursesHttpService: CoursesHttpService) {
2731

2832
}
2933

3034
ngOnInit() {
3135

36+
const courses$ = this.coursesHttpService.findAllCourses()
37+
.pipe(
38+
map(courses => courses.sort(compareCourses)),
39+
shareReplay()
40+
);
41+
42+
this.loading$ = courses$.pipe(map(courses => !!courses));
43+
44+
this.beginnerCourses$ = courses$
45+
.pipe(
46+
map(courses => courses.filter(course => course.category == 'BEGINNER'))
47+
);
48+
49+
50+
this.advancedCourses$ = courses$
51+
.pipe(
52+
map(courses => courses.filter(course => course.category == 'ADVANCED'))
53+
);
54+
55+
3256
}
3357

3458
onAddCourse() {

0 commit comments

Comments
 (0)