Skip to content

Commit

Permalink
ci: apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Dec 14, 2024
1 parent 5da065d commit 23cfe36
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 73 deletions.
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 = ''
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ export const mockInterceptor: HttpInterceptorFn = (
case 'GET':
return respondWith(
200,
JSON.parse(sessionStorage.getItem('optimistic-updates-tasks') || '[]'),
JSON.parse(
sessionStorage.getItem('optimistic-updates-tasks') || '[]',
),
)
case 'POST':
const tasks = JSON.parse(sessionStorage.getItem('optimistic-updates-tasks') || '[]')
const tasks = JSON.parse(
sessionStorage.getItem('optimistic-updates-tasks') || '[]',
)
tasks.push(req.body)
sessionStorage.setItem('optimistic-updates-tasks', JSON.stringify(tasks))
sessionStorage.setItem(
'optimistic-updates-tasks',
JSON.stringify(tasks),
)
return respondWith(201, {
status: 'success',
task: req.body,
Expand All @@ -42,7 +49,6 @@ export const mockInterceptor: HttpInterceptorFn = (
return respondWith(500, {
status: 'error',
})

}

return next(req)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,39 @@ export class TasksService {
*/
addTask() {
return mutationOptions({
mutationFn: ({ task, failMutation = false }: { task: string; failMutation: boolean }) =>
lastValueFrom(this.#http.post(`/api/tasks${failMutation ? '-wrong-url' : ''}`, task)),
mutationFn: ({
task,
failMutation = false,
}: {
task: string
failMutation: boolean
}) =>
lastValueFrom(
this.#http.post(
`/api/tasks${failMutation ? '-wrong-url' : ''}`,
task,
),
),
mutationKey: ['tasks'],
onSuccess: () => {
this.#queryClient.invalidateQueries({ queryKey: ['tasks'] })
},
onMutate: async ({ task }) => {

// Cancel any outgoing refetches
// (so they don't overwrite our optimistic update)
await this.#queryClient.cancelQueries({ queryKey: ['tasks'] })

// Snapshot the previous value
const previousTodos = this.#queryClient.getQueryData<Array<string>>(['tasks'])
const previousTodos = this.#queryClient.getQueryData<Array<string>>([
'tasks',
])

// Optimistically update to the new value
if (previousTodos) {
this.#queryClient.setQueryData<Array<string>>(['tasks'], [
...previousTodos,
task,
])
this.#queryClient.setQueryData<Array<string>>(
['tasks'],
[...previousTodos, task],
)
}

return previousTodos
Expand All @@ -69,5 +81,4 @@ export class TasksService {
},
})
}

}
3 changes: 1 addition & 2 deletions examples/angular/optimistic-updates/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ import { bootstrapApplication } from '@angular/platform-browser'
import { appConfig } from './app/app.config'
import { AppComponent } from './app/app.component'

bootstrapApplication(AppComponent, appConfig)
.catch((err) => console.error(err))
bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err))
6 changes: 3 additions & 3 deletions examples/react/playground/src/styles.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
body {
margin: 0;
padding: 1rem;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto',
'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans',
'Helvetica Neue', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: white;
Expand Down
6 changes: 3 additions & 3 deletions examples/react/react-router/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ html {
}

body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto',
'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans',
'Helvetica Neue', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/svelte/playground/src/app.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
body {
margin: 0;
padding: 1rem;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto',
'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans',
'Helvetica Neue', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: white;
Expand Down
5 changes: 4 additions & 1 deletion packages/query-devtools/src/Devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,10 @@ const QueryStatus: Component<QueryStatusProps> = (props) => {
css`
cursor: pointer;
&:hover {
background: ${t(colors.gray[200], colors.darkGray[400])}${alpha[80]};
background: ${t(
colors.gray[200],
colors.darkGray[400],
)}${alpha[80]};
}
`,
'tsqd-query-status-tag',
Expand Down

0 comments on commit 23cfe36

Please sign in to comment.