Skip to content
This repository was archived by the owner on Oct 14, 2019. It is now read-only.

Commit a4f0b7d

Browse files
author
Your Name
committed
Angular and Firebase App
1 parent 87fedc5 commit a4f0b7d

File tree

7 files changed

+56
-59
lines changed

7 files changed

+56
-59
lines changed

src/app/course-detail/course-detail.component.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ <h2>{{ (course$ | async)?.description }}</h2>
1414
(lesson)="navigateToLesson($event)"></lessons-list>
1515

1616
<div class="tools-bar">
17-
<button routerLink="new"
18-
[queryParams]="{courseId: (course$ | async)?.$key}">
17+
<button routerLink="new" [queryParams]="{courseId: (course$ | async)?.$key }">
1918
New Lesson
2019
</button>
2120
</div>

src/app/lesson-form/lesson-form.component.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
<form autocomplete="off" novalidate class="lesson-form" [formGroup]="form">
1+
2+
<form [formGroup]="form" autocomplete="off" novalidate class="lesson-form">
23

34
<fieldset>
45
<legend>Lesson</legend>
56
<div class="form-field">
67
<label>Description:</label>
78
<input name="title" formControlName="description">
8-
<div class="field-error-message" *ngIf="isErrorVisible('description','required')">field is mandatory</div>
9+
<div class="field-error-message"
10+
*ngIf="isErrorVisible('description','required')">field is mandatory</div>
911
</div>
1012
<div class="form-field">
1113
<label>Lesson Url:</label>
@@ -16,7 +18,7 @@
1618
<label>Video Url:</label>
1719
<input name="title" formControlName="videoUrl">
1820
<div class="field-error-message" *ngIf="isErrorVisible('videoUrl','required')">field is mandatory</div>
19-
<div class="field-error-message" *ngIf="isErrorVisible('videoUrl','validUrl')">not a valid url</div>
21+
<!--<div class="field-error-message" *ngIf="isErrorVisible('videoUrl','validUrl')">not a valid url</div>-->
2022
</div>
2123
<div class="form-field">
2224
<label>Tags:</label>
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
11
import {Component, OnInit, Input} from '@angular/core';
22
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
3-
import {validateUrl} from "../shared/validators/validateUrl";
4-
53
@Component({
64
selector: 'lesson-form',
75
templateUrl: './lesson-form.component.html',
86
styleUrls: ['./lesson-form.component.css']
97
})
108
export class LessonFormComponent implements OnInit {
119

12-
form: FormGroup;
13-
14-
@Input()
15-
initialValue: any;
10+
form:FormGroup;
1611

17-
constructor(private fb: FormBuilder) {
12+
constructor(private fb:FormBuilder) {
1813
}
1914

2015
ngOnInit() {
2116
this.form = this.fb.group({
2217
description: ['',Validators.required],
2318
url: ['',Validators.required],
24-
videoUrl: ['',[Validators.required, validateUrl]],
19+
videoUrl: ['',[Validators.required]],
2520
tags: ['',Validators.required],
2621
longDescription: ['']
2722
});
28-
}
2923

30-
ngOnChanges(changes) {
31-
if (changes.initialValue) {
32-
this.form.patchValue(changes.initialValue.currentValue);
33-
}
3424
}
3525

36-
3726
isErrorVisible(field:string, error:string) {
38-
return this.form.controls[field].dirty &&this.form.controls[field].errors && this.form.controls[field].errors[error];
27+
28+
return this.form.controls[field].dirty
29+
&& this.form.controls[field].errors &&
30+
this.form.controls[field].errors[error];
31+
3932
}
4033

41-
get value() {
42-
return this.form.value;
34+
35+
reset() {
36+
this.form.reset();
4337
}
4438

39+
4540
get valid() {
4641
return this.form.valid;
4742
}
4843

49-
reset() {
50-
this.form.reset();
44+
get value() {
45+
return this.form.value;
5146
}
5247

5348
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

22
<lesson-form #form></lesson-form>
33

4-
<button (click)="save(form)" [disabled]="!form.valid">Save New Lesson</button>
5-
4+
<button [disabled]="!form.valid" (click)="save(form)">Save New Lesson</button>
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
2-
import {LessonsService} from "../shared/model/lessons.service";
32
import {ActivatedRoute} from "@angular/router";
3+
import {LessonsService} from "../shared/model/lessons.service";
4+
45

56
@Component({
67
selector: 'app-new-lesson',
@@ -9,18 +10,17 @@ import {ActivatedRoute} from "@angular/router";
910
})
1011
export class NewLessonComponent implements OnInit {
1112

12-
courseId:string;
13+
courseId:string;
1314

14-
constructor(private lessonsService: LessonsService,
15-
private route: ActivatedRoute) { }
15+
constructor(private route:ActivatedRoute, private lessonsService: LessonsService) { }
1616

1717

1818
ngOnInit() {
19+
1920
this.courseId = this.route.snapshot.queryParams['courseId'];
20-
console.log('course', this.courseId);
21+
console.log("course", this.courseId);
2122
}
2223

23-
2424
save(form) {
2525
this.lessonsService.createNewLesson(this.courseId, form.value)
2626
.subscribe(
@@ -33,4 +33,6 @@ export class NewLessonComponent implements OnInit {
3333

3434
}
3535

36+
37+
3638
}

src/app/shared/model/firebaseUpdate.ts

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

src/app/shared/model/lessons.service.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import {Injectable, Inject} from '@angular/core';
2-
import {Observable} from "rxjs/Rx";
2+
import {Observable, Subject} from "rxjs/Rx";
33
import {Lesson} from "./lesson";
44
import {AngularFireDatabase, FirebaseRef} from "angularfire2";
5-
import firebaseUpdate from "./firebaseUpdate";
5+
66

77
@Injectable()
88
export class LessonsService {
99

10-
sdkDb: any;
10+
sdkDb:any;
1111

1212
constructor(private db:AngularFireDatabase, @Inject(FirebaseRef) fb) {
13+
1314
this.sdkDb = fb.database().ref();
15+
1416
}
1517

1618

@@ -72,10 +74,31 @@ export class LessonsService {
7274
dataToSave["lessons/" + newLessonKey] = lessonToSave;
7375
dataToSave[`lessonsPerCourse/${courseId}/${newLessonKey}`] = true;
7476

75-
return firebaseUpdate(this.sdkDb, dataToSave);
77+
78+
return this.firebaseUpdate(dataToSave);
79+
}
80+
81+
firebaseUpdate(dataToSave) {
82+
const subject = new Subject();
83+
84+
this.sdkDb.update(dataToSave)
85+
.then(
86+
val => {
87+
subject.next(val);
88+
subject.complete();
89+
90+
},
91+
err => {
92+
subject.error(err);
93+
subject.complete();
94+
}
95+
);
96+
97+
return subject.asObservable();
7698
}
7799

78100

101+
79102
}
80103

81104

0 commit comments

Comments
 (0)