Skip to content

Commit

Permalink
Merge pull request TanStack#1 from eneajaho/feature/angular-fixes
Browse files Browse the repository at this point in the history
feat: update angular version, code DX, examples and tests
  • Loading branch information
arnoud-dv authored Nov 13, 2023
2 parents 8f35de3 + 0c2a6ef commit e13d286
Show file tree
Hide file tree
Showing 58 changed files with 1,837 additions and 1,273 deletions.
31 changes: 19 additions & 12 deletions docs/angular/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ We are in the process of getting to a stable API for Angular Query. If you have
The adapter works with signals, which means it only supports Angular 16+

## Example

```typescript
import { AngularQueryDevtoolsComponent } from '@tanstack/angular-query-devtools-experimental'
import { ChangeDetectionStrategy, Component } from '@angular/core'
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { CommonModule } from '@angular/common'
import { injectQuery } from '@tanstack/angular-query-experimental'
import axios from 'axios'
import { lastValueFrom } from 'rxjs'

type Response = {
name: string
Expand All @@ -34,28 +36,33 @@ type Response = {
selector: 'simple-example',
standalone: true,
template: `
<ng-container *ngIf="query().isPending">Loading...</ng-container>
<ng-container *ngIf="query().error as error">
An error has occurred: {{ error?.message }}
</ng-container>
<div *ngIf="query().data as data">
@if (query.isPending()) {
Loading...
}
@if (query.error()) {
An error has occurred: {{ query.error().message }}
}
@if (query.data(); as data) {
<h1>{{ data.name }}</h1>
<p>{{ data.description }}</p>
<strong>👀 {{ data.subscribers_count }}</strong>
<strong>✨ {{ data.stargazers_count }}</strong>
<strong>🍴 {{ data.forks_count }}</strong>
</div>
}
<angular-query-devtools initialIsOpen />
`,
imports: [AngularQueryDevtoolsComponent, CommonModule],
imports: [AngularQueryDevtoolsComponent],
})
export class SimpleExampleComponent {
http = inject(HttpClient)

query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
axios
.get('https://api.github.com/repos/tannerlinsley/react-query')
.then((res) => res.data as Response),
lastValueFrom(
this.http.get<Response>('https://api.github.com/repos/tannerlinsley/react-query')
),
}))
}
```
20 changes: 9 additions & 11 deletions docs/angular/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,15 @@ import { getTodos, postTodo } from '../my-api'

@Component({
standalone: true,
imports: [CommonModule],
template: `
<div>
<ul>
<li *ngFor="let todo of query().data">
{{ todo.title }}
</li>
@for (todo of query().data) {
<li>{{ todo.title }}</li>
}
</ul>
<button
(click)="onAddTodo()"
>
Add Todo
</button>
<button (click)="onAddTodo()">Add Todo</button>
</div>
`,
})
Expand All @@ -54,10 +49,13 @@ export class TodosComponent {
queryFn: getTodos
}))

mutation = injectMutation(() => ({
mutation = injectMutation((client) => ({
mutationFn: postTodo,
onSuccess: () => {
// Invalidate and refetch
// Invalidate and refetch by using the client directly
client.invalidateQueries({ queryKey: ['todos'] })

// OR use the queryClient that is injected into the component
this.queryClient.invalidateQueries({ queryKey: ['todos'] })
}
}))
Expand Down
9 changes: 4 additions & 5 deletions examples/angular/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
},
"license": "MIT",
"dependencies": {
"@angular/common": "^16.2.11",
"@angular/compiler": "^16.2.12",
"@angular/core": "^16.2.11",
"@angular/platform-browser": "^16.2.12",
"@angular/common": "^17.0.2",
"@angular/compiler": "^17.0.2",
"@angular/core": "^17.0.2",
"@angular/platform-browser": "^17.0.2",
"@tanstack/angular-query-experimental": "^5.4.3",
"@tanstack/angular-query-devtools-experimental": "^5.4.3",
"axios": "^1.4.0",
"rxjs": "^7.4.0",
"zone.js": "^0.14.0"
},
Expand Down
12 changes: 9 additions & 3 deletions examples/angular/simple/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
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 {
QueryClient,
provideAngularQuery,
} from '@tanstack/angular-query-experimental'
import { provideHttpClient, withFetch } from '@angular/common/http'
import { SimpleExampleComponent } from './simple-example.component'

bootstrapApplication(SimpleExampleComponent, {
providers: [provideAngularQuery(new QueryClient())],
providers: [
provideHttpClient(withFetch()),
provideAngularQuery(new QueryClient()),
],
})
28 changes: 17 additions & 11 deletions examples/angular/simple/src/simple-example.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AngularQueryDevtoolsComponent } from '@tanstack/angular-query-devtools-experimental'
import { ChangeDetectionStrategy, Component } from '@angular/core'
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
import { CommonModule } from '@angular/common'
import { injectQuery } from '@tanstack/angular-query-experimental'
import axios from 'axios'
import { HttpClient } from '@angular/common/http'
import { lastValueFrom } from 'rxjs'

type Response = {
name: string
Expand All @@ -17,27 +18,32 @@ type Response = {
selector: 'simple-example',
standalone: true,
template: `
<ng-container *ngIf="query().isPending">Loading...</ng-container>
<ng-container *ngIf="query().error as error">
An error has occurred: {{ error?.message }}
</ng-container>
<div *ngIf="query().data as data">
@if (query.isPending()) {
Loading...
}
@if (query.error()) {
An error has occurred: {{ query.error().message }}
}
@if (query.data(); as data) {
<h1>{{ data.name }}</h1>
<p>{{ data.description }}</p>
<strong>👀 {{ data.subscribers_count }}</strong>
<strong>✨ {{ data.stargazers_count }}</strong>
<strong>🍴 {{ data.forks_count }}</strong>
</div>
}
<angular-query-devtools initialIsOpen />
`,
imports: [AngularQueryDevtoolsComponent, CommonModule],
})
export class SimpleExampleComponent {
http = inject(HttpClient)

query = injectQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
axios
.get('https://api.github.com/repos/tannerlinsley/react-query')
.then((res) => res.data as Response),
lastValueFrom(
this.http.get<Response>('https://api.github.com/repos/tannerlinsley/react-query')
),
}))
}
42 changes: 42 additions & 0 deletions integrations/angular-cli-standalone-16/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# Compiled output
/dist
/tmp
/out-tsc
/bazel-out

# Node
/node_modules
npm-debug.log
yarn-error.log

# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*

# Miscellaneous
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings

# System files
.DS_Store
Thumbs.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=827846
"recommendations": ["angular.ng-template"]
}
13 changes: 13 additions & 0 deletions integrations/angular-cli-standalone-16/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "ng serve",
"type": "chrome",
"request": "launch",
"preLaunchTask": "npm: start",
"url": "http://localhost:4200/"
}
]
}
24 changes: 24 additions & 0 deletions integrations/angular-cli-standalone-16/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// For more information, visit: https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "start",
"isBackground": true,
"problemMatcher": {
"owner": "typescript",
"pattern": "$tsc",
"background": {
"activeOnStart": true,
"beginsPattern": {
"regexp": "(.*?)"
},
"endsPattern": {
"regexp": "bundle generation complete"
}
}
}
}
]
}
27 changes: 27 additions & 0 deletions integrations/angular-cli-standalone-16/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# AngularCliStandalone16

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 16.2.9.

## Development server

Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The application will automatically reload if you change any of the source files.

## Code scaffolding

Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.

## Build

Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory.

## Running unit tests

Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).

## Running end-to-end tests

Run `ng e2e` to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.

## Further help

To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page.
Loading

0 comments on commit e13d286

Please sign in to comment.