Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer: 43 #1149

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ Thumbs.db
TODO.md
.nx/cache
.nx/workspace-data

.cursorrules
16 changes: 9 additions & 7 deletions apps/signal/43-signal-input/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { JsonPipe } from '@angular/common';
import { Component } from '@angular/core';
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { UserComponent } from './user.component';

@Component({
standalone: true,
imports: [UserComponent, JsonPipe],
imports: [UserComponent],
selector: 'app-root',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="flex flex-col gap-3">
<div class="flex gap-2 ">
Name:
<input #name class="border" />
@if (showUser && !name.value) {

@if (showUser() && !name.value) {
<div class="text-sm text-red-500">name required</div>
}
</div>
Expand All @@ -24,12 +25,13 @@ import { UserComponent } from './user.component';
<input type="number" #age class="border" />
</div>
<button
(click)="showUser = true"
(click)="showUser.set(true)"
class="w-fit rounded-md border border-blue-500 bg-blue-200 px-4 py-2">
Submit
</button>
</div>
@if (showUser && !!name.value) {

@if (showUser() && !!name.value) {
<app-user
[name]="name.value"
[lastName]="lastName.value"
Expand All @@ -41,5 +43,5 @@ import { UserComponent } from './user.component';
},
})
export class AppComponent {
showUser = false;
readonly showUser = signal(false);
}
5 changes: 0 additions & 5 deletions apps/signal/43-signal-input/src/app/app.config.ts

This file was deleted.

8 changes: 8 additions & 0 deletions apps/signal/43-signal-input/src/app/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type User = {
name: string;
lastName?: string;
age?: number;
category?: Category;
};

export type Category = 'Youth' | 'Junior' | 'Open' | 'Senior';
32 changes: 10 additions & 22 deletions apps/signal/43-signal-input/src/app/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,28 @@ import { TitleCasePipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
Input,
OnChanges,
computed,
input,
} from '@angular/core';

type Category = 'Youth' | 'Junior' | 'Open' | 'Senior';
const ageToCategory = (age: number): Category => {
if (age < 10) return 'Youth';
else if (age < 18) return 'Junior';
else if (age < 35) return 'Open';
return 'Senior';
};
import { ageToCategory } from './utils';

@Component({
selector: 'app-user',
standalone: true,
imports: [TitleCasePipe],
template: `
{{ fullName | titlecase }} plays tennis in the {{ category }} category!!
{{ fullName() | titlecase }} plays tennis in the {{ category() }} category!
`,
host: {
class: 'text-xl text-green-800',
},
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserComponent implements OnChanges {
@Input({ required: true }) name!: string;
@Input() lastName?: string;
@Input() age?: string;

fullName = '';
category: Category = 'Junior';
export class UserComponent {
readonly name = input.required<string>();
readonly lastName = input<string>('');
readonly age = input<string>('');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can add a transform method to make it a number with a default value to 0


ngOnChanges(): void {
this.fullName = `${this.name} ${this.lastName ?? ''}`;
this.category = ageToCategory(Number(this.age) ?? 0);
}
readonly fullName = computed(() => `${this.name()} ${this.lastName() ?? ''}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the ?? '', lastName has a default value.

readonly category = computed(() => ageToCategory(Number(this.age()) ?? 0));
}
19 changes: 19 additions & 0 deletions apps/signal/43-signal-input/src/app/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Category } from './models';

export const ageToCategory = (age: number): Category => {
if (age < 0 || !Number.isInteger(age)) {
throw new Error('Age must be a positive integer');
}

const categoryRanges: { [key: number]: Category } = {
10: 'Youth',
18: 'Junior',
35: 'Open',
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not putting this const outside of the scope of the function, so you don't recreate it at each call.


const threshold = Object.keys(categoryRanges)
.map(Number)
.find((limit) => age < limit);

return threshold ? categoryRanges[threshold] : 'Senior';
};
5 changes: 1 addition & 4 deletions apps/signal/43-signal-input/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { appConfig } from './app/app.config';

bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err),
);
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Loading