10
10
See the License for the specific language governing permissions and
11
11
limitations under the License.
12
12
*/
13
- import { HttpClient } from '@angular/common/http' ;
14
- import { Component } from '@angular/core' ;
13
+ import { Component , DestroyRef , inject } from '@angular/core' ;
15
14
import { MatIconModule } from '@angular/material/icon' ;
15
+ import { DocumentService } from '../service/document.service' ;
16
+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
17
+ import { tap } from 'rxjs' ;
18
+ import { ChapterPages } from '../model/book' ;
16
19
17
20
@Component ( {
18
21
selector : 'app-book-import' ,
@@ -23,24 +26,26 @@ import { MatIconModule } from '@angular/material/icon';
23
26
} )
24
27
export class BookImportComponent {
25
28
protected file : File | null = null ;
29
+ private destroyRef = inject ( DestroyRef ) ;
30
+ protected uploading = false ;
26
31
27
- constructor ( private http : HttpClient ) { }
32
+ constructor ( private documentService : DocumentService ) { }
28
33
29
- onFileSelected ( $event : Event ) {
34
+ protected onFileSelected ( $event : Event ) : void {
30
35
const files = ! $event . target
31
36
? null
32
37
: ( $event . target as HTMLInputElement ) . files ;
33
38
this . file = ! ! files && files . length > 0 ? files [ 0 ] : null ;
34
39
35
- this . file ?. name
36
40
if ( ! ! this . file ) {
37
41
const formData = new FormData ( ) ;
42
+ const chapters = [ { startPage : 1 , endPage : 2 } as ChapterPages ] ;
43
+ formData . append ( 'book' , this . file )
44
+ formData . append ( 'chapters' , JSON . stringify ( chapters ) ) ;
38
45
39
- formData . append ( "thumbnail" , this . file ) ;
40
-
41
- const upload$ = this . http . post ( "/api/thumbnail-upload" , formData ) ;
42
-
43
- upload$ . subscribe ( ) ;
46
+ this . documentService . postBookForm ( formData ) . pipe ( tap ( ( ) => {
47
+ this . uploading = true ;
48
+ } ) , takeUntilDestroyed ( this . destroyRef ) ) . subscribe ( result => console . log ( result ) ) ;
44
49
}
45
50
}
46
51
}
0 commit comments