Skip to content

Commit 835bcfd

Browse files
author
Your Name
committed
Firebase & AngularFire In Depth
1 parent a9caaef commit 835bcfd

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ <h3>Create New Course</h3>
1414

1515
<span>Course thumbnail:</span>
1616

17-
<img class="course-thumbnail mat-elevation-z3">
17+
<img class="course-thumbnail mat-elevation-z3"
18+
[src]="iconUrl"
19+
*ngIf="iconUrl">
1820

1921
<input type="file" (change)="uploadThumbnail($event)">
2022

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

+59-48
Original file line numberDiff line numberDiff line change
@@ -11,87 +11,98 @@ import Timestamp = firebase.firestore.Timestamp;
1111
import {CoursesService} from "../services/courses.service";
1212

1313
@Component({
14-
selector: 'create-course',
15-
templateUrl: 'create-course.component.html',
16-
styleUrls: ['create-course.component.css']
14+
selector: 'create-course',
15+
templateUrl: 'create-course.component.html',
16+
styleUrls: ['create-course.component.css']
1717
})
1818
export class CreateCourseComponent implements OnInit {
1919

20-
courseId:string;
20+
courseId: string;
2121

22-
percentageChanges$: Observable<number>;
22+
percentageChanges$: Observable<number>;
2323

24+
iconUrl:string;
2425

25-
form = this.fb.group({
26-
description: ['', Validators.required],
27-
category: ["BEGINNER", Validators.required],
28-
url: [''],
29-
longDescription: ['', Validators.required],
30-
promo: [false],
31-
promoStartAt: [null]
32-
});
3326

34-
constructor(private fb:FormBuilder,
35-
private coursesService:CoursesService,
36-
private afs: AngularFirestore,
37-
private router: Router,
38-
private storage: AngularFireStorage) {
27+
form = this.fb.group({
28+
description: ['', Validators.required],
29+
category: ["BEGINNER", Validators.required],
30+
url: [''],
31+
longDescription: ['', Validators.required],
32+
promo: [false],
33+
promoStartAt: [null]
34+
});
3935

40-
}
36+
constructor(private fb: FormBuilder,
37+
private coursesService: CoursesService,
38+
private afs: AngularFirestore,
39+
private router: Router,
40+
private storage: AngularFireStorage) {
41+
42+
}
4143

4244
uploadThumbnail(event) {
4345

44-
const file:File = event.target.files[0];
46+
const file: File = event.target.files[0];
4547

46-
console.log(file.name);
48+
console.log(file.name);
4749

48-
const filePath = `courses/${this.courseId}/${file.name}`;
50+
const filePath = `courses/${this.courseId}/${file.name}`;
4951

50-
const task = this.storage.upload(filePath, file, {
51-
cacheControl: "max-age=2592000,public"
52-
})
52+
const task = this.storage.upload(filePath, file, {
53+
cacheControl: "max-age=2592000,public"
54+
});
5355

54-
this.percentageChanges$ = task.percentageChanges();
56+
this.percentageChanges$ = task.percentageChanges();
5557

58+
task.snapshotChanges()
59+
.pipe(
60+
last(),
61+
concatMap(() => this.storage.ref(filePath).getDownloadURL()),
62+
tap(url => this.iconUrl = url),
63+
catchError(err => {
64+
console.log(err);
65+
alert("Could not create thumbnail url.");
66+
return throwError(err);
67+
})
5668

57-
task.snapshotChanges().subscribe();
69+
)
70+
.subscribe();
5871

5972

6073
}
6174

62-
ngOnInit() {
63-
this.courseId = this.afs.createId();
64-
}
75+
ngOnInit() {
76+
this.courseId = this.afs.createId();
77+
}
6578

6679
onCreateCourse() {
6780

6881
const val = this.form.value;
6982

7083
const newCourse: Partial<Course> = {
71-
description: val.description,
84+
description: val.description,
7285
url: val.url,
7386
longDescription: val.longDescription,
7487
promo: val.promo,
7588
categories: [val.category]
7689
};
7790

78-
newCourse.promoStartAt = Timestamp.fromDate(this.form.value.promoStartAt);
79-
80-
this.coursesService.createCourse(newCourse, this.courseId)
81-
.pipe(
82-
tap(course => {
83-
console.log("Created new course: ", course);
84-
this.router.navigateByUrl("/courses");
85-
}),
86-
catchError(err => {
87-
console.log(err);
88-
alert("Could not create the course.");
89-
return throwError(err);
90-
})
91-
)
92-
.subscribe();
93-
94-
91+
newCourse.promoStartAt = Timestamp.fromDate(this.form.value.promoStartAt);
92+
93+
this.coursesService.createCourse(newCourse, this.courseId)
94+
.pipe(
95+
tap(course => {
96+
console.log("Created new course: ", course);
97+
this.router.navigateByUrl("/courses");
98+
}),
99+
catchError(err => {
100+
console.log(err);
101+
alert("Could not create the course.");
102+
return throwError(err);
103+
})
104+
)
105+
.subscribe();
95106

96107

97108
}

0 commit comments

Comments
 (0)