Skip to content

Commit 825f5e1

Browse files
committed
user removed
1 parent b6b17fc commit 825f5e1

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

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

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

1010
@if (places()) {
11-
<app-places [places]="places()!" />
11+
<app-places [places]="places()!" (selectPlace)="onSelectPlaceRemoce($event)" />
1212
} @else if (places().length === 0) {
1313
<p class="fallback-text">Unfortunately, no places could be found.</p>
1414
}

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

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

88
@Component({
99
selector: 'app-user-places',
@@ -36,4 +36,12 @@ export class UserPlacesComponent implements OnInit {
3636

3737
this.destroyRef.onDestroy(() => availablePlaceSub.unsubscribe());
3838
}
39+
40+
onSelectPlaceRemoce(place: Place) {
41+
const removePlSub = this.placeServ.removeUserPlace(place).subscribe({
42+
next: resp => console.log('Place renoved! ', resp),
43+
});
44+
45+
this.destroyRef.onDestroy(() => removePlSub.unsubscribe());
46+
}
3947
}

src/app/services/places.service.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,22 @@ export class PlacesService {
5555
);
5656
}
5757

58-
removeUserPlace(place: Place) {}
58+
removeUserPlace(place: Place) {
59+
const prevPlaces = this.userPlaces();
60+
61+
if (prevPlaces.some(p => p.id === place.id)) {
62+
// optimistic update
63+
this.userPlaces.set(prevPlaces.filter(pl => pl.id !== place.id));
64+
}
65+
66+
return this.http.delete('/api/v2/user-places/' + place.id).pipe(
67+
catchError(err => {
68+
this.userPlaces.set(prevPlaces);
69+
this.errorServ.showError('Unable to remove the selected place!');
70+
return throwError(() => new Error('Unable to remove the selected place!'));
71+
}),
72+
);
73+
}
5974

6075
private fetchPlaces(url: string, errMsg: string) {
6176
return this.http

0 commit comments

Comments
 (0)