import { Component, DestroyRef, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Subject, concatMap, map } from 'rxjs';
@Component({
standalone: true,
imports: [CommonModule, FormsModule],
selector: 'app-root',
template: `
possible values: posts, comments, albums, photos, todos, users
{{ response | json }}
`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
submit$$ = new Subject();
input = '';
response: unknown;
private destroyRef = inject(DestroyRef);
constructor(private http: HttpClient) {}
ngOnInit() {
this.submit$$
.pipe(
map(() => this.input),
concatMap((value) =>
this.http.get(`https://jsonplaceholder.typicode.com/${value}/1`)
),
takeUntilDestroyed(this.destroyRef)
)
.subscribe({
next: (value) => {
console.log(value);
this.response = value;
},
error: (error) => {
console.log(error);
this.response = error;
},
complete: () => console.log('done'),
});
}
}