Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import { CardType } from '../../model/card.model';
import { City } from '../../model/city.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
template: `
<app-card
(itemDeleted)="onDelete($event)"
(itemAdded)="onAdd()"
[list]="cities()"
[displayNameFn]="displayNameFn"
customClass="bg-light-green">
<img ngSrc="assets/img/city.png" width="200" height="200" alt="" />
</app-card>
`,
imports: [CardComponent, NgOptimizedImage],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent {
private http = inject(FakeHttpService);
private store = inject(CityStore);

cities = this.store.cities;
cardType = CardType.CITY;

ngOnInit(): void {
this.http.fetchCities$.subscribe((s) => this.store.addAll(s));
}

displayNameFn = (city: City) => city.name;

onDelete(id: number) {
this.store.deleteOne(id);
}

onAdd() {
this.store.addOne(randomCity());
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
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 { Student } from '../../model/student.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-student-card',
template: `
<app-card
(itemDeleted)="onDelete($event)"
(itemAdded)="onAdd()"
[list]="students()"
[type]="cardType"
customClass="bg-light-green" />
[displayNameFn]="displayNameFn"
customClass="bg-light-green">
<img ngSrc="assets/img/student.webp" width="200" height="200" alt="" />
</app-card>
`,
styles: [
`
Expand All @@ -24,7 +33,7 @@ import { CardComponent } from '../../ui/card/card.component';
}
`,
],
imports: [CardComponent],
imports: [CardComponent, NgOptimizedImage],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
Expand All @@ -37,4 +46,14 @@ export class StudentCardComponent implements OnInit {
ngOnInit(): void {
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));
}

displayNameFn = (student: Student) => student.firstName;

onDelete(id: number) {
this.store.deleteOne(id);
}

onAdd() {
this.store.addOne(randStudent());
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { NgOptimizedImage } from '@angular/common';
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 { Teacher } from '../../model/teacher.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-teacher-card',
template: `
<app-card
(itemDeleted)="onDelete($event)"
(itemAdded)="onAdd()"
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
[displayNameFn]="displayNameFn"
customClass="bg-light-red">
<img ngSrc="assets/img/teacher.png" width="200" height="200" alt="" />
</app-card>
`,
styles: [
`
Expand All @@ -19,16 +28,26 @@ import { CardComponent } from '../../ui/card/card.component';
}
`,
],
imports: [CardComponent],
imports: [CardComponent, NgOptimizedImage],
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);
public store = inject(TeacherStore);

teachers = this.store.teachers;
cardType = CardType.TEACHER;

ngOnInit(): void {
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));
}

displayNameFn = (teacher: Teacher) => teacher.firstName;

onDelete(id: number) {
this.store.deleteOne(id);
}

onAdd() {
this.store.addOne(randTeacher());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { City } from '../model/city.model';
providedIn: 'root',
})
export class CityStore {
private cities = signal<City[]>([]);
public cities = signal<City[]>([]);

addAll(cities: City[]) {
this.cities.set(cities);
Expand Down
35 changes: 9 additions & 26 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
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 { CardType } from '../../model/card.model';
import { Component, input, output } from '@angular/core';
import { ListItemComponent } from '../list-item/list-item.component';

@Component({
Expand All @@ -12,19 +7,14 @@ import { ListItemComponent } from '../list-item/list-item.component';
<div
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
[class]="customClass()">
@if (type() === CardType.TEACHER) {
<img ngSrc="assets/img/teacher.png" width="200" height="200" alt="" />
}
@if (type() === CardType.STUDENT) {
<img ngSrc="assets/img/student.webp" width="200" height="200" alt="" />
}
<ng-content></ng-content>

<section>
@for (item of list(); track item) {
<app-list-item
[name]="item.firstName"
[name]="displayNameFn()(item)"
[id]="item.id"
[type]="type()"></app-list-item>
(itemDeleted)="itemDeleted.emit($event)"></app-list-item>
}
</section>

Expand All @@ -35,24 +25,17 @@ import { ListItemComponent } from '../list-item/list-item.component';
</button>
</div>
`,
imports: [ListItemComponent, NgOptimizedImage],
imports: [ListItemComponent],
})
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
itemDeleted = output<number>();
itemAdded = output<void>();

readonly list = input<any[] | null>(null);
readonly type = input.required<CardType>();
readonly customClass = input('');

CardType = CardType;
readonly displayNameFn = input<(item: any) => string>(() => '');

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
this.itemAdded.emit();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import {
ChangeDetectionStrategy,
Component,
inject,
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',
Expand All @@ -21,19 +18,12 @@ import { CardType } from '../../model/card.model';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
itemDeleted = output<number>();

readonly id = input.required<number>();
readonly name = input.required<string>();
readonly type = input.required<CardType>();

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.itemDeleted.emit(id);
}
}
Loading