diff --git a/apps/angular/1-projection/src/app/app.component.ts b/apps/angular/1-projection/src/app/app.component.ts index df654bbc2..abb8c8b42 100644 --- a/apps/angular/1-projection/src/app/app.component.ts +++ b/apps/angular/1-projection/src/app/app.component.ts @@ -7,6 +7,7 @@ import { TeacherCardComponent } from './component/teacher-card/teacher-card.comp selector: 'app-root', template: `
+ 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 8895c8c84..f534f43f8 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,9 +1,47 @@ -import { ChangeDetectionStrategy, Component } from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; +import { CityStore } from '../../data-access/city.store'; +import { + FakeHttpService, + randomCity, +} from '../../data-access/fake-http.service'; +import { CardComponent } from '../../ui/card/card.component'; +import { ListItemComponent } from '../../ui/list-item/list-item.component'; @Component({ selector: 'app-city-card', - template: 'TODO City', - imports: [], + template: ` + +

Titulo para city

+ +
+ + + + + + `, + imports: [CardComponent, ListItemComponent], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class CityCardComponent {} +export class CityCardComponent { + //Inyectamos el store de cityes + private store = inject(CityStore); + private http = inject(FakeHttpService); + + cities = this.store.cities; + citiesStore = this.store; + + ngOnInit(): void { + this.http.fetchCities$.subscribe((t) => this.store.addAll(t)); + } + + addCity = () => { + this.citiesStore.addOne(randomCity()); + }; + deleteCity(id: number) { + this.citiesStore.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 bdfa4abd4..33f185adb 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 @@ -4,27 +4,41 @@ import { inject, 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 { CardComponent } from '../../ui/card/card.component'; +import { ListItemComponent } from '../../ui/list-item/list-item.component'; @Component({ selector: 'app-student-card', template: ` + [itemTemplate]="studentsItem" + [onAdd]="addStudent"> +

Titulo para student

+ +
+ + + + + `, - styles: [ - ` - ::ng-deep .bg-light-green { - background-color: rgba(0, 250, 0, 0.1); - } - `, - ], - imports: [CardComponent], + // styles: [ + // ` + // ::ng-deep .bg-light-green { + // background-color: rgba(0, 250, 0, 0.1); + // } + // `, + // ], + imports: [CardComponent, ListItemComponent], changeDetection: ChangeDetectionStrategy.OnPush, }) export class StudentCardComponent implements OnInit { @@ -32,9 +46,17 @@ export class StudentCardComponent implements OnInit { private store = inject(StudentStore); students = this.store.students; - cardType = CardType.STUDENT; + studentsStore = this.store; + //cardType = CardType.STUDENT; ngOnInit(): void { this.http.fetchStudents$.subscribe((s) => this.store.addAll(s)); } + + addStudent = () => { + this.studentsStore.addOne(randStudent()); + }; + deleteStudent(id: number) { + this.studentsStore.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 adf0ad3c1..dde07d331 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,34 +1,58 @@ import { Component, inject, 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 { CardComponent } from '../../ui/card/card.component'; +import { ListItemComponent } from '../../ui/list-item/list-item.component'; @Component({ selector: 'app-teacher-card', template: ` + [itemTemplate]="teacherItem" + [onAdd]="addTeacher"> + +

Titulo para teacher

+ + + + + + + + `, - styles: [ - ` - ::ng-deep .bg-light-red { - background-color: rgba(250, 0, 0, 0.1); - } - `, - ], - imports: [CardComponent], + // styles: [ + // ` + // ::ng-deep .bg-light-red { + // background-color: rgba(250, 0, 0, 0.1); + // } + // `, + // ], + imports: [CardComponent, ListItemComponent], }) export class TeacherCardComponent implements OnInit { private http = inject(FakeHttpService); private store = inject(TeacherStore); teachers = this.store.teachers; - cardType = CardType.TEACHER; + teacherStore = this.store; + // cardType = CardType.TEACHER; ngOnInit(): void { this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t)); } + + addTeacher = () => { + this.teacherStore.addOne(randTeacher()); + }; + deleteTeacher(id: number) { + this.teacherStore.deleteOne(id); + } } diff --git a/apps/angular/1-projection/src/app/data-access/city.store.ts b/apps/angular/1-projection/src/app/data-access/city.store.ts index a8b523569..9fbcb346b 100644 --- a/apps/angular/1-projection/src/app/data-access/city.store.ts +++ b/apps/angular/1-projection/src/app/data-access/city.store.ts @@ -5,7 +5,7 @@ import { City } from '../model/city.model'; providedIn: 'root', }) export class CityStore { - private cities = signal([]); + public cities = signal([]); addAll(cities: City[]) { this.cities.set(cities); 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 1a6c3648c..af633c8fc 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,58 +1,83 @@ -import { NgOptimizedImage } from '@angular/common'; -import { Component, inject, 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 { CommonModule, NgOptimizedImage } from '@angular/common'; +import { Component, input, TemplateRef } from '@angular/core'; import { CardType } from '../../model/card.model'; import { ListItemComponent } from '../list-item/list-item.component'; +//La template que tengo hay ahora mismo es poco reutilizable porque cuando queramos añadir otro tipo de card +//tenemos que poner más condiciones de if y más imágenes en el img y añadir más condiciones de if en la funcion de addNewItem + +/* +Esto se hace con: + + para contenido estático + + + ngTemplateOutlet para contenido dinámico +*/ @Component({ selector: 'app-card', template: ` -
- @if (type() === CardType.TEACHER) { +
+ + + + + +
@for (item of list(); track item) { - + [type]="type()"> --> + + }
`, - imports: [ListItemComponent, NgOptimizedImage], + imports: [CommonModule, ListItemComponent, NgOptimizedImage], }) export class CardComponent { - private teacherStore = inject(TeacherStore); - private studentStore = inject(StudentStore); + //Input que permite que el padre decida como se ve cada item de la lista + //@Input() itemTemplate!: TemplateRef; + //Usamos un input signal mejor -> en el ngContainer se llama con () por ser un signal: + readonly itemTemplate = input | null>(null); + //Input signal para recibir una funcion + readonly onAdd = input<() => void>(); + + // private teacherStore = inject(TeacherStore); + // private studentStore = inject(StudentStore); readonly list = input(null); - readonly type = input.required(); - readonly customClass = input(''); + // readonly type = input.required(); + // readonly customClass = input(''); CardType = CardType; - addNewItem() { - const type = this.type(); - if (type === CardType.TEACHER) { - this.teacherStore.addOne(randTeacher()); - } else if (type === CardType.STUDENT) { - this.studentStore.addOne(randStudent()); - } - } + // addNewItem() { + // const type = this.type(); + // if (type === CardType.TEACHER) { + // this.teacherStore.addOne(randTeacher()); + // } else if (type === CardType.STUDENT) { + // this.studentStore.addOne(randStudent()); + // } + // } } 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..1fbdeaaa9 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,19 +1,17 @@ import { ChangeDetectionStrategy, Component, - inject, + EventEmitter, input, + Output, } from '@angular/core'; -import { StudentStore } from '../../data-access/student.store'; -import { TeacherStore } from '../../data-access/teacher.store'; -import { CardType } from '../../model/card.model'; @Component({ selector: 'app-list-item', template: `
{{ name() }} -
@@ -21,19 +19,22 @@ import { CardType } from '../../model/card.model'; changeDetection: ChangeDetectionStrategy.OnPush, }) export class ListItemComponent { - private teacherStore = inject(TeacherStore); - private studentStore = inject(StudentStore); + @Output() delete = new EventEmitter(); + + // private teacherStore = inject(TeacherStore); + // private studentStore = inject(StudentStore); readonly id = input.required(); readonly name = input.required(); - readonly type = input.required(); + // readonly type = input.required(); - delete(id: number) { - const type = this.type(); + deleteItem() { + this.delete.emit(this.id()); + /*const type = this.type(); if (type === CardType.TEACHER) { this.teacherStore.deleteOne(id); } else if (type === CardType.STUDENT) { this.studentStore.deleteOne(id); - } + }*/ } }