From a5d8821e54de68f21ddc7f53daab5a88125fd268 Mon Sep 17 00:00:00 2001 From: Arnoud de Vries <6420061+arnoud-dv@users.noreply.github.com> Date: Mon, 12 Feb 2024 21:16:44 +0100 Subject: [PATCH] docs(angular-query): angular example improvements --- docs/config.json | 4 +- .../.devcontainer/devcontainer.json | 4 - .../default-query-function/.eslintrc.cjs | 6 - .../angular/default-query-function/README.md | 6 - .../angular/default-query-function/index.html | 12 -- .../default-query-function/package.json | 26 ---- ...efault-query-function-example.component.ts | 141 ------------------ .../default-query-function/src/index.ts | 30 ---- .../default-query-function/tsconfig.json | 27 ---- .../src/app/components/example.component.ts | 2 +- examples/angular/router/angular.json | 3 +- .../src/app/components/post.component.ts | 10 +- examples/angular/simple/README.md | 2 +- examples/angular/simple/angular.json | 3 +- examples/angular/simple/package.json | 4 +- pnpm-lock.yaml | 37 ----- 16 files changed, 15 insertions(+), 302 deletions(-) delete mode 100644 examples/angular/default-query-function/.devcontainer/devcontainer.json delete mode 100644 examples/angular/default-query-function/.eslintrc.cjs delete mode 100644 examples/angular/default-query-function/README.md delete mode 100644 examples/angular/default-query-function/index.html delete mode 100644 examples/angular/default-query-function/package.json delete mode 100644 examples/angular/default-query-function/src/default-query-function-example.component.ts delete mode 100644 examples/angular/default-query-function/src/index.ts delete mode 100644 examples/angular/default-query-function/tsconfig.json diff --git a/docs/config.json b/docs/config.json index c3d891392d..1f72a4250d 100644 --- a/docs/config.json +++ b/docs/config.json @@ -959,8 +959,8 @@ "to": "framework/angular/examples/infinite-query-with-max-pages" }, { - "label": "Default Query Function", - "to": "framework/angular/examples/default-query-function" + "label": "Angular Router", + "to": "framework/angular/examples/router" } ] } diff --git a/examples/angular/default-query-function/.devcontainer/devcontainer.json b/examples/angular/default-query-function/.devcontainer/devcontainer.json deleted file mode 100644 index 36f47d8762..0000000000 --- a/examples/angular/default-query-function/.devcontainer/devcontainer.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "Node.js", - "image": "mcr.microsoft.com/devcontainers/javascript-node:18" -} diff --git a/examples/angular/default-query-function/.eslintrc.cjs b/examples/angular/default-query-function/.eslintrc.cjs deleted file mode 100644 index cca134ce16..0000000000 --- a/examples/angular/default-query-function/.eslintrc.cjs +++ /dev/null @@ -1,6 +0,0 @@ -// @ts-check - -/** @type {import('eslint').Linter.Config} */ -const config = {} - -module.exports = config diff --git a/examples/angular/default-query-function/README.md b/examples/angular/default-query-function/README.md deleted file mode 100644 index 28462a4ad2..0000000000 --- a/examples/angular/default-query-function/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Basic example - -To run this example: - -- `npm install` or `yarn` or `pnpm i` -- `npm run dev` or `yarn dev` or `pnpm dev` diff --git a/examples/angular/default-query-function/index.html b/examples/angular/default-query-function/index.html deleted file mode 100644 index c1737e5a82..0000000000 --- a/examples/angular/default-query-function/index.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - TanStack Query Angular Default Query Function Example App - - - - - - diff --git a/examples/angular/default-query-function/package.json b/examples/angular/default-query-function/package.json deleted file mode 100644 index 1a0201b5c4..0000000000 --- a/examples/angular/default-query-function/package.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "@tanstack/query-example-angular-default-query-function", - "private": true, - "type": "module", - "scripts": { - "dev": "vite", - "build": "vite build", - "preview": "vite preview", - "test:types": "tsc" - }, - "dependencies": { - "@angular/common": "^17.1.3", - "@angular/compiler": "^17.1.3", - "@angular/core": "^17.1.3", - "@angular/platform-browser": "^17.1.3", - "@tanstack/angular-query-devtools-experimental": "^5.20.2", - "@tanstack/angular-query-experimental": "^5.20.2", - "axios": "^1.6.7", - "rxjs": "^7.8.1", - "zone.js": "^0.14.3" - }, - "devDependencies": { - "typescript": "5.2.2", - "vite": "^5.1.1" - } -} diff --git a/examples/angular/default-query-function/src/default-query-function-example.component.ts b/examples/angular/default-query-function/src/default-query-function-example.component.ts deleted file mode 100644 index 767ee748b8..0000000000 --- a/examples/angular/default-query-function/src/default-query-function-example.component.ts +++ /dev/null @@ -1,141 +0,0 @@ -import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental' -import { - ChangeDetectionStrategy, - Component, - EventEmitter, - Input, - Output, - signal, -} from '@angular/core' -import { CommonModule, NgIf } from '@angular/common' -import { - injectQuery, - injectQueryClient, -} from '@tanstack/angular-query-experimental' - -type Post = { - id: number - title: string - body: string -} - -@Component({ - changeDetection: ChangeDetectionStrategy.OnPush, - selector: 'post', - standalone: true, - imports: [NgIf], - template: ` -
-
- Back -
- - Loading... - - - Error: {{ postQuery.error()?.message }} - - -

{{ post.title }}

-
-

{{ post.body }}

-
-
Background Updating...
-
-
- `, -}) -export class PostComponent { - @Output() setPostId = new EventEmitter() - - // Until Angular supports signal-based inputs, we have to set a signal - @Input({ required: true }) - set postId(value: number) { - this.postIdSignal.set(value) - } - postIdSignal = signal(0) - - // You can even leave out the queryFn and just go straight into options - postQuery = injectQuery(() => ({ - enabled: this.postIdSignal() > 0, - queryKey: [`/posts/${this.postIdSignal()}`], - })) - - queryClient = injectQueryClient -} - -@Component({ - changeDetection: ChangeDetectionStrategy.OnPush, - selector: 'posts', - standalone: true, - template: `
-

Posts

-
-
Loading...
-
- Error: {{ postsQuery.error()?.message }} -
- -

- - - {{ post.title }} -

-
- -
Background Updating...
-
-
`, - imports: [CommonModule], -}) -export class PostsComponent { - @Output() setPostId = new EventEmitter() - - // All you have to do now is pass a key! - postsQuery = injectQuery>(() => ({ - queryKey: ['/posts'], - })) - - queryClient = injectQueryClient() -} - -@Component({ - changeDetection: ChangeDetectionStrategy.OnPush, - selector: 'default-query-function-example', - standalone: true, - template: ` -

- As you visit the posts below, you will notice them in a loading state the - first time you load them. However, after you return to this list and click - on any posts you have already visited again, you will see them load - instantly and background refresh right before your eyes! - - (You may need to throttle your network speed to simulate longer loading - sequences) - -

- -
- -
- - - - `, - imports: [AngularQueryDevtools, NgIf, PostComponent, PostsComponent], -}) -export class DefaultQueryFunctionExampleComponent { - postId = signal(-1) -} diff --git a/examples/angular/default-query-function/src/index.ts b/examples/angular/default-query-function/src/index.ts deleted file mode 100644 index b5bf027c4c..0000000000 --- a/examples/angular/default-query-function/src/index.ts +++ /dev/null @@ -1,30 +0,0 @@ -import '@angular/compiler' -import 'zone.js' -import { bootstrapApplication } from '@angular/platform-browser' -import { provideAngularQuery } from '@tanstack/angular-query-experimental' -import { QueryClient } from '@tanstack/angular-query-experimental' -import axios from 'axios' -import { DefaultQueryFunctionExampleComponent } from './default-query-function-example.component' -import type { QueryFunction } from '@tanstack/angular-query-experimental' - -// Define a default query function that will receive the query key -const defaultQueryFn: QueryFunction = async ({ queryKey }) => { - const { data } = await axios.get( - `https://jsonplaceholder.typicode.com${queryKey[0]}`, - ) - return data -} - -bootstrapApplication(DefaultQueryFunctionExampleComponent, { - providers: [ - provideAngularQuery( - new QueryClient({ - defaultOptions: { - queries: { - queryFn: defaultQueryFn, - }, - }, - }), - ), - ], -}) diff --git a/examples/angular/default-query-function/tsconfig.json b/examples/angular/default-query-function/tsconfig.json deleted file mode 100644 index c3f5c82ab6..0000000000 --- a/examples/angular/default-query-function/tsconfig.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "compilerOptions": { - "target": "ES2022", - "useDefineForClassFields": true, - "module": "ESNext", - "lib": ["ESNext", "DOM"], - "moduleResolution": "Bundler", - "strict": true, - "resolveJsonModule": true, - "isolatedModules": true, - "esModuleInterop": true, - "noEmit": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "skipLibCheck": true, - "types": ["vite/client"], - "experimentalDecorators": true - }, - "angularCompilerOptions": { - "enableI18nLegacyMessageIdFormat": false, - "strictInjectionParameters": true, - "strictInputAccessModifiers": true, - "strictTemplates": true - }, - "include": ["src", ".eslintrc.cjs"] -} diff --git a/examples/angular/infinite-query-with-max-pages/src/app/components/example.component.ts b/examples/angular/infinite-query-with-max-pages/src/app/components/example.component.ts index c831c9ef73..509961ef80 100644 --- a/examples/angular/infinite-query-with-max-pages/src/app/components/example.component.ts +++ b/examples/angular/infinite-query-with-max-pages/src/app/components/example.component.ts @@ -22,7 +22,7 @@ export class ExampleComponent { query = injectInfiniteQuery(() => ({ queryKey: ['projects'], - queryFn: async ({ pageParam }) => { + queryFn: ({ pageParam }) => { return lastValueFrom(this.projectsService.getProjects(pageParam)) }, initialPageParam: 0, diff --git a/examples/angular/router/angular.json b/examples/angular/router/angular.json index 5b5a4d61a2..1b5cd74c2c 100644 --- a/examples/angular/router/angular.json +++ b/examples/angular/router/angular.json @@ -2,7 +2,8 @@ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "cli": { - "packageManager": "pnpm" + "packageManager": "pnpm", + "analytics": false }, "newProjectRoot": "projects", "projects": { diff --git a/examples/angular/router/src/app/components/post.component.ts b/examples/angular/router/src/app/components/post.component.ts index bfa9a3ea04..6d60a170bc 100644 --- a/examples/angular/router/src/app/components/post.component.ts +++ b/examples/angular/router/src/app/components/post.component.ts @@ -1,10 +1,9 @@ import { ChangeDetectionStrategy, Component, - EventEmitter, - Output, inject, input, + numberAttribute, } from '@angular/core' import { RouterLink } from '@angular/router' import { @@ -23,15 +22,16 @@ import { PostsService } from '../services/posts-service' }) export default class PostComponent { #postsService = inject(PostsService) + queryClient = injectQueryClient() - postId = input(0, { - transform: (value: string) => Number(value), + postId = input(0, { + transform: numberAttribute, }) postQuery = injectQuery(() => ({ enabled: this.postId() > 0, queryKey: ['post', this.postId()], - queryFn: async () => { + queryFn: () => { return lastValueFrom(this.#postsService.postById$(this.postId())) }, })) diff --git a/examples/angular/simple/README.md b/examples/angular/simple/README.md index 4ca7ebd60c..cddff73343 100644 --- a/examples/angular/simple/README.md +++ b/examples/angular/simple/README.md @@ -1,4 +1,4 @@ -# Simple example +# Angular Query simple example To run this example: diff --git a/examples/angular/simple/angular.json b/examples/angular/simple/angular.json index 5c526b7ef7..8c600c0813 100644 --- a/examples/angular/simple/angular.json +++ b/examples/angular/simple/angular.json @@ -2,7 +2,8 @@ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "cli": { - "packageManager": "pnpm" + "packageManager": "pnpm", + "analytics": false }, "newProjectRoot": "projects", "projects": { diff --git a/examples/angular/simple/package.json b/examples/angular/simple/package.json index 546fb03d41..fbd1a48fcc 100644 --- a/examples/angular/simple/package.json +++ b/examples/angular/simple/package.json @@ -1,6 +1,6 @@ { - "name": "simple", - "version": "0.0.0", + "name": "@tanstack/query-example-angular-simple", + "type": "module", "scripts": { "ng": "ng", "start": "ng serve", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6c6b834040..0d23a3f537 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -174,43 +174,6 @@ importers: specifier: 5.2.2 version: 5.2.2 - examples/angular/default-query-function: - dependencies: - '@angular/common': - specifier: ^17.1.3 - version: 17.1.3(@angular/core@17.1.3)(rxjs@7.8.1) - '@angular/compiler': - specifier: ^17.1.3 - version: 17.1.3(@angular/core@17.1.3) - '@angular/core': - specifier: ^17.1.3 - version: 17.1.3(rxjs@7.8.1)(zone.js@0.14.3) - '@angular/platform-browser': - specifier: ^17.1.3 - version: 17.1.3(@angular/animations@17.1.3)(@angular/common@17.1.3)(@angular/core@17.1.3) - '@tanstack/angular-query-devtools-experimental': - specifier: ^5.20.2 - version: link:../../../packages/angular-query-devtools-experimental - '@tanstack/angular-query-experimental': - specifier: ^5.20.2 - version: link:../../../packages/angular-query-experimental - axios: - specifier: ^1.6.7 - version: 1.6.7 - rxjs: - specifier: ^7.8.1 - version: 7.8.1 - zone.js: - specifier: ^0.14.3 - version: 0.14.3 - devDependencies: - typescript: - specifier: 5.2.2 - version: 5.2.2 - vite: - specifier: ^5.1.1 - version: 5.1.1(@types/node@18.19.3) - examples/angular/infinite-query-with-max-pages: dependencies: '@angular/animations':