|
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'; |
| 1 | +import { NgOptimizedImage, NgTemplateOutlet } from '@angular/common'; |
| 2 | +import { Component, input, output } from '@angular/core'; |
6 | 3 | import { CardType } from '../../model/card.model'; |
7 | 4 | import { ListItemComponent } from '../list-item/list-item.component'; |
8 | 5 |
|
9 | 6 | @Component({ |
10 | 7 | selector: 'app-card', |
11 | 8 | 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" /> |
17 | | - } |
18 | | - @if (type() === CardType.STUDENT) { |
19 | | - <img ngSrc="assets/img/student.webp" width="200" height="200" /> |
20 | | - } |
| 9 | + <ng-template #card let-cardTypeVal="cardType"> |
| 10 | + <div |
| 11 | + class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4" |
| 12 | + [class]="customClass()"> |
| 13 | + <img |
| 14 | + ngSrc="assets/img/{{ CardType[cardTypeVal()].toLowerCase() }}.{{ |
| 15 | + this.pictureType() |
| 16 | + }}" |
| 17 | + width="200" |
| 18 | + height="200" |
| 19 | + priority /> |
| 20 | + <section> |
| 21 | + @for (item of list(); track item) { |
| 22 | + <app-list-item |
| 23 | + [name]="item.name" |
| 24 | + [firstName]="item.firstName" |
| 25 | + [lastName]="item.lastName" |
| 26 | + [id]="item.id" |
| 27 | + [type]="type()" |
| 28 | + (deleteItem)="delete($event)" /> |
| 29 | + } |
| 30 | + </section> |
21 | 31 |
|
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> |
| 32 | + <button |
| 33 | + class="rounded-sm border border-blue-500 bg-blue-300 p-2" |
| 34 | + (click)="addItem.emit()"> |
| 35 | + Add |
| 36 | + </button> |
| 37 | + </div> |
| 38 | + </ng-template> |
30 | 39 |
|
31 | | - <button |
32 | | - class="rounded-sm border border-blue-500 bg-blue-300 p-2" |
33 | | - (click)="addNewItem()"> |
34 | | - Add |
35 | | - </button> |
36 | | - </div> |
| 40 | + <ng-container *ngTemplateOutlet="card; context: ctx"></ng-container> |
37 | 41 | `, |
38 | | - imports: [ListItemComponent, NgOptimizedImage], |
| 42 | + imports: [ListItemComponent, NgOptimizedImage, NgTemplateOutlet], |
39 | 43 | }) |
40 | 44 | export class CardComponent { |
41 | | - private teacherStore = inject(TeacherStore); |
42 | | - private studentStore = inject(StudentStore); |
43 | | - |
44 | 45 | readonly list = input<any[] | null>(null); |
45 | 46 | readonly type = input.required<CardType>(); |
46 | 47 | readonly customClass = input(''); |
47 | | - |
| 48 | + readonly pictureType = input.required<string>(); |
| 49 | + addItem = output<void>(); |
| 50 | + deleteItem = output<number>(); |
| 51 | + readonly ctx = { cardType: this.type }; |
48 | 52 | CardType = CardType; |
49 | 53 |
|
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 | | - } |
| 54 | + delete(id: number) { |
| 55 | + this.deleteItem.emit(id); |
57 | 56 | } |
58 | 57 | } |
0 commit comments