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 standalone: true,
18 templateUrl: './auto-refetching.component.html',
19 imports: [NgStyle],
20})
21export class AutoRefetchingExampleComponent {
22 #tasksService = inject(TasksService)
23β
24 intervalMs = signal(1000)
25β
26 tasks = injectQuery(() => this.#tasksService.allTasks(this.intervalMs()))
27β
28 addMutation = injectMutation(() => this.#tasksService.addTask())
29 clearMutation = injectMutation(() => this.#tasksService.clearAllTasks())
30β
31 clearTasks() {
32 this.clearMutation.mutate()
33 }
34β
35 inputChange($event: Event) {
36 const target = $event.target as HTMLInputElement
37 this.intervalMs.set(Number(target.value))
38 }
39β
40 addItem($event: Event) {
41 const target = $event.target as HTMLInputElement
42 const value = target.value
43 this.addMutation.mutate(value)
44 target.value = ''
45 }
46}