|
1 | | -import { NgOptimizedImage } from '@angular/common'; |
2 | | -import { Component, inject, input } from '@angular/core'; |
3 | | -import { randStudent, randTeacher } from '../../data-access/fake-http.service'; |
4 | | -import { StudentStore } from '../../data-access/student.store'; |
5 | | -import { TeacherStore } from '../../data-access/teacher.store'; |
6 | | -import { CardType } from '../../model/card.model'; |
7 | | -import { ListItemComponent } from '../list-item/list-item.component'; |
| 1 | +import { NgTemplateOutlet } from '@angular/common'; |
| 2 | +import { |
| 3 | + Component, |
| 4 | + contentChild, |
| 5 | + input, |
| 6 | + output, |
| 7 | + TemplateRef, |
| 8 | +} from '@angular/core'; |
| 9 | +import { CardItemDirective } from '../../card-item'; |
8 | 10 |
|
9 | 11 | @Component({ |
10 | 12 | selector: 'app-card', |
| 13 | + standalone: true, |
11 | 14 | template: ` |
12 | 15 | <div |
13 | 16 | class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4" |
14 | 17 | [class]="customClass()"> |
15 | | - @if (type() === CardType.TEACHER) { |
16 | | - <img ngSrc="assets/img/teacher.png" width="200" height="200" /> |
17 | | - } |
18 | | - @if (type() === CardType.STUDENT) { |
19 | | - <img ngSrc="assets/img/student.webp" width="200" height="200" /> |
20 | | - } |
| 18 | + <ng-content></ng-content> |
21 | 19 |
|
22 | 20 | <section> |
23 | | - @for (item of list(); track item) { |
24 | | - <app-list-item |
25 | | - [name]="item.firstName" |
26 | | - [id]="item.id" |
27 | | - [type]="type()"></app-list-item> |
| 21 | + @for (item of list(); track item.id) { |
| 22 | + <ng-container |
| 23 | + *ngTemplateOutlet=" |
| 24 | + itemTemplate(); |
| 25 | + context: { $implicit: item } |
| 26 | + "></ng-container> |
28 | 27 | } |
29 | 28 | </section> |
30 | 29 |
|
31 | 30 | <button |
32 | 31 | class="rounded-sm border border-blue-500 bg-blue-300 p-2" |
33 | | - (click)="addNewItem()"> |
| 32 | + (click)="onAddClicked()"> |
34 | 33 | Add |
35 | 34 | </button> |
36 | 35 | </div> |
37 | 36 | `, |
38 | | - imports: [ListItemComponent, NgOptimizedImage], |
| 37 | + imports: [NgTemplateOutlet], |
39 | 38 | }) |
40 | 39 | export class CardComponent { |
41 | | - private teacherStore = inject(TeacherStore); |
42 | | - private studentStore = inject(StudentStore); |
43 | | - |
44 | | - readonly list = input<any[] | null>(null); |
45 | | - readonly type = input.required<CardType>(); |
| 40 | + readonly list = input<any[]>(); |
| 41 | + readonly itemTemplate = contentChild.required(CardItemDirective, { |
| 42 | + read: TemplateRef, |
| 43 | + }); |
46 | 44 | readonly customClass = input(''); |
47 | 45 |
|
48 | | - CardType = CardType; |
49 | | - |
50 | | - addNewItem() { |
51 | | - const type = this.type(); |
52 | | - if (type === CardType.TEACHER) { |
53 | | - this.teacherStore.addOne(randTeacher()); |
54 | | - } else if (type === CardType.STUDENT) { |
55 | | - this.studentStore.addOne(randStudent()); |
56 | | - } |
| 46 | + addClicked = output<void>(); |
| 47 | + onAddClicked() { |
| 48 | + this.addClicked.emit(); |
57 | 49 | } |
58 | 50 | } |
0 commit comments