Skip to content

Commit 94b3bf6

Browse files
committed
Angular NgRx Course
1 parent 01fa682 commit 94b3bf6

16 files changed

+200
-159
lines changed

src/app/courses/course-dialog/course-dialog.component.css

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/app/courses/course-dialog/course-dialog.component.html

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/app/courses/course-dialog/course-dialog.component.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
<h2 mat-dialog-title>{{dialogTitle}}</h2>
3+
4+
5+
<mat-dialog-content>
6+
7+
<course-form [course]="course"></course-form>
8+
9+
</mat-dialog-content>
10+
11+
<mat-dialog-actions>
12+
13+
<button mat-raised-button (click)="close()">
14+
Close
15+
</button>
16+
17+
<button mat-raised-button color="primary"
18+
[disabled]="!courseForm?.form?.valid"
19+
(click)="save()">
20+
Save
21+
</button>
22+
23+
</mat-dialog-actions>
24+
25+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {Component, Inject, OnInit, ViewChild} from '@angular/core';
2+
import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material/dialog";
3+
import {Course} from "../model/course";
4+
import {AppState} from "../../reducers";
5+
import {Store} from "@ngrx/store";
6+
import {CourseEntityService} from '../services/course-entity.service';
7+
import {CourseFormComponent} from '../course-form/course-form.component';
8+
9+
@Component({
10+
selector: 'course-dialog',
11+
templateUrl: './edit-course-dialog.component.html',
12+
styleUrls: ['./edit-course-dialog.component.css']
13+
})
14+
export class EditCourseDialogComponent {
15+
16+
dialogTitle:string;
17+
18+
course:Course;
19+
20+
@ViewChild(CourseFormComponent, {static:false})
21+
courseForm: CourseFormComponent;
22+
23+
constructor(
24+
private store: Store<AppState>,
25+
private dialogRef: MatDialogRef<EditCourseDialogComponent>,
26+
@Inject(MAT_DIALOG_DATA) data,
27+
private coursesService: CourseEntityService) {
28+
29+
this.dialogTitle = data.dialogTitle;
30+
this.course = data.course;
31+
32+
}
33+
34+
35+
save() {
36+
37+
const course = {
38+
...this.course,
39+
...this.courseForm.form.value
40+
};
41+
42+
this.coursesService.update(course);
43+
44+
this.dialogRef.close();
45+
46+
}
47+
48+
close() {
49+
this.dialogRef.close();
50+
}
51+
52+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
.mat-form-field {
3+
display: block;
4+
}
5+
6+
textarea {
7+
height: 100px;
8+
resize: vertical;
9+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
<ng-container *ngIf="form">
3+
4+
<ng-container [formGroup]="form">
5+
6+
<mat-form-field>
7+
8+
<input matInput
9+
placeholder="Course Description"
10+
formControlName="description">
11+
12+
</mat-form-field>
13+
14+
<mat-form-field>
15+
16+
<mat-select placeholder="Select category"
17+
formControlName="category">
18+
19+
<mat-option value="BEGINNER">
20+
Beginner</mat-option>
21+
<mat-option value="INTERMEDIATE">
22+
Intermediate</mat-option>
23+
<mat-option value="ADVANCED">
24+
Advanced</mat-option>
25+
26+
</mat-select>
27+
28+
</mat-form-field>
29+
30+
<mat-slide-toggle formControlName="promo">Promotion On</mat-slide-toggle>
31+
32+
33+
<mat-form-field>
34+
35+
<textarea matInput placeholder="Description"
36+
formControlName="longDescription">
37+
38+
</textarea>
39+
40+
</mat-form-field>
41+
42+
</ng-container>
43+
44+
45+
</ng-container>
46+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
2+
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
3+
import {Course} from '../model/course';
4+
5+
@Component({
6+
selector: 'course-form',
7+
templateUrl: './course-form.component.html',
8+
styleUrls: ['./course-form.component.css']
9+
})
10+
export class CourseFormComponent implements OnChanges {
11+
12+
form: FormGroup;
13+
14+
@Input()
15+
course: Course;
16+
17+
constructor(private fb: FormBuilder) {
18+
19+
20+
}
21+
22+
ngOnChanges(changes: SimpleChanges) {
23+
if (changes['course']) {
24+
const change = changes['course'],
25+
course: Course = {...change.currentValue};
26+
27+
if (change.isFirstChange()) {
28+
setTimeout(() => {
29+
this.form = this.fb.group({
30+
description: [course.description, Validators.required],
31+
category: [course.category, Validators.required],
32+
longDescription: [course.longDescription, Validators.required],
33+
promo: [course.promo, []]
34+
});
35+
});
36+
}
37+
}
38+
}
39+
40+
}

src/app/courses/course.effects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Injectable} from '@angular/core';
22
import {Actions, createEffect, ofType} from '@ngrx/effects';
33
import {of} from 'rxjs';
44
import {catchError, map, mergeMap} from 'rxjs/operators';
5-
import {CoursesService} from './services/courses.service';
5+
import {CoursesHttpService} from './services/courses-http.service';
66
import {AppState} from '../reducers';
77
import {select, Store} from '@ngrx/store';
88
import {CourseActions} from './action-types';
@@ -33,7 +33,7 @@ export class CourseEffects {
3333

3434
constructor(
3535
private actions$: Actions,
36-
private coursesService: CoursesService,
36+
private coursesService: CoursesHttpService,
3737
private store: Store<AppState>) {
3838

3939
}

src/app/courses/course/course.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { MatPaginator } from "@angular/material/paginator";
55
import { MatSort } from "@angular/material/sort";
66
import { MatTableDataSource } from "@angular/material/table";
77
import {Course} from "../model/course";
8-
import {CoursesService} from "../services/courses.service";
8+
import {CoursesHttpService} from "../services/courses-http.service";
99
import {debounceTime, distinctUntilChanged, startWith, tap, delay} from 'rxjs/operators';
1010
import {merge, fromEvent, Observable} from "rxjs";
1111
import {LessonsDataSource} from "../services/lessons.datasource";

src/app/courses/courses-card-list/courses-card-list.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Component, Input, OnInit, ViewEncapsulation} from '@angular/core';
22
import {Course} from "../model/course";
33
import { MatDialog, MatDialogConfig } from "@angular/material/dialog";
4-
import {CourseDialogComponent} from "../course-dialog/course-dialog.component";
4+
import {EditCourseDialogComponent} from "../course-dialog/edit-course-dialog.component";
55
import {defaultDialogConfig} from '../../shared/default-dialog-config';
66

77
@Component({
@@ -27,11 +27,10 @@ export class CoursesCardListComponent implements OnInit {
2727

2828
dialogConfig.data = {
2929
dialogTitle:"Edit Course",
30-
mode: 'update',
3130
course
3231
};
3332

34-
this.dialog.open(CourseDialogComponent, dialogConfig);
33+
this.dialog.open(EditCourseDialogComponent, dialogConfig);
3534

3635

3736
}

0 commit comments

Comments
 (0)