-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: resolve projection of Answer:1
- Loading branch information
Showing
7 changed files
with
159 additions
and
102 deletions.
There are no files selected for viewing
47 changes: 42 additions & 5 deletions
47
apps/angular/1-projection/src/app/component/city-card/city-card.component.ts
This file contains 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,13 +1,50 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Component, OnInit, Signal } 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 { City } from '../../model/city.model'; | ||
import { CardComponent } from '../../ui/card/card.component'; | ||
import { ListItemTemplateDirective } from '../../ui/list-item/list-item-template.directive'; | ||
import { ListItemComponent } from '../../ui/list-item/list-item.component'; | ||
|
||
@Component({ | ||
selector: 'app-city-card', | ||
template: 'TODO City', | ||
template: ` | ||
<app-card [list]="cities()" (add)="addCity()"> | ||
<img src="assets/img/city.png" width="200px" alt="student" /> | ||
<ng-template appListItemTemplate let-city> | ||
<app-list-item (deleteItem)="deleteCity(city.id)"> | ||
{{ city.name }} | ||
</app-list-item> | ||
</ng-template> | ||
</app-card> | ||
`, | ||
standalone: true, | ||
imports: [], | ||
imports: [CardComponent, ListItemTemplateDirective, ListItemComponent], | ||
}) | ||
export class CityCardComponent implements OnInit { | ||
constructor() {} | ||
cities: Signal<City[]> = toSignal(this.store.cities$, { | ||
initialValue: [], | ||
}); | ||
|
||
constructor( | ||
private http: FakeHttpService, | ||
private store: CityStore, | ||
) {} | ||
|
||
ngOnInit(): void { | ||
this.http.fetchCities$.subscribe((s) => this.store.addAll(s)); | ||
} | ||
|
||
addCity() { | ||
this.store.addOne(randomCity()); | ||
} | ||
|
||
ngOnInit(): void {} | ||
deleteCity(id: number) { | ||
this.store.deleteOne(id); | ||
} | ||
} |
This file contains 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 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 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
77 changes: 32 additions & 45 deletions
77
apps/angular/1-projection/src/app/ui/card/card.component.ts
This file contains 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,61 +1,48 @@ | ||
import { NgFor, NgIf } from '@angular/common'; | ||
import { Component, Input } 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 { NgTemplateOutlet } from '@angular/common'; | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
contentChild, | ||
input, | ||
output, | ||
} from '@angular/core'; | ||
import { ListItemTemplateDirective } from '../list-item/list-item-template.directive'; | ||
import { ListItemComponent } from '../list-item/list-item.component'; | ||
|
||
@Component({ | ||
selector: 'app-card', | ||
template: ` | ||
<div | ||
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4" | ||
[class]="customClass"> | ||
<img | ||
*ngIf="type === CardType.TEACHER" | ||
src="assets/img/teacher.png" | ||
width="200px" /> | ||
<img | ||
*ngIf="type === CardType.STUDENT" | ||
src="assets/img/student.webp" | ||
width="200px" /> | ||
<ng-content select="img" /> | ||
<section> | ||
<app-list-item | ||
*ngFor="let item of list" | ||
[name]="item.firstName" | ||
[id]="item.id" | ||
[type]="type"></app-list-item> | ||
</section> | ||
<section> | ||
@for (item of list(); track item) { | ||
<ng-template | ||
[ngTemplateOutlet]="itemTemplate()?.templateRef ?? null" | ||
[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)="addNewItem()"> | ||
Add | ||
</button> | ||
`, | ||
standalone: true, | ||
imports: [NgIf, NgFor, ListItemComponent], | ||
imports: [ListItemComponent, NgTemplateOutlet], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
host: { | ||
class: 'flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4', | ||
}, | ||
}) | ||
export class CardComponent { | ||
@Input() list: any[] | null = null; | ||
@Input() type!: CardType; | ||
@Input() customClass = ''; | ||
export class CardComponent<T> { | ||
list = input<T[]>([]); | ||
|
||
CardType = CardType; | ||
itemTemplate = contentChild(ListItemTemplateDirective); | ||
|
||
constructor( | ||
private teacherStore: TeacherStore, | ||
private studentStore: StudentStore, | ||
) {} | ||
add = output(); | ||
|
||
addNewItem() { | ||
if (this.type === CardType.TEACHER) { | ||
this.teacherStore.addOne(randTeacher()); | ||
} else if (this.type === CardType.STUDENT) { | ||
this.studentStore.addOne(randStudent()); | ||
} | ||
this.add.emit(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
apps/angular/1-projection/src/app/ui/list-item/list-item-template.directive.ts
This file contains 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,9 @@ | ||
import { Directive, TemplateRef } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[appListItemTemplate]', | ||
standalone: true, | ||
}) | ||
export class ListItemTemplateDirective { | ||
constructor(public templateRef: TemplateRef<unknown>) {} | ||
} |
37 changes: 11 additions & 26 deletions
37
apps/angular/1-projection/src/app/ui/list-item/list-item.component.ts
This file contains 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,35 +1,20 @@ | ||
import { Component, 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="border-grey-300 flex justify-between border px-2 py-1"> | ||
{{ name }} | ||
<button (click)="delete(id)"> | ||
<img class="h-5" src="assets/svg/trash.svg" /> | ||
</button> | ||
</div> | ||
<ng-content /> | ||
<button (click)="deleteItem.emit()"> | ||
<img class="h-5" src="assets/svg/trash.svg" alt="delete" /> | ||
</button> | ||
`, | ||
standalone: true, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
host: { | ||
class: 'border-grey-300 flex justify-between border px-2 py-1', | ||
}, | ||
}) | ||
export class ListItemComponent { | ||
@Input() id!: number; | ||
@Input() name!: string; | ||
@Input() type!: CardType; | ||
|
||
constructor( | ||
private teacherStore: TeacherStore, | ||
private studentStore: StudentStore, | ||
) {} | ||
|
||
delete(id: number) { | ||
if (this.type === CardType.TEACHER) { | ||
this.teacherStore.deleteOne(id); | ||
} else if (this.type === CardType.STUDENT) { | ||
this.studentStore.deleteOne(id); | ||
} | ||
} | ||
deleteItem = output(); | ||
} |