-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Answer: 12 #1538
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: 12 #1538
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,54 @@ | ||
| import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
| import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||
| import { CityStore } from '../../data-access/city.store'; | ||
| import { | ||
| FakeHttpService, | ||
| randomCity, | ||
| } from '../../data-access/fake-http.service'; | ||
| import { CardRowDirective } from '../../ui/card/card-row.directive'; | ||
| import { CardComponent } from '../../ui/card/card.component'; | ||
| import { ListItemComponent } from '../../ui/list-item/list-item.component'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-city-card', | ||
| template: 'TODO City', | ||
| imports: [], | ||
| template: ` | ||
| <app-card | ||
| class="bg-light-blue" | ||
| [items]="cities()" | ||
| (addNew)="handleAddNew()"> | ||
| <img src="assets/img/city.png" alt="city" width="200px" /> | ||
| <ng-template [cardRow]="cities()" let-city> | ||
| <app-list-item (delete)="handleDelete(city.id)"> | ||
| {{ city.name }} | ||
| </app-list-item> | ||
| </ng-template> | ||
| </app-card> | ||
| `, | ||
| styles: [ | ||
| ` | ||
| .bg-light-blue { | ||
| background-color: rgba(0, 0, 250, 0.1); | ||
| } | ||
| `, | ||
| ], | ||
| imports: [CardComponent, ListItemComponent, CardRowDirective], | ||
| standalone: true, | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class CityCardComponent {} | ||
| export class CityCardComponent { | ||
| private http = inject(FakeHttpService); | ||
| private store = inject(CityStore); | ||
|
|
||
| cities = this.store.cities; | ||
|
|
||
| constructor() { | ||
| this.http.fetchCities$.subscribe(this.store.addAll); | ||
| } | ||
|
|
||
| handleAddNew(): void { | ||
| this.store.addOne(randomCity()); | ||
| } | ||
|
|
||
| handleDelete(id: number): void { | ||
| this.store.deleteOne(id); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,55 @@ | ||
| import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| inject, | ||
| OnInit, | ||
| } from '@angular/core'; | ||
| import { FakeHttpService } from '../../data-access/fake-http.service'; | ||
| FakeHttpService, | ||
| randStudent, | ||
| } from '../../data-access/fake-http.service'; | ||
| import { StudentStore } from '../../data-access/student.store'; | ||
| import { CardType } from '../../model/card.model'; | ||
| import { CardRowDirective } from '../../ui/card/card-row.directive'; | ||
| import { CardComponent } from '../../ui/card/card.component'; | ||
| import { ListItemComponent } from '../../ui/list-item/list-item.component'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-student-card', | ||
| template: ` | ||
| <app-card | ||
| [list]="students()" | ||
| [type]="cardType" | ||
| customClass="bg-light-green" /> | ||
| class="bg-light-green" | ||
| [items]="students()" | ||
| (addNew)="handleAddNew()"> | ||
| <img src="assets/img/student.webp" alt="student" width="200px" /> | ||
|
|
||
| <ng-template [cardRow]="students()" let-student> | ||
| <app-list-item (delete)="handleDelete(student.id)"> | ||
| {{ student.firstName }} | ||
| </app-list-item> | ||
| </ng-template> | ||
| </app-card> | ||
| `, | ||
| styles: [ | ||
| ` | ||
| ::ng-deep .bg-light-green { | ||
| .bg-light-green { | ||
| background-color: rgba(0, 250, 0, 0.1); | ||
| } | ||
| `, | ||
| ], | ||
| imports: [CardComponent], | ||
| imports: [CardComponent, CardRowDirective, ListItemComponent], | ||
| standalone: true, | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class StudentCardComponent implements OnInit { | ||
| export class StudentCardComponent { | ||
| private http = inject(FakeHttpService); | ||
| private store = inject(StudentStore); | ||
|
|
||
| students = this.store.students; | ||
| cardType = CardType.STUDENT; | ||
|
|
||
| ngOnInit(): void { | ||
| this.http.fetchStudents$.subscribe((s) => this.store.addAll(s)); | ||
| constructor() { | ||
| this.http.fetchStudents$.subscribe(this.store.addAll); | ||
| } | ||
|
|
||
| handleAddNew(): void { | ||
| this.store.addOne(randStudent()); | ||
| } | ||
|
|
||
| handleDelete(id: number): void { | ||
| this.store.deleteOne(id); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,40 +1,55 @@ | ||||||
| import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||||||
| import { | ||||||
| ChangeDetectionStrategy, | ||||||
| Component, | ||||||
| inject, | ||||||
| OnInit, | ||||||
| } from '@angular/core'; | ||||||
| import { FakeHttpService } from '../../data-access/fake-http.service'; | ||||||
| FakeHttpService, | ||||||
| randTeacher, | ||||||
| } from '../../data-access/fake-http.service'; | ||||||
| import { TeacherStore } from '../../data-access/teacher.store'; | ||||||
| import { CardType } from '../../model/card.model'; | ||||||
| import { CardRowDirective } from '../../ui/card/card-row.directive'; | ||||||
| import { CardComponent } from '../../ui/card/card.component'; | ||||||
| import { ListItemComponent } from '../../ui/list-item/list-item.component'; | ||||||
|
|
||||||
| @Component({ | ||||||
| selector: 'app-teacher-card', | ||||||
| template: ` | ||||||
| <app-card | ||||||
| [list]="teachers()" | ||||||
| [type]="cardType" | ||||||
| customClass="bg-light-red"></app-card> | ||||||
| class="bg-light-red" | ||||||
| [items]="teachers()" | ||||||
| (addNew)="handleAddNew()"> | ||||||
| <img src="assets/img/teacher.png" alt="student" width="200px" /> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Correct the teacher image alternative text.
Proposed fix- <img src="assets/img/teacher.png" alt="student" width="200px" />
+ <img src="assets/img/teacher.png" alt="teacher" width="200px" />📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| <ng-template [cardRow]="teachers()" let-teacher> | ||||||
| <app-list-item (delete)="handleDelete(teacher.id)"> | ||||||
| {{ teacher.firstName }} | ||||||
| </app-list-item> | ||||||
| </ng-template> | ||||||
| </app-card> | ||||||
| `, | ||||||
| styles: [ | ||||||
| ` | ||||||
| ::ng-deep .bg-light-red { | ||||||
| .bg-light-red { | ||||||
| background-color: rgba(250, 0, 0, 0.1); | ||||||
| } | ||||||
| `, | ||||||
| ], | ||||||
| changeDetection: ChangeDetectionStrategy.Eager, | ||||||
| imports: [CardComponent], | ||||||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||||||
| standalone: true, | ||||||
| imports: [CardComponent, CardRowDirective, ListItemComponent], | ||||||
| }) | ||||||
| export class TeacherCardComponent implements OnInit { | ||||||
| export class TeacherCardComponent { | ||||||
| private http = inject(FakeHttpService); | ||||||
| private store = inject(TeacherStore); | ||||||
|
|
||||||
| teachers = this.store.teachers; | ||||||
| cardType = CardType.TEACHER; | ||||||
|
|
||||||
| ngOnInit(): void { | ||||||
| this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t)); | ||||||
| constructor() { | ||||||
| this.http.fetchTeachers$.subscribe(this.store.addAll); | ||||||
| } | ||||||
|
|
||||||
| handleAddNew(): void { | ||||||
| this.store.addOne(randTeacher()); | ||||||
| } | ||||||
|
|
||||||
| handleDelete(id: number): void { | ||||||
| this.store.deleteOne(id); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +0,0 @@ | ||
| export enum CardType { | ||
| TEACHER, | ||
| STUDENT, | ||
| CITY, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Directive, input } from '@angular/core'; | ||
|
|
||
| interface CardRowContext<T> { | ||
| $implicit: T; | ||
| } | ||
|
|
||
| // eslint-disable-next-line @angular-eslint/directive-selector | ||
| @Directive({ selector: 'ng-template[cardRow]', standalone: true }) | ||
| export class CardRowDirective<T> { | ||
| cardRow = input.required<T[]>(); | ||
|
|
||
| static ngTemplateContextGuard<TContext>( | ||
| dir: CardRowDirective<TContext>, | ||
| ctx: unknown, | ||
| ): ctx is CardRowContext<TContext> { | ||
| return true; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,44 @@ | ||
| import { NgOptimizedImage } from '@angular/common'; | ||
| import { NgTemplateOutlet } from '@angular/common'; | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| inject, | ||
| contentChild, | ||
| input, | ||
| output, | ||
| TemplateRef, | ||
| } from '@angular/core'; | ||
| import { randStudent, randTeacher } from '../../data-access/fake-http.service'; | ||
| import { StudentStore } from '../../data-access/student.store'; | ||
| import { TeacherStore } from '../../data-access/teacher.store'; | ||
| import { CardType } from '../../model/card.model'; | ||
| import { ListItemComponent } from '../list-item/list-item.component'; | ||
| import { CardRowDirective } from './card-row.directive'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-card', | ||
| template: ` | ||
| <div | ||
| class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4" | ||
| [class]="customClass()"> | ||
| @if (type() === CardType.TEACHER) { | ||
| <img ngSrc="assets/img/teacher.png" width="200" height="200" alt="" /> | ||
| } | ||
| @if (type() === CardType.STUDENT) { | ||
| <img ngSrc="assets/img/student.webp" width="200" height="200" alt="" /> | ||
| } | ||
| <ng-content select="img" /> | ||
|
|
||
| <section> | ||
| @for (item of list(); track item) { | ||
| <app-list-item | ||
| [name]="item.firstName" | ||
| [id]="item.id" | ||
| [type]="type()"></app-list-item> | ||
| } | ||
| </section> | ||
| <section> | ||
| @for (item of items(); track item.id) { | ||
| <ng-template | ||
| [ngTemplateOutlet]="rowTemplate()" | ||
| [ngTemplateOutletContext]="{ $implicit: item }" /> | ||
| } | ||
| </section> | ||
|
|
||
| <button | ||
| class="rounded-sm border border-blue-500 bg-blue-300 p-2" | ||
| (click)="addNewItem()"> | ||
| Add | ||
| </button> | ||
| </div> | ||
| <button | ||
| class="rounded-sm border border-blue-500 bg-blue-300 p-2" | ||
| (click)="addNew.emit()"> | ||
| Add | ||
| </button> | ||
| `, | ||
| changeDetection: ChangeDetectionStrategy.Eager, | ||
| imports: [ListItemComponent, NgOptimizedImage], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| standalone: true, | ||
| host: { | ||
| class: 'flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4', | ||
| }, | ||
| imports: [NgTemplateOutlet], | ||
| }) | ||
| export class CardComponent { | ||
| private teacherStore = inject(TeacherStore); | ||
| private studentStore = inject(StudentStore); | ||
|
|
||
| readonly list = input<any[] | null>(null); | ||
| readonly type = input.required<CardType>(); | ||
| readonly customClass = input(''); | ||
| export class CardComponent<T extends { id: number }> { | ||
| items = input.required<T[]>(); | ||
|
|
||
| CardType = CardType; | ||
| addNew = output<void>(); | ||
|
|
||
| addNewItem() { | ||
| const type = this.type(); | ||
| if (type === CardType.TEACHER) { | ||
| this.teacherStore.addOne(randTeacher()); | ||
| } else if (type === CardType.STUDENT) { | ||
| this.studentStore.addOne(randStudent()); | ||
| } | ||
| } | ||
| rowTemplate = contentChild.required(CardRowDirective, { read: TemplateRef }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,18 @@ | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| inject, | ||
| input, | ||
| } from '@angular/core'; | ||
| import { StudentStore } from '../../data-access/student.store'; | ||
| import { TeacherStore } from '../../data-access/teacher.store'; | ||
| import { CardType } from '../../model/card.model'; | ||
| import { ChangeDetectionStrategy, Component, output } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-list-item', | ||
| template: ` | ||
| <div class="flex justify-between border border-gray-300 px-2 py-1"> | ||
| {{ name() }} | ||
| <button (click)="delete(id())"> | ||
| <img class="h-5" src="assets/svg/trash.svg" alt="trash" /> | ||
| <ng-content /> | ||
| <button (click)="delete.emit()"> | ||
| <img class="h-5" [src]="'assets/svg/trash.svg'" alt="trash" /> | ||
| </button> | ||
| </div> | ||
| `, | ||
| standalone: true, | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class ListItemComponent { | ||
| private teacherStore = inject(TeacherStore); | ||
| private studentStore = inject(StudentStore); | ||
|
|
||
| readonly id = input.required<number>(); | ||
| readonly name = input.required<string>(); | ||
| readonly type = input.required<CardType>(); | ||
|
|
||
| delete(id: number) { | ||
| const type = this.type(); | ||
| if (type === CardType.TEACHER) { | ||
| this.teacherStore.deleteOne(id); | ||
| } else if (type === CardType.STUDENT) { | ||
| this.studentStore.deleteOne(id); | ||
| } | ||
| } | ||
| readonly delete = output<void>(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: tomalaforge/angular-challenges
Length of output: 3674
Preserve the store receiver in all three subscriptions.
CityStore.addAll,StudentStore.addAll, andTeacherStore.addAlleach accessthisto update their signal. Passing these methods directly tosubscriberemoves the store receiver, so each response can fail before updating the list. Use an arrow callback or bind each method.📍 Affects 3 files
apps/angular/1-projection/src/app/component/city-card/city-card.component.ts#L43-L45(this comment)apps/angular/1-projection/src/app/component/student-card/student-card.component.ts#L44-L46apps/angular/1-projection/src/app/component/teacher-card/teacher-card.component.ts#L44-L46🤖 Prompt for AI Agents