@@ -11,87 +11,98 @@ import Timestamp = firebase.firestore.Timestamp;
11
11
import { CoursesService } from "../services/courses.service" ;
12
12
13
13
@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' ]
17
17
} )
18
18
export class CreateCourseComponent implements OnInit {
19
19
20
- courseId :string ;
20
+ courseId : string ;
21
21
22
- percentageChanges$ : Observable < number > ;
22
+ percentageChanges$ : Observable < number > ;
23
23
24
+ iconUrl :string ;
24
25
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
- } ) ;
33
26
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
+ } ) ;
39
35
40
- }
36
+ constructor ( private fb : FormBuilder ,
37
+ private coursesService : CoursesService ,
38
+ private afs : AngularFirestore ,
39
+ private router : Router ,
40
+ private storage : AngularFireStorage ) {
41
+
42
+ }
41
43
42
44
uploadThumbnail ( event ) {
43
45
44
- const file :File = event . target . files [ 0 ] ;
46
+ const file : File = event . target . files [ 0 ] ;
45
47
46
- console . log ( file . name ) ;
48
+ console . log ( file . name ) ;
47
49
48
- const filePath = `courses/${ this . courseId } /${ file . name } ` ;
50
+ const filePath = `courses/${ this . courseId } /${ file . name } ` ;
49
51
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
+ } ) ;
53
55
54
- this . percentageChanges$ = task . percentageChanges ( ) ;
56
+ this . percentageChanges$ = task . percentageChanges ( ) ;
55
57
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
+ } )
56
68
57
- task . snapshotChanges ( ) . subscribe ( ) ;
69
+ )
70
+ . subscribe ( ) ;
58
71
59
72
60
73
}
61
74
62
- ngOnInit ( ) {
63
- this . courseId = this . afs . createId ( ) ;
64
- }
75
+ ngOnInit ( ) {
76
+ this . courseId = this . afs . createId ( ) ;
77
+ }
65
78
66
79
onCreateCourse ( ) {
67
80
68
81
const val = this . form . value ;
69
82
70
83
const newCourse : Partial < Course > = {
71
- description : val . description ,
84
+ description : val . description ,
72
85
url : val . url ,
73
86
longDescription : val . longDescription ,
74
87
promo : val . promo ,
75
88
categories : [ val . category ]
76
89
} ;
77
90
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 ( ) ;
95
106
96
107
97
108
}
0 commit comments