-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5da065d
commit 23cfe36
Showing
8 changed files
with
94 additions
and
73 deletions.
There are no files selected for viewing
98 changes: 50 additions & 48 deletions
98
examples/angular/optimistic-updates/src/app/components/optimistic-updates.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,73 @@ | ||
import { Component, inject } from '@angular/core'; | ||
import { injectMutation, injectQuery } from '@tanstack/angular-query-experimental'; | ||
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; | ||
import { DatePipe } from '@angular/common'; | ||
import { TasksService } from '../services/tasks.service'; | ||
import { Component, inject } from '@angular/core' | ||
import { | ||
injectMutation, | ||
injectQuery, | ||
} from '@tanstack/angular-query-experimental' | ||
import { FormsModule, ReactiveFormsModule } from '@angular/forms' | ||
import { DatePipe } from '@angular/common' | ||
import { TasksService } from '../services/tasks.service' | ||
|
||
@Component({ | ||
selector: 'optimistic-updates', | ||
imports: [ | ||
FormsModule, | ||
DatePipe | ||
], | ||
imports: [FormsModule, DatePipe], | ||
template: ` | ||
<p> | ||
In this example, new items can be created using a mutation. The new item will be optimistically added to the list in hopes that the server accepts the item. If it does, the list is refetched with the true items from the list. Every now and then, the mutation may fail though. When that happens, the previous list of items is restored and the list is again refetched from the server. | ||
In this example, new items can be created using a mutation. The new item | ||
will be optimistically added to the list in hopes that the server accepts | ||
the item. If it does, the list is refetched with the true items from the | ||
list. Every now and then, the mutation may fail though. When that happens, | ||
the previous list of items is restored and the list is again refetched | ||
from the server. | ||
</p> | ||
<hr/> | ||
@if(tasks.isLoading()){ | ||
<p>Loading...</p> | ||
} | ||
<div class="container"> | ||
<label> | ||
<input type="checkbox" [(ngModel)]="failMutation" /> | ||
Fail Mutation | ||
</label> | ||
<div class="input-container"> | ||
<input type="text" [(ngModel)]="newItem" placeholder="Enter text" /> | ||
<button (click)="addItem()">Create</button> | ||
<ul> | ||
@for(task of tasks.data(); track task){ | ||
<li>{{task}}</li> | ||
} | ||
</ul> | ||
<hr /> | ||
@if (tasks.isLoading()) { | ||
<p>Loading...</p> | ||
} | ||
<div> | ||
Updated At: {{tasks.dataUpdatedAt() | date: 'MMMM d, h:mm:ss a '}} | ||
</div> | ||
<div class="container"> | ||
<label> | ||
<input type="checkbox" [(ngModel)]="failMutation" /> | ||
Fail Mutation | ||
</label> | ||
</div> | ||
@if(!tasks.isLoading() && tasks.isFetching()){ | ||
<p>Fetching in background</p> | ||
} | ||
<div class="input-container"> | ||
<input type="text" [(ngModel)]="newItem" placeholder="Enter text" /> | ||
<button (click)="addItem()">Create</button> | ||
<ul> | ||
@for (task of tasks.data(); track task) { | ||
<li>{{ task }}</li> | ||
} | ||
</ul> | ||
<div> | ||
Updated At: {{ tasks.dataUpdatedAt() | date: 'MMMM d, h:mm:ss a ' }} | ||
</div> | ||
</div> | ||
@if (!tasks.isLoading() && tasks.isFetching()) { | ||
<p>Fetching in background</p> | ||
} | ||
</div> | ||
`, | ||
styles: `` | ||
styles: ``, | ||
}) | ||
export class OptimisticUpdatesComponent { | ||
#tasksService = inject(TasksService) | ||
|
||
#tasksService = inject(TasksService); | ||
|
||
tasks = (() => injectQuery(() => this.#tasksService.allTasks()))(); | ||
tasks = (() => injectQuery(() => this.#tasksService.allTasks()))() | ||
clearMutation = injectMutation(() => this.#tasksService.addTask()) | ||
addMutation = injectMutation(() => this.#tasksService.addTask()) | ||
|
||
|
||
newItem = '' | ||
failMutation = false; | ||
failMutation = false | ||
|
||
addItem() { | ||
if (!this.newItem) return; | ||
|
||
this.addMutation.mutate({ task: this.newItem, failMutation: this.failMutation }); | ||
this.newItem = ''; | ||
if (!this.newItem) return | ||
|
||
this.addMutation.mutate({ | ||
task: this.newItem, | ||
failMutation: this.failMutation, | ||
}) | ||
this.newItem = '' | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters