Skip to content

Commit eb61b45

Browse files
committed
refactor(app): apply 'httpResource' to the home page component
1 parent 33ad6f2 commit eb61b45

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

src/app/features/home/home.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,6 @@ <h3>Animations</h3>
118118
/>
119119
</div>
120120
<div class="real-time__container">
121-
<p i18n>Users seeing this page: {{activeUsers()}}</p>
121+
<p i18n>Users seeing this page: {{ activeUsersResource.value().activeUsers }}</p>
122122
</div>
123123
</main>
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
import type { OnInit } from '@angular/core';
2-
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, effect, inject } from '@angular/core';
32
import { NgOptimizedImage } from '@angular/common';
43
import { DecorativeHeaderComponent } from '~core/components/decorative-header/decorative-header.component';
54
import { CardComponent } from '~core/components/card/card.component';
65
import { GoogleAnalyticsService } from '~features/home/services/google-analitycs.service';
6+
import { interval } from 'rxjs';
77

88
@Component({
99
selector: 'app-home',
1010
templateUrl: './home.component.html',
1111
styleUrl: './home.component.scss',
1212
changeDetection: ChangeDetectionStrategy.OnPush,
13+
standalone: true,
1314
imports: [DecorativeHeaderComponent, NgOptimizedImage, CardComponent],
1415
})
15-
export class HomeComponent implements OnInit {
16+
export class HomeComponent {
1617
private readonly googleAnalyticsService = inject(GoogleAnalyticsService);
18+
readonly activeUsersResource = this.googleAnalyticsService.getRealtimeUsersResource();
1719

18-
readonly activeUsers = signal(1);
19-
20-
ngOnInit(): void {
21-
this.fetchRealtimeUsers();
22-
setInterval(() => {
23-
this.fetchRealtimeUsers();
24-
}, 5000);
25-
}
26-
27-
fetchRealtimeUsers(): void {
28-
this.googleAnalyticsService.getRealtimeUsers().subscribe({
29-
next: (data) => {
30-
this.activeUsers.set(Math.max(data.activeUsers || 0, 1));
31-
},
20+
constructor() {
21+
this.activeUsersResource.reload();
22+
effect(() => {
23+
const sub = interval(5000).subscribe(() => {
24+
this.activeUsersResource.reload();
25+
});
26+
return () => {
27+
sub.unsubscribe();
28+
};
3229
});
3330
}
3431
}

src/app/features/home/services/google-analitycs.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inject, Injectable } from '@angular/core';
22
import type { Observable } from 'rxjs';
3-
import { HttpClient } from '@angular/common/http';
3+
import { HttpClient, httpResource, type HttpResourceRef } from '@angular/common/http';
44
import { environment } from '~environments/environment';
55

66
@Injectable({
@@ -14,4 +14,9 @@ export class GoogleAnalyticsService {
1414
const getRealtimeUsersEndpoint = `${this.apiUrl}/v1/analytics/realtime-users`;
1515
return this.httpClient.get<{ activeUsers: number }>(getRealtimeUsersEndpoint);
1616
}
17+
getRealtimeUsersResource(): HttpResourceRef<{ activeUsers: number }> {
18+
return httpResource<{ activeUsers: number }>(`${this.apiUrl}/v1/analytics/realtime-users`, {
19+
defaultValue: { activeUsers: 1 },
20+
});
21+
}
1722
}

0 commit comments

Comments
 (0)