|
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 { Component, ContentChild, input, TemplateRef } from '@angular/core'; |
8 | 3 |
|
9 | 4 | @Component({ |
10 | 5 | selector: 'app-card', |
11 | 6 | template: ` |
12 | | - <div |
13 | | - class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4" |
14 | | - [class]="customClass()"> |
15 | | - @if (type() === CardType.TEACHER) { |
16 | | - <img ngSrc="assets/img/teacher.png" width="200" height="200" /> |
| 7 | + <ng-content select="img" /> |
| 8 | + <section> |
| 9 | + @for (item of list(); track item) { |
| 10 | + <ng-container |
| 11 | + *ngTemplateOutlet=" |
| 12 | + rowRef; |
| 13 | + context: { $implicit: item } |
| 14 | + "></ng-container> |
17 | 15 | } |
18 | | - @if (type() === CardType.STUDENT) { |
19 | | - <img ngSrc="assets/img/student.webp" width="200" height="200" /> |
20 | | - } |
21 | | -
|
22 | | - <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> |
28 | | - } |
29 | | - </section> |
30 | | -
|
31 | | - <button |
32 | | - class="rounded-sm border border-blue-500 bg-blue-300 p-2" |
33 | | - (click)="addNewItem()"> |
34 | | - Add |
35 | | - </button> |
36 | | - </div> |
| 16 | + </section> |
| 17 | + <ng-content /> |
37 | 18 | `, |
38 | | - imports: [ListItemComponent, NgOptimizedImage], |
| 19 | + imports: [NgTemplateOutlet], |
| 20 | + host: { |
| 21 | + class: 'flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4 ', |
| 22 | + }, |
39 | 23 | }) |
40 | 24 | export class CardComponent { |
41 | | - private teacherStore = inject(TeacherStore); |
42 | | - private studentStore = inject(StudentStore); |
43 | | - |
| 25 | + @ContentChild('rowRef') rowRef!: TemplateRef<any>; |
44 | 26 | readonly list = input<any[] | null>(null); |
45 | | - readonly type = input.required<CardType>(); |
46 | 27 | readonly customClass = input(''); |
47 | | - |
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 | | - } |
57 | | - } |
58 | 28 | } |
0 commit comments