-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Answer:1 Projection #1556
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
Open
grzegorz555
wants to merge
9
commits into
tomalaforge:main
Choose a base branch
from
grzegorz555:answer-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Answer:1 Projection #1556
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf5c09e
feat(1-projection): project card image via ng-content instead of type…
grzegorz555 436edc4
feat(1-projection): replace CardComponent's type-based add logic with…
grzegorz555 d9e8088
feat(1-projection): replace ListItemComponent's type-based delete log…
grzegorz555 c57610b
feat(1-projection): project list item template via NgTemplateOutlet +…
grzegorz555 f11baeb
refactor(1-projection): replace ::ng-deep background hack with a CSS …
grzegorz555 1854815
feat(1-projection): implement CityCardComponent without touching Card…
grzegorz555 b55c600
refactor(1-projection): fetch data via toSignal + effect instead of n…
grzegorz555 a40883b
fix(1-projection): merge fetched entities instead of overwriting store
grzegorz555 30d0215
fix(1-projection): avoid effect reschedule loop in store addAll
grzegorz555 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 62 additions & 4 deletions
66
apps/angular/1-projection/src/app/component/city-card/city-card.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,67 @@ | ||
| import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
| import { NgOptimizedImage } from '@angular/common'; | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| effect, | ||
| inject, | ||
| } from '@angular/core'; | ||
| import { toSignal } from '@angular/core/rxjs-interop'; | ||
| import { CityStore } from '../../data-access/city.store'; | ||
| import { | ||
| FakeHttpService, | ||
| randomCity, | ||
| } from '../../data-access/fake-http.service'; | ||
| import { CardItemDirective } from '../../directive/card-item.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 | ||
| [list]="cities()" | ||
| (add)="addCity()" | ||
| [style.--bg]="'rgba(250, 150, 0, 0.1)'"> | ||
| <ng-template appCardItem let-item> | ||
| <app-list-item | ||
| [id]="item.id" | ||
| [name]="item.name" | ||
| (delete)="deleteCity($event)" /> | ||
| </ng-template> | ||
| <ng-container card-image> | ||
| <img ngSrc="assets/img/city.png" width="200" height="200" alt="" /> | ||
| </ng-container> | ||
| </app-card> | ||
| `, | ||
| imports: [ | ||
| CardComponent, | ||
| ListItemComponent, | ||
| CardItemDirective, | ||
| NgOptimizedImage, | ||
| ], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class CityCardComponent {} | ||
| export class CityCardComponent { | ||
| private http = inject(FakeHttpService); | ||
| private store = inject(CityStore); | ||
|
|
||
| private fetchedCities = toSignal(this.http.fetchCities$); | ||
| cities = this.store.cities; | ||
|
|
||
| constructor() { | ||
| effect(() => { | ||
| const cities = this.fetchedCities(); | ||
| if (cities) { | ||
| this.store.addAll(cities); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| addCity(): void { | ||
| this.store.addOne(randomCity()); | ||
| } | ||
|
|
||
| deleteCity(id: number): void { | ||
| this.store.deleteOne(id); | ||
| } | ||
| } | ||
59 changes: 43 additions & 16 deletions
59
apps/angular/1-projection/src/app/component/student-card/student-card.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,67 @@ | ||
| import { NgOptimizedImage } from '@angular/common'; | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| effect, | ||
| inject, | ||
| OnInit, | ||
| } from '@angular/core'; | ||
| import { FakeHttpService } from '../../data-access/fake-http.service'; | ||
| import { toSignal } from '@angular/core/rxjs-interop'; | ||
| import { | ||
| FakeHttpService, | ||
| randStudent, | ||
| } from '../../data-access/fake-http.service'; | ||
| import { StudentStore } from '../../data-access/student.store'; | ||
| import { CardType } from '../../model/card.model'; | ||
| import { CardItemDirective } from '../../directive/card-item.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" /> | ||
| (add)="addStudent()" | ||
| [style.--bg]="'rgba(0, 250, 0, 0.1)'"> | ||
| <ng-template appCardItem let-item> | ||
| <app-list-item | ||
| [id]="item.id" | ||
| [name]="item.firstName" | ||
| (delete)="deleteStudent($event)" /> | ||
| </ng-template> | ||
| <ng-container card-image> | ||
| <img ngSrc="assets/img/student.webp" width="200" height="200" alt="" /> | ||
| </ng-container> | ||
| </app-card> | ||
| `, | ||
| styles: [ | ||
| ` | ||
| ::ng-deep .bg-light-green { | ||
| background-color: rgba(0, 250, 0, 0.1); | ||
| } | ||
| `, | ||
| imports: [ | ||
| CardComponent, | ||
| ListItemComponent, | ||
| CardItemDirective, | ||
| NgOptimizedImage, | ||
| ], | ||
| imports: [CardComponent], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class StudentCardComponent implements OnInit { | ||
| export class StudentCardComponent { | ||
| private http = inject(FakeHttpService); | ||
| private store = inject(StudentStore); | ||
|
|
||
| private fetchedStudents = toSignal(this.http.fetchStudents$); | ||
| students = this.store.students; | ||
| cardType = CardType.STUDENT; | ||
|
|
||
| ngOnInit(): void { | ||
| this.http.fetchStudents$.subscribe((s) => this.store.addAll(s)); | ||
| constructor() { | ||
| effect(() => { | ||
| const students = this.fetchedStudents(); | ||
| if (students) { | ||
| this.store.addAll(students); | ||
| } | ||
| }); | ||
|
grzegorz555 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| addStudent(): void { | ||
| this.store.addOne(randStudent()); | ||
| } | ||
|
|
||
| deleteStudent(id: number): void { | ||
| this.store.deleteOne(id); | ||
| } | ||
| } | ||
62 changes: 44 additions & 18 deletions
62
apps/angular/1-projection/src/app/component/teacher-card/teacher-card.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,66 @@ | ||
| import { NgOptimizedImage } from '@angular/common'; | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| effect, | ||
| inject, | ||
| OnInit, | ||
| } from '@angular/core'; | ||
| import { FakeHttpService } from '../../data-access/fake-http.service'; | ||
| import { toSignal } from '@angular/core/rxjs-interop'; | ||
| import { | ||
| FakeHttpService, | ||
| randTeacher, | ||
| } from '../../data-access/fake-http.service'; | ||
| import { TeacherStore } from '../../data-access/teacher.store'; | ||
| import { CardType } from '../../model/card.model'; | ||
| import { CardItemDirective } from '../../directive/card-item.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> | ||
| (add)="addTeacher()" | ||
| [style.--bg]="'rgba(250, 0, 0, 0.1)'"> | ||
| <ng-template appCardItem let-item> | ||
| <app-list-item | ||
| [id]="item.id" | ||
| [name]="item.firstName" | ||
| (delete)="deleteTeacher($event)" /> | ||
| </ng-template> | ||
| <ng-container card-image> | ||
| <img ngSrc="assets/img/teacher.png" width="200" height="200" alt="" /> | ||
| </ng-container> | ||
| </app-card> | ||
| `, | ||
| styles: [ | ||
| ` | ||
| ::ng-deep .bg-light-red { | ||
| background-color: rgba(250, 0, 0, 0.1); | ||
| } | ||
| `, | ||
| ], | ||
| changeDetection: ChangeDetectionStrategy.Eager, | ||
| imports: [CardComponent], | ||
| imports: [ | ||
| CardComponent, | ||
| ListItemComponent, | ||
| CardItemDirective, | ||
| NgOptimizedImage, | ||
| ], | ||
| }) | ||
| export class TeacherCardComponent implements OnInit { | ||
| export class TeacherCardComponent { | ||
| private http = inject(FakeHttpService); | ||
| private store = inject(TeacherStore); | ||
|
|
||
| private fetchedTeachers = toSignal(this.http.fetchTeachers$); | ||
| teachers = this.store.teachers; | ||
| cardType = CardType.TEACHER; | ||
|
|
||
| ngOnInit(): void { | ||
| this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t)); | ||
| constructor() { | ||
| effect(() => { | ||
| const teachers = this.fetchedTeachers(); | ||
| if (teachers) { | ||
| this.store.addAll(teachers); | ||
| } | ||
| }); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| addTeacher(): void { | ||
| this.store.addOne(randTeacher()); | ||
| } | ||
|
|
||
| deleteTeacher(id: number): void { | ||
| this.store.deleteOne(id); | ||
| } | ||
| } | ||
10 changes: 7 additions & 3 deletions
10
apps/angular/1-projection/src/app/data-access/city.store.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
apps/angular/1-projection/src/app/directive/card-item.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { Directive, inject, TemplateRef } from '@angular/core'; | ||
|
|
||
| @Directive({ | ||
| selector: '[appCardItem]', | ||
| }) | ||
| export class CardItemDirective { | ||
| templateRef = inject(TemplateRef); | ||
| } |
58 changes: 20 additions & 38 deletions
58
apps/angular/1-projection/src/app/ui/card/card.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,46 @@ | ||
| import { NgOptimizedImage } from '@angular/common'; | ||
| import { CommonModule } from '@angular/common'; | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| inject, | ||
| contentChild, | ||
| input, | ||
| output, | ||
| } 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 { CardItemDirective } from '../../directive/card-item.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="" /> | ||
| } | ||
|
|
||
| class="card-container flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"> | ||
| <ng-content select="[card-image]"></ng-content> | ||
| <section> | ||
| @for (item of list(); track item) { | ||
| <app-list-item | ||
| [name]="item.firstName" | ||
| [id]="item.id" | ||
| [type]="type()"></app-list-item> | ||
| <ng-container | ||
| [ngTemplateOutlet]="itemTemplate().templateRef" | ||
| [ngTemplateOutletContext]="{ $implicit: item }"></ng-container> | ||
| } | ||
| </section> | ||
|
|
||
| <button | ||
| class="rounded-sm border border-blue-500 bg-blue-300 p-2" | ||
| (click)="addNewItem()"> | ||
| (click)="add.emit()"> | ||
| Add | ||
| </button> | ||
| </div> | ||
| `, | ||
| styles: [ | ||
| ` | ||
| .card-container { | ||
| background: var(--bg, gray); | ||
| } | ||
| `, | ||
| ], | ||
| changeDetection: ChangeDetectionStrategy.Eager, | ||
| imports: [ListItemComponent, NgOptimizedImage], | ||
| imports: [CommonModule], | ||
| }) | ||
| 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(''); | ||
|
|
||
| CardType = CardType; | ||
|
|
||
| addNewItem() { | ||
| const type = this.type(); | ||
| if (type === CardType.TEACHER) { | ||
| this.teacherStore.addOne(randTeacher()); | ||
| } else if (type === CardType.STUDENT) { | ||
| this.studentStore.addOne(randStudent()); | ||
| } | ||
| } | ||
| readonly itemTemplate = contentChild.required(CardItemDirective); | ||
| add = output<void>(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.