Skip to content

Commit 4d2c26b

Browse files
committed
optimistic updating of user places
1 parent e024c30 commit 4d2c26b

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class AvailablePlacesComponent implements OnInit {
4141
}
4242

4343
onSelectPlaces(selectedPlace: Place) {
44-
const selectPlaceSub = this.placeServ.addPlaceToUserPlaces(selectedPlace.id).subscribe({
44+
const selectPlaceSub = this.placeServ.addPlaceToUserPlaces(selectedPlace).subscribe({
4545
next: resp => console.log('Place added. ', resp),
4646
});
4747

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@if (places()) {
1111
<app-places [places]="places()!" />
12-
} @else if (places()?.length === 0) {
12+
} @else if (places().length === 0) {
1313
<p class="fallback-text">Unfortunately, no places could be found.</p>
1414
}
1515
</app-places-container>

src/app/places/user-places/user-places.component.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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';
5+
// import { Place } from '../../models/place.model';
66
import { PlacesService } from '../../services/places.service';
77

88
@Component({
@@ -13,21 +13,18 @@ import { PlacesService } from '../../services/places.service';
1313
imports: [PlacesContainerComponent, PlacesComponent],
1414
})
1515
export class UserPlacesComponent implements OnInit {
16-
places = signal<Place[] | undefined>(undefined);
1716
isFetching = signal(false);
1817
errorMsg = signal('');
1918

2019
private placeServ = inject(PlacesService);
2120
private destroyRef = inject(DestroyRef);
2221

22+
places = this.placeServ.loadedUserPlaces;
23+
2324
ngOnInit(): void {
2425
this.isFetching.set(true);
2526

2627
const availablePlaceSub = this.placeServ.loadUserPlaces().subscribe({
27-
next: resp => {
28-
this.places.set(resp?.places);
29-
this.errorMsg.set('');
30-
},
3128
complete: () => {
3229
this.isFetching.set(false);
3330
},

src/app/services/places.service.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,35 @@ export class PlacesService {
2121
}
2222

2323
loadUserPlaces() {
24-
return this.fetchPlaces('/api/v2/user-places', 'Error loading user places!');
24+
return this.fetchPlaces('/api/v2/user-places', 'Error loading user places!').pipe(
25+
tap({
26+
next: resp => {
27+
if (resp) {
28+
this.userPlaces.set(resp.places);
29+
}
30+
},
31+
}),
32+
);
2533
}
2634

27-
addPlaceToUserPlaces(placeId: string) {
28-
return this.http.put('/api/v2/user-places', {
29-
placeId,
30-
});
35+
addPlaceToUserPlaces(place: Place) {
36+
const prevPlaces = this.userPlaces();
37+
38+
if (!prevPlaces.some(p => p.id === place.id)) {
39+
// optimistic update
40+
this.userPlaces.set([...prevPlaces, place]);
41+
}
42+
43+
return this.http
44+
.put('/api/v2/user-places', {
45+
placeId: place.id,
46+
})
47+
.pipe(
48+
catchError(err => {
49+
this.userPlaces.set(prevPlaces);
50+
return throwError(() => new Error('Unable to store the selected place!'));
51+
}),
52+
);
3153
}
3254

3355
removeUserPlace(place: Place) {}

0 commit comments

Comments
 (0)