Open History to see the last commit
1import {
2 ChangeDetectionStrategy,
3 Component,
4 inject,
5 signal,
6} from '@angular/core'
7import {
8 injectMutation,
9 injectQuery,
10} from '@tanstack/angular-query-experimental'
11import { NgStyle } from '@angular/common'
12import { TasksService } from '../services/tasks.service'
13β
14@Component({
15 changeDetection: ChangeDetectionStrategy.OnPush,
16 selector: 'auto-refetching-example',
17 templateUrl: './auto-refetching.component.html',
18 imports: [NgStyle],
19})
20export class AutoRefetchingExampleComponent {
21 readonly #tasksService = inject(TasksService)
22β
23 readonly intervalMs = signal(1000)
24β
25 readonly tasks = injectQuery(() =>
26 this.#tasksService.allTasks(this.intervalMs()),
27 )
28β
29 readonly addMutation = injectMutation(() => this.#tasksService.addTask())
30 readonly clearMutation = injectMutation(() =>
31 this.#tasksService.clearAllTasks(),
32 )
33β
34 clearTasks() {
35 this.clearMutation.mutate()
36 }
37β
38 inputChange($event: Event) {
39 const target = $event.target as HTMLInputElement
40 this.intervalMs.set(Number(target.value))
41 }
42β
43 addItem($event: Event) {
44 const target = $event.target as HTMLInputElement
45 const value = target.value
46 this.addMutation.mutate(value)
47 target.value = ''
48 }
49}