- @if (type() === CardType.TEACHER) {
-
![]()
- }
- @if (type() === CardType.STUDENT) {
-
![]()
- }
+ [style.backgroundColor]="backgroundColor()">
+
@for (item of list(); track item) {
}
@@ -40,19 +40,39 @@ import { ListItemComponent } from '../list-item/list-item.component';
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
+ private cityStore = inject(CityStore);
readonly list = input(null);
readonly type = input.required();
- readonly customClass = input('');
+ readonly backgroundColor = input('');
CardType = CardType;
+ addHandler: Record void> = {
+ [CardType.TEACHER]: () => this.teacherStore.addOne(randTeacher()),
+ [CardType.STUDENT]: () => this.studentStore.addOne(randStudent()),
+ [CardType.CITY]: () => this.cityStore.addOne(randomCity()),
+ };
+
+ nameLookup: Record string> = {
+ [CardType.TEACHER]: (item: any) => item.firstName,
+ [CardType.STUDENT]: (item: any) => item.firstName,
+ [CardType.CITY]: (item: any) => item.name,
+ };
+
addNewItem() {
- const type = this.type();
- if (type === CardType.TEACHER) {
- this.teacherStore.addOne(randTeacher());
- } else if (type === CardType.STUDENT) {
- this.studentStore.addOne(randStudent());
- }
+ this.addHandler[this.type()]();
+ }
+
+ itemName(item: any) {
+ return this.nameLookup[this.type()](item);
}
+
+ imageLookup: Record = {
+ [CardType.TEACHER]: 'assets/img/teacher.png',
+ [CardType.STUDENT]: 'assets/img/student.webp',
+ [CardType.CITY]: 'assets/img/city.png',
+ };
+
+ imageSrc = computed(() => this.imageLookup[this.type()]);
}
diff --git a/apps/angular/1-projection/src/app/ui/list-item/list-item.component.ts b/apps/angular/1-projection/src/app/ui/list-item/list-item.component.ts
index 5d504f372..0c5eadcd8 100644
--- a/apps/angular/1-projection/src/app/ui/list-item/list-item.component.ts
+++ b/apps/angular/1-projection/src/app/ui/list-item/list-item.component.ts
@@ -4,6 +4,7 @@ import {
inject,
input,
} from '@angular/core';
+import { CityStore } from '../../data-access/city.store';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
@@ -23,17 +24,19 @@ import { CardType } from '../../model/card.model';
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
+ private cityStore = inject(CityStore);
readonly id = input.required();
readonly name = input.required();
readonly type = input.required();
+ deleteItem: Record void> = {
+ [CardType.TEACHER]: (id: number) => this.teacherStore.deleteOne(id),
+ [CardType.STUDENT]: (id: number) => this.studentStore.deleteOne(id),
+ [CardType.CITY]: (id: number) => this.cityStore.deleteOne(id),
+ };
+
delete(id: number) {
- const type = this.type();
- if (type === CardType.TEACHER) {
- this.teacherStore.deleteOne(id);
- } else if (type === CardType.STUDENT) {
- this.studentStore.deleteOne(id);
- }
+ this.deleteItem[this.type()](id);
}
}