11import { Component , OnInit } from '@angular/core' ;
22import { FormBuilder , FormGroup } from '@angular/forms' ;
33import { DataService } from '../../services/data.service' ;
4- import { Library } from '../../models/models.model' ;
4+ import { Book , Library } from '../../models/models.model' ;
55
66@Component ( {
77 selector : 'app-libraries' ,
@@ -13,6 +13,7 @@ export class LibrariesComponent implements OnInit {
1313 msgText : string ;
1414
1515 libraries : object = [ ] ;
16+ books : object = [ ] ;
1617
1718 libraryForm : FormGroup ;
1819
@@ -23,6 +24,7 @@ export class LibrariesComponent implements OnInit {
2324 desc : '' ,
2425 } ;
2526
27+ isLoaded = false ;
2628
2729 constructor ( protected service : DataService , protected fb : FormBuilder ) {
2830
@@ -37,12 +39,32 @@ export class LibrariesComponent implements OnInit {
3739
3840 ngOnInit ( ) {
3941
42+ this . service . getBooks ( ) . subscribe ( ( data ) => {
43+ console . log ( data ) ;
44+ this . books = data as Book [ ] ;
45+ } , ( error ) => {
46+ this . msgText = error . message ;
47+ } , ( ) => {
48+ setTimeout ( ( ) => {
49+ // @ts -ignore
50+ $ ( '.selectpicker' ) . selectpicker ( 'refresh' ) ;
51+ } ) ;
52+ } ) ;
53+
4054 this . service . getLibraries ( ) . subscribe ( ( data ) => {
4155 console . log ( data ) ;
4256 this . libraries = data as Library [ ] ;
4357 } , ( error ) => {
4458 this . msgText = error . message ;
45- } ) ;
59+ } , ( ) => {
60+ this . isLoaded = true ;
61+ } ) ;
62+
63+ }
64+
65+ refreshSelect ( ) {
66+ // @ts -ignore
67+ $ ( '.selectpicker' ) . selectpicker ( 'refresh' ) ;
4668 }
4769
4870 showLibrary ( library ) {
@@ -88,8 +110,30 @@ export class LibrariesComponent implements OnInit {
88110 }
89111
90112 reloadData ( ) {
113+ this . resetItems ( ) ;
114+ this . service . getLibraries ( ) . subscribe ( ( data ) => {
115+ this . libraries = data as Library [ ]
116+ } , ( error ) => {
117+ this . msgText = error . message ;
118+ } , ( ) => {
119+ this . isLoaded = true ;
120+ } ) ;
121+ }
122+
123+ resetItems ( ) {
91124 this . libraries = [ ] ;
92- this . service . getLibraries ( ) . subscribe ( ( data ) => { this . libraries = data as Library [ ] } , ( error ) => { this . msgText = error . message ; } ) ;
125+ this . isLoaded = false ;
126+ this . card = {
127+ title : '' ,
128+ desc : '' ,
129+ } ;
130+
131+ this . libraryForm = this . fb . group ( {
132+ id : null ,
133+ name : '' ,
134+ address : '' ,
135+ books : [ ]
136+ } ) ;
93137 }
94138
95139}
0 commit comments