Skip to content

Commit c5b5112

Browse files
committed
backend api works
1 parent fe5aa0c commit c5b5112

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<app-places-container title="Available Places">
2+
@if (isFetching()) {
3+
<p class="fallback-text">fetching available places!</p>
4+
}
5+
26
@if (places()) {
3-
<app-places [places]="places()!" />
7+
<app-places [places]="places()!" />
48
} @else if (places()?.length === 0) {
5-
<p class="fallback-text">Unfortunately, no places could be found.</p>
9+
<p class="fallback-text">Unfortunately, no places could be found.</p>
610
}
711
</app-places-container>

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { Component, signal } from '@angular/core';
1+
import { Component, DestroyRef, inject, OnInit, signal } from '@angular/core';
2+
import { HttpClient } from '@angular/common/http';
3+
4+
import { map, tap } from 'rxjs/operators';
25

36
import { Place } from '../../models/place.model';
47
import { PlacesComponent } from '../places.component';
@@ -11,6 +14,35 @@ import { PlacesContainerComponent } from '../places-container/places-container.c
1114
styleUrl: './available-places.component.css',
1215
imports: [PlacesComponent, PlacesContainerComponent],
1316
})
14-
export class AvailablePlacesComponent {
17+
export class AvailablePlacesComponent implements OnInit {
1518
places = signal<Place[] | undefined>(undefined);
19+
isFetching = signal(false);
20+
21+
private http = inject(HttpClient);
22+
private destroyRef = inject(DestroyRef);
23+
24+
ngOnInit(): void {
25+
this.isFetching.set(true);
26+
27+
const availablePlaceSub = this.http
28+
.get<{ places: Place[] }>('/api/v2/places', {
29+
observe: 'response',
30+
})
31+
.pipe(
32+
tap(rawResp => {
33+
console.log('Raw Response: ', rawResp);
34+
}),
35+
map(data => data.body),
36+
)
37+
.subscribe({
38+
next: resp => {
39+
this.places.set(resp?.places);
40+
},
41+
complete: () => {
42+
this.isFetching.set(false);
43+
},
44+
});
45+
46+
this.destroyRef.onDestroy(() => availablePlaceSub.unsubscribe());
47+
}
1648
}

‎src/app/places/places.component.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
@for (place of places(); track place.id) {
33
<li class="place-item">
44
<button (click)="onSelectPlace(place)">
5-
<img
6-
[src]="'http://localhost:3000/' + place.image.src"
7-
[alt]="place.image.alt"
8-
/>
5+
<img [src]="url + place.image.src" [alt]="place.image.alt" />
96
<h3>{{ place.title }}</h3>
107
</button>
118
</li>

‎src/app/places/places.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { Place } from '../models/place.model';
1212
export class PlacesComponent {
1313
places = input.required<Place[]>();
1414
selectPlace = output<Place>();
15+
// protected url = 'http://localhost:3000/';
16+
protected url = 'https://3000-actionanand-angularhttp-six5y8k89a8.ws-us116.gitpod.io/';
1517

1618
onSelectPlace(place: Place) {
1719
this.selectPlace.emit(place);

0 commit comments

Comments
 (0)