1- import { Component , signal } from '@angular/core' ;
1+ import { Component , DestroyRef , inject , OnInit , signal } from '@angular/core' ;
22
33import { Place } from '../../models/place.model' ;
44import { PlacesComponent } from '../places.component' ;
55import { PlacesContainerComponent } from '../places-container/places-container.component' ;
6+ import { PlacesService } from '../../services/places.service' ;
67
78@Component ( {
89 selector : 'app-available-places' ,
@@ -11,6 +12,39 @@ import { PlacesContainerComponent } from '../places-container/places-container.c
1112 styleUrl : './available-places.component.css' ,
1213 imports : [ PlacesComponent , PlacesContainerComponent ] ,
1314} )
14- export class AvailablePlacesComponent {
15+ export class AvailablePlacesComponent implements OnInit {
1516 places = signal < Place [ ] | undefined > ( undefined ) ;
17+ isFetching = signal ( false ) ;
18+ errorMsg = signal ( '' ) ;
19+
20+ private placeServ = inject ( PlacesService ) ;
21+ private destroyRef = inject ( DestroyRef ) ;
22+
23+ ngOnInit ( ) : void {
24+ this . isFetching . set ( true ) ;
25+
26+ const availablePlaceSub = this . placeServ . loadAvailablePlaces ( ) . subscribe ( {
27+ next : resp => {
28+ this . places . set ( resp ?. places ) ;
29+ this . errorMsg . set ( '' ) ;
30+ } ,
31+ complete : ( ) => {
32+ this . isFetching . set ( false ) ;
33+ } ,
34+ error : ( err : Error ) => {
35+ console . error ( err . message ) ;
36+ this . errorMsg . set ( err . message ) ;
37+ } ,
38+ } ) ;
39+
40+ this . destroyRef . onDestroy ( ( ) => availablePlaceSub . unsubscribe ( ) ) ;
41+ }
42+
43+ onSelectPlaces ( selectedPlace : Place ) {
44+ const selectPlaceSub = this . placeServ . addPlaceToUserPlaces ( selectedPlace . id ) . subscribe ( {
45+ next : resp => console . log ( 'Place added. ' , resp ) ,
46+ } ) ;
47+
48+ this . destroyRef . onDestroy ( ( ) => selectPlaceSub . unsubscribe ( ) ) ;
49+ }
1650}
0 commit comments