Skip to content

Commit

Permalink
docs(angular-query): add rxjs example (#8108)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoud-dv authored Sep 28, 2024
1 parent 8727d97 commit 68ca717
Show file tree
Hide file tree
Showing 24 changed files with 509 additions and 28 deletions.
4 changes: 4 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,10 @@
{
"label": "Angular Router",
"to": "framework/angular/examples/router"
},
{
"label": "RxJS autocomplete",
"to": "framework/angular/examples/rxjs"
}
]
}
Expand Down
6 changes: 3 additions & 3 deletions examples/angular/basic/src/app/components/post.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<div>
<a (click)="setPostId.emit(-1)" href="#"> Back </a>
</div>
@if (postQuery.status() === 'pending') {
@if (postQuery.isPending()) {
Loading...
} @else if (postQuery.status() === 'error') {
Error: {{ postQuery.error()?.message }}
} @else if (postQuery.isError()) {
Error: {{ postQuery.error().message }}
}
@if (postQuery.data(); as post) {
<h1>{{ post.title }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion examples/angular/basic/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Angular Query basic example</title>
<title>TanStack Query Angular basic example</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<div>
<a routerLink="/" href="#">Back</a>
</div>
@if (postQuery.status() === 'pending') {
@if (postQuery.isPending()) {
Loading...
} @else if (postQuery.status() === 'error') {
Error: {{ postQuery.error()?.message }}
} @else if (postQuery.isError()) {
Error: {{ postQuery.error().message }}
}
@if (postQuery.data(); as post) {
<h1>{{ post.title }}</h1>
Expand Down
3 changes: 3 additions & 0 deletions examples/angular/router/src/app/components/post.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { PostsService } from '../services/posts-service'
export default class PostComponent {
#postsService = inject(PostsService)

// The Angular router will automatically bind postId
// as `withComponentInputBinding` is added to `provideRouter`.
// See https://angular.dev/api/router/withComponentInputBinding
postId = input.required({
transform: numberAttribute,
})
Expand Down
2 changes: 1 addition & 1 deletion examples/angular/router/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Angular Query router example</title>
<title>TanStack Query Angular router example</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
Expand Down
4 changes: 4 additions & 0 deletions examples/angular/rxjs/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Node.js",
"image": "mcr.microsoft.com/devcontainers/javascript-node:18"
}
6 changes: 6 additions & 0 deletions examples/angular/rxjs/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-check

/** @type {import('eslint').Linter.Config} */
const config = {}

module.exports = config
6 changes: 6 additions & 0 deletions examples/angular/rxjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# TanStack Query Angular RxJS Example

To run this example:

- `npm install` or `yarn` or `pnpm i` or `bun i`
- `npm run start` or `yarn start` or `pnpm start` or `bun start`
104 changes: 104 additions & 0 deletions examples/angular/rxjs/angular.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"cli": {
"packageManager": "pnpm",
"analytics": false,
"cache": {
"enabled": false
}
},
"newProjectRoot": "projects",
"projects": {
"basic": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"inlineTemplate": true,
"inlineStyle": true,
"skipTests": true
},
"@schematics/angular:class": {
"skipTests": true
},
"@schematics/angular:directive": {
"skipTests": true
},
"@schematics/angular:guard": {
"skipTests": true
},
"@schematics/angular:interceptor": {
"skipTests": true
},
"@schematics/angular:pipe": {
"skipTests": true
},
"@schematics/angular:resolver": {
"skipTests": true
},
"@schematics/angular:service": {
"skipTests": true
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/basic",
"index": "src/index.html",
"browser": "src/main.ts",
"polyfills": ["zone.js"],
"tsConfig": "tsconfig.app.json",
"assets": ["src/favicon.ico", "src/mockServiceWorker.js"],
"styles": [],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"buildTarget": "basic:build:production"
},
"development": {
"buildTarget": "basic:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"buildTarget": "basic:build"
}
}
}
}
}
}
31 changes: 31 additions & 0 deletions examples/angular/rxjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@tanstack/query-example-angular-rxjs",
"private": true,
"type": "module",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development"
},
"dependencies": {
"@angular/cdk": "17.3.10",
"@angular/common": "^17.3.12",
"@angular/compiler": "^17.3.12",
"@angular/core": "^17.3.12",
"@angular/forms": "17.3.12",
"@angular/platform-browser": "^17.3.12",
"@angular/platform-browser-dynamic": "^17.3.12",
"@tanstack/angular-query-experimental": "^5.56.2",
"rxjs": "^7.8.1",
"tslib": "^2.6.3",
"zone.js": "^0.14.8"
},
"devDependencies": {
"@angular-devkit/build-angular": "^17.3.8",
"@angular/cli": "^17.3.8",
"@angular/compiler-cli": "^17.3.12",
"@tanstack/angular-query-devtools-experimental": "^5.58.0",
"typescript": "5.3.3"
}
}
51 changes: 51 additions & 0 deletions examples/angular/rxjs/src/app/api/autocomplete-mock.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { HttpResponse } from '@angular/common/http'
import { delayWhen, of, timer } from 'rxjs'
import type { Observable } from 'rxjs'
import type { HttpEvent, HttpInterceptorFn } from '@angular/common/http'

export const autocompleteMockInterceptor: HttpInterceptorFn = (
req,
next,
): Observable<HttpEvent<any>> => {
const { url } = req

if (url.includes('/api/autocomplete')) {
const term = new URLSearchParams(req.url.split('?')[1]).get('term') || ''

const data = [
'C#',
'C++',
'Go',
'Java',
'JavaScript',
'Kotlin',
'Lisp',
'Objective-C',
'PHP',
'Perl',
'Python',
'R',
'Ruby',
'Rust',
'SQL',
'Scala',
'Shell',
'Swift',
'TypeScript',
]

// Simulate network latency with a random delay between 100ms and 500ms
const delayDuration = Math.random() * (500 - 100) + 100
return of(
new HttpResponse({
status: 200,
body: {
suggestions: data.filter((item) =>
item.toLowerCase().startsWith(term.toLowerCase()),
),
},
}),
).pipe(delayWhen(() => timer(delayDuration)))
}
return next(req)
}
12 changes: 12 additions & 0 deletions examples/angular/rxjs/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ChangeDetectionStrategy, Component } from '@angular/core'
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental'
import { ExampleComponent } from './components/example.component'

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'app-root',
standalone: true,
template: `<example /><angular-query-devtools initialIsOpen />`,
imports: [AngularQueryDevtools, ExampleComponent],
})
export class AppComponent {}
29 changes: 29 additions & 0 deletions examples/angular/rxjs/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
provideHttpClient,
withFetch,
withInterceptors,
} from '@angular/common/http'
import {
QueryClient,
provideAngularQuery,
} from '@tanstack/angular-query-experimental'
import { autocompleteMockInterceptor } from './api/autocomplete-mock.interceptor'
import type { ApplicationConfig } from '@angular/core'

export const appConfig: ApplicationConfig = {
providers: [
provideAngularQuery(
new QueryClient({
defaultOptions: {
queries: {
gcTime: 1000 * 60 * 60 * 24, // 24 hours
},
},
}),
),
provideHttpClient(
withFetch(),
withInterceptors([autocompleteMockInterceptor]),
),
],
}
13 changes: 13 additions & 0 deletions examples/angular/rxjs/src/app/components/example.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Search for a programming language</h1>

<form [formGroup]="form">
<input type="text" formControlName="term" />

@if (query.isSuccess() && query.data().suggestions.length) {
<ul>
@for (suggestion of query.data().suggestions; track suggestion) {
<li>{{ suggestion }}</li>
}
</ul>
}
</form>
45 changes: 45 additions & 0 deletions examples/angular/rxjs/src/app/components/example.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
import { toSignal } from '@angular/core/rxjs-interop'
import { NonNullableFormBuilder, ReactiveFormsModule } from '@angular/forms'
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental'
import {
injectQuery,
keepPreviousData,
} from '@tanstack/angular-query-experimental'
import { debounceTime, distinctUntilChanged, lastValueFrom } from 'rxjs'
import { AutocompleteService } from '../services/autocomplete-service'

@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'example',
standalone: true,
templateUrl: './example.component.html',
imports: [AngularQueryDevtools, ReactiveFormsModule],
})
export class ExampleComponent {
#autocompleteService = inject(AutocompleteService)
#fb = inject(NonNullableFormBuilder)

form = this.#fb.group({
term: '',
})

term = toSignal(
this.form.controls.term.valueChanges.pipe(
debounceTime(300),
distinctUntilChanged(),
),
{ initialValue: '' },
)

query = injectQuery(() => ({
queryKey: ['suggestions', this.term()],
queryFn: () => {
return lastValueFrom(
this.#autocompleteService.getSuggestions(this.term()),
)
},
placeholderData: keepPreviousData,
staleTime: 1000 * 60 * 5, // 5 minutes
}))
}
18 changes: 18 additions & 0 deletions examples/angular/rxjs/src/app/services/autocomplete-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { HttpClient } from '@angular/common/http'
import { Injectable, inject } from '@angular/core'
import { of } from 'rxjs'

interface Response {
suggestions: Array<string>
}

@Injectable({
providedIn: 'root',
})
export class AutocompleteService {
#http = inject(HttpClient)
getSuggestions = (term: string) =>
term.trim() === ''
? of({ suggestions: [] })
: this.#http.get<Response>(`/api/autocomplete?term=${term}`)
}
Binary file added examples/angular/rxjs/src/favicon.ico
Binary file not shown.
Loading

0 comments on commit 68ca717

Please sign in to comment.