diff --git a/apps/angular/1-projection/src/app/component/city-card/city-card.component.ts b/apps/angular/1-projection/src/app/component/city-card/city-card.component.ts index 47b089650..a63a91994 100644 --- a/apps/angular/1-projection/src/app/component/city-card/city-card.component.ts +++ b/apps/angular/1-projection/src/app/component/city-card/city-card.component.ts @@ -1,12 +1,59 @@ import { Component, OnInit } from '@angular/core'; +import { CityStore } from '../../data-access/city.store'; +import { + FakeHttpService, + randomCity, +} from '../../data-access/fake-http.service'; +import { CardListItem } from '../../model/card.model'; +import { CardComponent } from '../../ui/card/card.component'; @Component({ selector: 'app-city-card', - template: 'TODO City', - imports: [], + template: ` + +
+ +
+
+ `, + styles: [ + ` + .bg-light-green { + background-color: rgba(0, 250, 0, 0.1); + } + `, + ], + imports: [CardComponent], }) export class CityCardComponent implements OnInit { - constructor() {} + cityListItems: CardListItem[] = []; - ngOnInit(): void {} + constructor( + private http: FakeHttpService, + private store: CityStore, + ) {} + + ngOnInit(): void { + this.http.fetchCities$.subscribe((e) => this.store.addAll(e)); + this.store.cities$.subscribe((cities) => { + this.cityListItems = cities.map((city) => { + return { + name: city.name, + id: city.id, + }; + }); + }); + } + + handleAddNewItemEvent() { + this.store.addOne(randomCity()); + } + + handleDeleteItemEvent(id: number) { + this.store.deleteOne(id); + } } diff --git a/apps/angular/1-projection/src/app/component/student-card/student-card.component.ts b/apps/angular/1-projection/src/app/component/student-card/student-card.component.ts index dae48a2d5..163517eb0 100644 --- a/apps/angular/1-projection/src/app/component/student-card/student-card.component.ts +++ b/apps/angular/1-projection/src/app/component/student-card/student-card.component.ts @@ -1,21 +1,28 @@ import { Component, OnInit } from '@angular/core'; -import { FakeHttpService } from '../../data-access/fake-http.service'; +import { + FakeHttpService, + randStudent, +} from '../../data-access/fake-http.service'; import { StudentStore } from '../../data-access/student.store'; -import { CardType } from '../../model/card.model'; -import { Student } from '../../model/student.model'; +import { CardListItem } from '../../model/card.model'; import { CardComponent } from '../../ui/card/card.component'; @Component({ selector: 'app-student-card', template: ` + [list]="studentListItems" + class="bg-light-green" + (addNewItemEvent)="handleAddNewItemEvent()" + (deleteItemEvent)="handleDeleteItemEvent($event)"> +
+ +
+ `, styles: [ ` - ::ng-deep .bg-light-green { + .bg-light-green { background-color: rgba(0, 250, 0, 0.1); } `, @@ -23,8 +30,7 @@ import { CardComponent } from '../../ui/card/card.component'; imports: [CardComponent], }) export class StudentCardComponent implements OnInit { - students: Student[] = []; - cardType = CardType.STUDENT; + studentListItems: CardListItem[] = []; constructor( private http: FakeHttpService, @@ -34,6 +40,21 @@ export class StudentCardComponent implements OnInit { ngOnInit(): void { this.http.fetchStudents$.subscribe((s) => this.store.addAll(s)); - this.store.students$.subscribe((s) => (this.students = s)); + this.store.students$.subscribe((students) => { + this.studentListItems = students.map((student) => { + return { + name: student.firstName, + id: student.id, + }; + }); + }); + } + + handleAddNewItemEvent() { + this.store.addOne(randStudent()); + } + + handleDeleteItemEvent(id: number) { + this.store.deleteOne(id); } } diff --git a/apps/angular/1-projection/src/app/component/teacher-card/teacher-card.component.ts b/apps/angular/1-projection/src/app/component/teacher-card/teacher-card.component.ts index 815cde9d5..2d653ae22 100644 --- a/apps/angular/1-projection/src/app/component/teacher-card/teacher-card.component.ts +++ b/apps/angular/1-projection/src/app/component/teacher-card/teacher-card.component.ts @@ -1,21 +1,28 @@ import { Component, OnInit } from '@angular/core'; -import { FakeHttpService } from '../../data-access/fake-http.service'; +import { + FakeHttpService, + randTeacher, +} from '../../data-access/fake-http.service'; import { TeacherStore } from '../../data-access/teacher.store'; -import { CardType } from '../../model/card.model'; -import { Teacher } from '../../model/teacher.model'; +import { CardListItem } from '../../model/card.model'; import { CardComponent } from '../../ui/card/card.component'; @Component({ selector: 'app-teacher-card', template: ` + [list]="teacherListItems" + class="bg-light-red" + (addNewItemEvent)="handleAddNewItemEvent()" + (deleteItemEvent)="handleDeleteItemEvent($event)"> +
+ +
+ `, styles: [ ` - ::ng-deep .bg-light-red { + .bg-light-red { background-color: rgba(250, 0, 0, 0.1); } `, @@ -23,8 +30,7 @@ import { CardComponent } from '../../ui/card/card.component'; imports: [CardComponent], }) export class TeacherCardComponent implements OnInit { - teachers: Teacher[] = []; - cardType = CardType.TEACHER; + teacherListItems: CardListItem[] = []; constructor( private http: FakeHttpService, @@ -34,6 +40,21 @@ export class TeacherCardComponent implements OnInit { ngOnInit(): void { this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t)); - this.store.teachers$.subscribe((t) => (this.teachers = t)); + this.store.teachers$.subscribe((teachers) => { + this.teacherListItems = teachers.map((teacher) => { + return { + name: teacher.firstName, + id: teacher.id, + }; + }); + }); + } + + handleAddNewItemEvent() { + this.store.addOne(randTeacher()); + } + + handleDeleteItemEvent(id: number) { + this.store.deleteOne(id); } } diff --git a/apps/angular/1-projection/src/app/model/card.model.ts b/apps/angular/1-projection/src/app/model/card.model.ts index 740cd2ae4..69c0e7d17 100644 --- a/apps/angular/1-projection/src/app/model/card.model.ts +++ b/apps/angular/1-projection/src/app/model/card.model.ts @@ -3,3 +3,8 @@ export enum CardType { STUDENT, CITY, } + +export interface CardListItem { + name: string; + id: number; +} diff --git a/apps/angular/1-projection/src/app/ui/card/card.component.ts b/apps/angular/1-projection/src/app/ui/card/card.component.ts index ca3c661de..1dca1c178 100644 --- a/apps/angular/1-projection/src/app/ui/card/card.component.ts +++ b/apps/angular/1-projection/src/app/ui/card/card.component.ts @@ -1,60 +1,39 @@ -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 { NgFor } from '@angular/common'; +import { Component, EventEmitter, Input, Output } from '@angular/core'; +import { CardListItem } from '../../model/card.model'; import { ListItemComponent } from '../list-item/list-item.component'; @Component({ selector: 'app-card', template: ` -
- - - -
- -
- - -
+ +
+ +
+ `, - imports: [NgIf, NgFor, ListItemComponent], + imports: [NgFor, ListItemComponent], + host: { + class: 'border-2 border-black rounded-md p-4 w-fit flex flex-col gap-3', + }, }) export class CardComponent { - @Input() list: any[] | null = null; - @Input() type!: CardType; - @Input() customClass = ''; - - CardType = CardType; - - constructor( - private teacherStore: TeacherStore, - private studentStore: StudentStore, - ) {} + @Input() list: CardListItem[] | null = null; + @Output() deleteItemEvent = new EventEmitter(); + @Output() addNewItemEvent = new EventEmitter(); addNewItem() { - if (this.type === CardType.TEACHER) { - this.teacherStore.addOne(randTeacher()); - } else if (this.type === CardType.STUDENT) { - this.studentStore.addOne(randStudent()); - } + this.addNewItemEvent.emit(); + } + handleDeleteItemEvent(id: number) { + this.deleteItemEvent.emit(id); } } 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 c0f9cff7f..2dbdf556a 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 @@ -1,7 +1,4 @@ -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 { Component, EventEmitter, Input, Output } from '@angular/core'; @Component({ selector: 'app-list-item', @@ -18,18 +15,9 @@ import { CardType } from '../../model/card.model'; export class ListItemComponent { @Input() id!: number; @Input() name!: string; - @Input() type!: CardType; - - constructor( - private teacherStore: TeacherStore, - private studentStore: StudentStore, - ) {} + @Output() deleteItemEvent = new EventEmitter(); delete(id: number) { - if (this.type === CardType.TEACHER) { - this.teacherStore.deleteOne(id); - } else if (this.type === CardType.STUDENT) { - this.studentStore.deleteOne(id); - } + this.deleteItemEvent.emit(id); } }