Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

observables, async pipe #35

Open
wants to merge 5 commits into
base: 1-start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/get-courses.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getAllCourses(req: Request, res: Response) {

res.status(200).json({payload:Object.values(COURSES)});

}, 200);
}, 2000);
}


Expand Down
2 changes: 2 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

</mat-toolbar>

<loading></loading>

<router-outlet></router-outlet>

</mat-sidenav-container>
4 changes: 3 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {Component, OnInit} from '@angular/core';
import { LoadingService } from './services/loading.service';



@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
styleUrls: ['./app.component.css'],
providers: [LoadingService]
})
export class AppComponent implements OnInit {

Expand Down
5 changes: 3 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {SafeUrlPipe} from './common/safe-url.pipe';
import {MessagesComponent} from './messages/messages.component';
import {SearchLessonsComponent} from './search-lessons/search-lessons.component';
import { LoadingComponent } from './loading/loading.component';
import { CoursesCardListComponent } from './courses-card-list/courses-card-list.component';

@NgModule({
declarations: [
Expand All @@ -46,8 +47,8 @@ import { LoadingComponent } from './loading/loading.component';
SafeUrlPipe,
MessagesComponent,
SearchLessonsComponent,
LoadingComponent

LoadingComponent,
CoursesCardListComponent
],
imports: [
BrowserModule,
Expand Down
15 changes: 12 additions & 3 deletions src/app/course-dialog/course-dialog.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {AfterViewInit, Component, ElementRef, Inject, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {AfterViewInit, Component, Inject} from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material/dialog";
import {Course} from "../model/course";
import {FormBuilder, Validators, FormGroup} from "@angular/forms";
import * as moment from 'moment';
import {catchError} from 'rxjs/operators';
import {throwError} from 'rxjs';
import { CoursesService } from '../services/courses.service';
import { LoadingService } from '../services/loading.service';

@Component({
selector: 'course-dialog',
Expand All @@ -20,6 +20,8 @@ export class CourseDialogComponent implements AfterViewInit {
constructor(
private fb: FormBuilder,
private dialogRef: MatDialogRef<CourseDialogComponent>,
private courseService: CoursesService,
private loadingService: LoadingService,
@Inject(MAT_DIALOG_DATA) course:Course) {

this.course = course;
Expand All @@ -41,6 +43,13 @@ export class CourseDialogComponent implements AfterViewInit {

const changes = this.form.value;

this.courseService.saveCourse(this.course.id, changes)
.subscribe(
(val) => {
this.dialogRef.close(val);
}
);

}

close() {
Expand Down
8 changes: 8 additions & 0 deletions src/app/courses-card-list/courses-card-list.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

.course-card {
margin: 20px 10px;
}

.course-actions {
text-align: center;
}
29 changes: 29 additions & 0 deletions src/app/courses-card-list/courses-card-list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<mat-card *ngFor="let course of courses"
class="course-card mat-elevation-z10">

<mat-card-header>

<mat-card-title>{{course.description}}</mat-card-title>

</mat-card-header>

<img mat-card-image [src]="course.iconUrl">

<mat-card-content>
<p>{{course.longDescription}}</p>
</mat-card-content>

<mat-card-actions class="course-actions">

<button mat-button class="mat-raised-button mat-primary" [routerLink]="['/courses', course.id]">
VIEW COURSE
</button>

<button mat-button class="mat-raised-button mat-accent"
(click)="editCourse(course)">
EDIT
</button>

</mat-card-actions>

</mat-card>
52 changes: 52 additions & 0 deletions src/app/courses-card-list/courses-card-list.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {Course} from '../model/course';
import {MatDialog, MatDialogConfig} from '@angular/material/dialog';
import {CourseDialogComponent} from '../course-dialog/course-dialog.component';
import {filter, tap} from 'rxjs/operators';

@Component({
selector: 'courses-card-list',
templateUrl: './courses-card-list.component.html',
styleUrls: ['./courses-card-list.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CoursesCardListComponent implements OnInit {

@Input()
courses: Course[] = [];

@Output()
private coursesChanged = new EventEmitter();

constructor(private dialog: MatDialog) {

}

ngOnInit() {

}

editCourse(course: Course) {

const dialogConfig = new MatDialogConfig();

dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.width = "400px";

dialogConfig.data = course;

const dialogRef = this.dialog.open(CourseDialogComponent, dialogConfig);

dialogRef.afterClosed()
.pipe(
filter(val => !!val),
tap(() => this.coursesChanged.emit())

)
.subscribe();


}

}
58 changes: 2 additions & 56 deletions src/app/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,13 @@ <h3>All Courses</h3>

<mat-tab label="Beginners">

<mat-card *ngFor="let course of beginnerCourses" class="course-card mat-elevation-z10">

<mat-card-header>

<mat-card-title>{{course.description}}</mat-card-title>

</mat-card-header>

<img mat-card-image [src]="course.iconUrl">

<mat-card-content>
<p>{{course.longDescription}}</p>
</mat-card-content>

<mat-card-actions class="course-actions">

<button mat-button class="mat-raised-button mat-primary" [routerLink]="['/courses', course.id]">
VIEW COURSE
</button>

<button mat-button class="mat-raised-button mat-accent"
(click)="editCourse(course)">
EDIT
</button>

</mat-card-actions>

</mat-card>
<courses-card-list (coursesChanged)="reloadCourses()" [courses]="beginnerCourses$ | async"></courses-card-list>

</mat-tab>

<mat-tab label="Advanced">

<mat-card *ngFor="let course of advancedCourses" class="course-card mat-elevation-z10">

<mat-card-header>

<mat-card-title>{{course.description}}</mat-card-title>

</mat-card-header>

<img mat-card-image [src]="course.iconUrl">

<mat-card-content>
<p>{{course.longDescription}}</p>
</mat-card-content>

<mat-card-actions class="course-actions">

<button mat-button class="mat-raised-button mat-primary" [routerLink]="['/courses', course.id]">
VIEW COURSE
</button>

<button mat-button class="mat-raised-button mat-accent"
(click)="editCourse(course)">
EDIT
</button>

</mat-card-actions>

</mat-card>
<courses-card-list (coursesChanged)="reloadCourses()" [courses]="advancedCourses$ | async"></courses-card-list>

</mat-tab>

Expand Down
43 changes: 25 additions & 18 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {Component, OnInit} from '@angular/core';
import {Course, sortCoursesBySeqNo} from '../model/course';
import {interval, noop, Observable, of, throwError, timer} from 'rxjs';
import {catchError, delay, delayWhen, filter, finalize, map, retryWhen, shareReplay, tap} from 'rxjs/operators';
import { Observable } from 'rxjs';
import { finalize, map } from 'rxjs/operators';
import {HttpClient} from '@angular/common/http';
import {MatDialog, MatDialogConfig} from '@angular/material/dialog';
import {CourseDialogComponent} from '../course-dialog/course-dialog.component';
import { CoursesService } from '../services/courses.service';
import { LoadingService } from '../services/loading.service';


@Component({
Expand All @@ -14,35 +16,40 @@ import {CourseDialogComponent} from '../course-dialog/course-dialog.component';
})
export class HomeComponent implements OnInit {

beginnerCourses: Course[];
beginnerCourses$: Observable<Course[]>;
advancedCourses$: Observable<Course[]>;

advancedCourses: Course[];


constructor(private http: HttpClient, private dialog: MatDialog) {

}
constructor(private http: HttpClient, private dialog: MatDialog, private coursesService: CoursesService, private loadingService: LoadingService) {}

ngOnInit() {
this.reloadCourses();
}

this.http.get('/api/courses')
.subscribe(
res => {

const courses: Course[] = res["payload"].sort(sortCoursesBySeqNo);
reloadCourses() {

this.beginnerCourses = courses.filter(course => course.category == "BEGINNER");
this.loadingService.loadingOn();

this.advancedCourses = courses.filter(course => course.category == "ADVANCED");
const courses$ = this.coursesService.loadAllCourses()
.pipe(
map(courses => courses.sort(sortCoursesBySeqNo)),
finalize(() => this.loadingService.loadingOff())
)

});
this.beginnerCourses$ = courses$
.pipe(
map(courses => courses.filter(course => course.category === "BEGINNER"))
)

this.advancedCourses$ = courses$
.pipe(
map(courses => courses.filter(course => course.category === "ADVANCED"))
)
}

editCourse(course: Course) {

const dialogConfig = new MatDialogConfig();

dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.width = "400px";
Expand Down
4 changes: 3 additions & 1 deletion src/app/loading/loading.component.html
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

<div class="spinner-container" *ngIf="loadingService.loading$ | async">
<mat-spinner></mat-spinner>
</div>
3 changes: 2 additions & 1 deletion src/app/loading/loading.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core';
import {Observable} from 'rxjs';
import { LoadingService } from '../services/loading.service';

@Component({
selector: 'loading',
Expand All @@ -9,7 +10,7 @@ import {Observable} from 'rxjs';
export class LoadingComponent implements OnInit {


constructor() {
constructor(public loadingService: LoadingService) {

}

Expand Down
28 changes: 28 additions & 0 deletions src/app/services/courses.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { map, shareReplay, tap } from "rxjs/operators";
import { Course } from "../model/course";

@Injectable({
providedIn: 'root'
})

export class CoursesService {
constructor(private http: HttpClient) {}

loadAllCourses(): Observable<Course[]> {
return this.http.get<Course[]>("/api/courses")
.pipe(
map(res => res["payload"]),
shareReplay()
)
}

saveCourse(couresId: string, changes: Partial<Course>): Observable<any> {
return this.http.put(`/api/courses/${couresId}`, changes)
.pipe(
shareReplay()
)
}
}
Loading