Skip to content

Commit b83fb83

Browse files
committed
refactoring
1 parent a3aa895 commit b83fb83

File tree

5 files changed

+104
-47
lines changed

5 files changed

+104
-47
lines changed

src/app/places/available-places/available-places.component.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<app-places-container title="Available Places">
2-
@if (isFetching()) {
2+
@if (isFetching() && !errorMsg()) {
33
<p class="fallback-text">fetching available places!</p>
44
}
55

6+
@if (errorMsg()) {
7+
<p class="fallback-text">{{ errorMsg() }}</p>
8+
}
9+
610
@if (places()) {
711
<app-places [places]="places()!" (selectPlace)="onSelectPlaces($event)" />
812
} @else if (places()?.length === 0) {
Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { Component, DestroyRef, inject, OnInit, signal } from '@angular/core';
2-
import { HttpClient } from '@angular/common/http';
3-
4-
import { catchError, map, tap } from 'rxjs/operators';
5-
import { throwError } from 'rxjs';
62

73
import { Place } from '../../models/place.model';
84
import { PlacesComponent } from '../places.component';
95
import { PlacesContainerComponent } from '../places-container/places-container.component';
6+
import { PlacesService } from '../../services/places.service';
107

118
@Component({
129
selector: 'app-available-places',
@@ -18,49 +15,36 @@ import { PlacesContainerComponent } from '../places-container/places-container.c
1815
export class AvailablePlacesComponent implements OnInit {
1916
places = signal<Place[] | undefined>(undefined);
2017
isFetching = signal(false);
18+
errorMsg = signal('');
2119

22-
private http = inject(HttpClient);
20+
private placeServ = inject(PlacesService);
2321
private destroyRef = inject(DestroyRef);
2422

2523
ngOnInit(): void {
2624
this.isFetching.set(true);
2725

28-
const availablePlaceSub = this.http
29-
.get<{ places: Place[] }>('/api/v2/places', {
30-
observe: 'response',
31-
})
32-
.pipe(
33-
tap(rawResp => {
34-
console.log('Raw Response: ', rawResp);
35-
}),
36-
map(data => data.body),
37-
catchError(error => {
38-
console.error(error);
39-
return throwError(() => new Error('Something went wrong!'));
40-
}),
41-
)
42-
.subscribe({
43-
next: resp => {
44-
this.places.set(resp?.places);
45-
},
46-
complete: () => {
47-
this.isFetching.set(false);
48-
},
49-
error: (err: Error) => {
50-
console.error(err.message);
51-
},
52-
});
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+
});
5339

5440
this.destroyRef.onDestroy(() => availablePlaceSub.unsubscribe());
5541
}
5642

5743
onSelectPlaces(selectedPlace: Place) {
58-
this.http
59-
.put('/api/v2/user-places', {
60-
placeId: selectedPlace.id,
61-
})
62-
.subscribe({
63-
next: resp => console.log('Place added. ', resp),
64-
});
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());
6549
}
6650
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
<app-places-container title="Your Favorite Places">
2-
<p>Todo...</p>
2+
@if (isFetching() && !errorMsg()) {
3+
<p class="fallback-text">fetching available places!</p>
4+
}
5+
6+
@if (errorMsg()) {
7+
<p class="fallback-text">{{ errorMsg() }}</p>
8+
}
9+
10+
@if (places()) {
11+
<app-places [places]="places()!" />
12+
} @else if (places()?.length === 0) {
13+
<p class="fallback-text">Unfortunately, no places could be found.</p>
14+
}
315
</app-places-container>
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Component } from '@angular/core';
1+
import { Component, DestroyRef, inject, OnInit, signal } from '@angular/core';
22

33
import { PlacesContainerComponent } from '../places-container/places-container.component';
44
import { PlacesComponent } from '../places.component';
5+
import { Place } from '../../models/place.model';
6+
import { PlacesService } from '../../services/places.service';
57

68
@Component({
79
selector: 'app-user-places',
@@ -10,5 +12,31 @@ import { PlacesComponent } from '../places.component';
1012
styleUrl: './user-places.component.css',
1113
imports: [PlacesContainerComponent, PlacesComponent],
1214
})
13-
export class UserPlacesComponent {
15+
export class UserPlacesComponent implements OnInit {
16+
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.loadUserPlaces().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+
}
1442
}

src/app/services/places.service.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
2-
import { Injectable, signal } from '@angular/core';
2+
import { inject, Injectable, signal } from '@angular/core';
3+
import { HttpClient } from '@angular/common/http';
4+
5+
import { catchError, map, tap } from 'rxjs/operators';
6+
import { throwError } from 'rxjs';
37

48
import { Place } from '../models/place.model';
59

@@ -8,15 +12,40 @@ import { Place } from '../models/place.model';
812
})
913
export class PlacesService {
1014
private userPlaces = signal<Place[]>([]);
11-
private readonly url = '/api/v2/places';
12-
1315
loadedUserPlaces = this.userPlaces.asReadonly();
1416

15-
loadAvailablePlaces() {}
17+
private http = inject(HttpClient);
1618

17-
loadUserPlaces() {}
19+
loadAvailablePlaces() {
20+
return this.fetchPlaces('/api/v2/places', 'Error loading available places!');
21+
}
1822

19-
addPlaceToUserPlaces(place: Place) {}
23+
loadUserPlaces() {
24+
return this.fetchPlaces('/api/v2/user-places', 'Error loading user places!');
25+
}
26+
27+
addPlaceToUserPlaces(placeId: string) {
28+
return this.http.put('/api/v2/user-places', {
29+
placeId,
30+
});
31+
}
2032

2133
removeUserPlace(place: Place) {}
34+
35+
private fetchPlaces(url: string, errMsg: string) {
36+
return this.http
37+
.get<{ places: Place[] }>(url, {
38+
observe: 'response',
39+
})
40+
.pipe(
41+
tap(rawResp => {
42+
console.log('Raw Response: ', rawResp);
43+
}),
44+
map(data => data.body),
45+
catchError(error => {
46+
console.error(error);
47+
return throwError(() => new Error(errMsg));
48+
}),
49+
);
50+
}
2251
}

0 commit comments

Comments
 (0)