|
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'; |
3 | 2 | import { NgOptimizedImage } from '@angular/common'; |
4 | 3 | import { DecorativeHeaderComponent } from '~core/components/decorative-header/decorative-header.component'; |
5 | 4 | import { CardComponent } from '~core/components/card/card.component'; |
6 | 5 | import { GoogleAnalyticsService } from '~features/home/services/google-analitycs.service'; |
| 6 | +import { interval } from 'rxjs'; |
7 | 7 |
|
8 | 8 | @Component({ |
9 | 9 | selector: 'app-home', |
10 | 10 | templateUrl: './home.component.html', |
11 | 11 | styleUrl: './home.component.scss', |
12 | 12 | changeDetection: ChangeDetectionStrategy.OnPush, |
| 13 | + standalone: true, |
13 | 14 | imports: [DecorativeHeaderComponent, NgOptimizedImage, CardComponent], |
14 | 15 | }) |
15 | | -export class HomeComponent implements OnInit { |
| 16 | +export class HomeComponent { |
16 | 17 | private readonly googleAnalyticsService = inject(GoogleAnalyticsService); |
| 18 | + readonly activeUsersResource = this.googleAnalyticsService.getRealtimeUsersResource(); |
17 | 19 |
|
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 | + }; |
32 | 29 | }); |
33 | 30 | } |
34 | 31 | } |
0 commit comments