Skip to content

Commit edbe7d4

Browse files
authored
challenge 51: signal effect trigger too often (tomalaforge#855)
* feat: challenge 51 * fix: permission denied
1 parent a964e4d commit edbe7d4

26 files changed

+362
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ If you would like to propose a challenge, this project is open source, so feel f
2424
2525
## Challenges
2626

27-
Check [all 50 challenges](https://angular-challenges.vercel.app/)
27+
Check [all 51 challenges](https://angular-challenges.vercel.app/)
2828

2929
## Contributors ✨
3030

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"extends": ["../../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts"],
7+
"extends": [
8+
"plugin:@nx/angular",
9+
"plugin:@angular-eslint/template/process-inline-templates"
10+
],
11+
"rules": {
12+
"@angular-eslint/directive-selector": [
13+
"error",
14+
{
15+
"type": "attribute",
16+
"prefix": "app",
17+
"style": "camelCase"
18+
}
19+
],
20+
"@angular-eslint/component-selector": [
21+
"error",
22+
{
23+
"type": "element",
24+
"prefix": "app",
25+
"style": "kebab-case"
26+
}
27+
]
28+
}
29+
},
30+
{
31+
"files": ["*.html"],
32+
"extends": ["plugin:@nx/angular-template"],
33+
"rules": {}
34+
}
35+
]
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Function call in effect
2+
3+
> author: thomas-laforge
4+
5+
### Run Application
6+
7+
```bash
8+
npx nx serve signal-function-call-effect
9+
```
10+
11+
### Documentation and Instruction
12+
13+
Challenge documentation is [here](https://angular-challenges.vercel.app/challenges/angular/51-function-call-effect/).
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"name": "signal-function-call-effect",
3+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
4+
"projectType": "application",
5+
"prefix": "app",
6+
"sourceRoot": "apps/signal/51-function-call-effect/src",
7+
"tags": [],
8+
"targets": {
9+
"build": {
10+
"executor": "@angular-devkit/build-angular:application",
11+
"outputs": ["{options.outputPath}"],
12+
"options": {
13+
"outputPath": "dist/apps/signal/51-function-call-effect",
14+
"index": "apps/signal/51-function-call-effect/src/index.html",
15+
"browser": "apps/signal/51-function-call-effect/src/main.ts",
16+
"polyfills": ["zone.js"],
17+
"tsConfig": "apps/signal/51-function-call-effect/tsconfig.app.json",
18+
"inlineStyleLanguage": "scss",
19+
"assets": [
20+
"apps/signal/51-function-call-effect/src/favicon.ico",
21+
"apps/signal/51-function-call-effect/src/assets"
22+
],
23+
"styles": ["apps/signal/51-function-call-effect/src/styles.scss"],
24+
"scripts": []
25+
},
26+
"configurations": {
27+
"production": {
28+
"budgets": [
29+
{
30+
"type": "initial",
31+
"maximumWarning": "500kb",
32+
"maximumError": "1mb"
33+
},
34+
{
35+
"type": "anyComponentStyle",
36+
"maximumWarning": "2kb",
37+
"maximumError": "4kb"
38+
}
39+
],
40+
"outputHashing": "all"
41+
},
42+
"development": {
43+
"optimization": false,
44+
"extractLicenses": false,
45+
"sourceMap": true
46+
}
47+
},
48+
"defaultConfiguration": "production"
49+
},
50+
"serve": {
51+
"executor": "@angular-devkit/build-angular:dev-server",
52+
"configurations": {
53+
"production": {
54+
"buildTarget": "signal-function-call-effect:build:production"
55+
},
56+
"development": {
57+
"buildTarget": "signal-function-call-effect:build:development"
58+
}
59+
},
60+
"defaultConfiguration": "development"
61+
},
62+
"extract-i18n": {
63+
"executor": "@angular-devkit/build-angular:extract-i18n",
64+
"options": {
65+
"buildTarget": "signal-function-call-effect:build"
66+
}
67+
},
68+
"lint": {
69+
"executor": "@nx/eslint:lint"
70+
}
71+
}
72+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {
2+
ChangeDetectionStrategy,
3+
Component,
4+
effect,
5+
inject,
6+
signal,
7+
} from '@angular/core';
8+
import { FormsModule } from '@angular/forms';
9+
import { UserService } from './user.service';
10+
11+
@Component({
12+
standalone: true,
13+
imports: [FormsModule],
14+
selector: 'app-actions',
15+
template: `
16+
<form class="mx-auto max-w-sm">
17+
<label for="actions" class="mb-2 block text-sm font-medium text-gray-900">
18+
Choose an action
19+
</label>
20+
<select
21+
name="actions"
22+
[(ngModel)]="action"
23+
id="actions"
24+
class="block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500">
25+
<option selected>Please select an action</option>
26+
@for (action of actions; track $index) {
27+
<option value="{{ action }}">{{ action }}</option>
28+
}
29+
</select>
30+
</form>
31+
`,
32+
changeDetection: ChangeDetectionStrategy.OnPush,
33+
})
34+
export class ActionsComponent {
35+
private userService = inject(UserService);
36+
protected action = signal(undefined);
37+
38+
protected actions = ['Create', 'Read', 'Update', 'Delete'];
39+
40+
constructor() {
41+
effect(() => {
42+
this.userService.log(this.action() ?? 'No action selected');
43+
});
44+
}
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
2+
import { FormsModule } from '@angular/forms';
3+
import { ActionsComponent } from './action.component';
4+
import { UserService } from './user.service';
5+
6+
@Component({
7+
standalone: true,
8+
imports: [FormsModule, ActionsComponent],
9+
selector: 'app-root',
10+
template: `
11+
<nav class="flex w-full items-center border border-b">
12+
Profile selected:
13+
<form class="m-4 w-48">
14+
<select
15+
[(ngModel)]="userService.name"
16+
name="name"
17+
class="block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500 ">
18+
<option selected>Please choose an user</option>
19+
@for (user of users; track $index) {
20+
<option value="{{ user }}">{{ user }}</option>
21+
}
22+
</select>
23+
</form>
24+
</nav>
25+
26+
<app-actions />
27+
`,
28+
changeDetection: ChangeDetectionStrategy.OnPush,
29+
})
30+
export class AppComponent {
31+
protected userService = inject(UserService);
32+
33+
protected users = ['Thomas', 'John', 'Alice', 'Bob', 'Charlie', 'David'];
34+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ApplicationConfig } from '@angular/core';
2+
3+
export const appConfig: ApplicationConfig = {
4+
providers: [],
5+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Injectable, signal } from '@angular/core';
2+
3+
@Injectable({ providedIn: 'root' })
4+
export class UserService {
5+
name = signal('Thomas');
6+
7+
log(message: string) {
8+
console.log(`${this.name()}: ${message}`);
9+
}
10+
}

apps/signal/51-function-call-effect/src/assets/.gitkeep

Whitespace-only changes.
14.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)