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,41 @@
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 { CardType } from '../../model/card.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
template: `
<app-card
[list]="cities()"
[type]="cardType"
(addItem)="addNewItem()"
(deleteItem)="deleteItem($event)"
pictureType="png"
customClass="bg-light-green" />
`,
imports: [CardComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent {
private http = inject(FakeHttpService);
private store = inject(CityStore);
readonly cardType = CardType.CITY;
readonly cities = this.store.cities;

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

addNewItem() {
this.store.addOne(randomCity());
}

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ 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';
Expand All @@ -15,6 +18,9 @@ import { CardComponent } from '../../ui/card/card.component';
<app-card
[list]="students()"
[type]="cardType"
(addItem)="addNewItem()"
(deleteItem)="deleteItem($event)"
pictureType="webp"
customClass="bg-light-green" />
`,
styles: [
Expand All @@ -30,11 +36,18 @@ import { CardComponent } from '../../ui/card/card.component';
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);

students = this.store.students;
cardType = CardType.STUDENT;

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

addNewItem() {
this.store.addOne(randStudent());
}

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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';
Expand All @@ -10,6 +13,9 @@ import { CardComponent } from '../../ui/card/card.component';
<app-card
[list]="teachers()"
[type]="cardType"
(addItem)="addNewItem()"
(deleteItem)="deleteItem($event)"
pictureType="png"
customClass="bg-light-red"></app-card>
`,
styles: [
Expand All @@ -31,4 +37,12 @@ export class TeacherCardComponent implements OnInit {
ngOnInit(): void {
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));
}

addNewItem() {
this.store.addOne(randTeacher());
}

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
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
79 changes: 39 additions & 40 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
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 { NgOptimizedImage, NgTemplateOutlet } from '@angular/common';
import { Component, input, output } from '@angular/core';
import { CardType } from '../../model/card.model';
import { ListItemComponent } from '../list-item/list-item.component';

@Component({
selector: 'app-card',
template: `
<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" />
}
@if (type() === CardType.STUDENT) {
<img ngSrc="assets/img/student.webp" width="200" height="200" />
}
<ng-template #card let-cardTypeVal="cardType">
<div
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
[class]="customClass()">
<img
ngSrc="assets/img/{{ CardType[cardTypeVal()].toLowerCase() }}.{{
this.pictureType()
}}"
width="200"
height="200"
priority />
<section>
@for (item of list(); track item) {
<app-list-item
[name]="item.name"
[firstName]="item.firstName"
[lastName]="item.lastName"
[id]="item.id"
[type]="type()"
(deleteItem)="delete($event)" />
}
</section>

<section>
@for (item of list(); track item) {
<app-list-item
[name]="item.firstName"
[id]="item.id"
[type]="type()"></app-list-item>
}
</section>
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addItem.emit()">
Add
</button>
</div>
</ng-template>

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
</div>
<ng-container *ngTemplateOutlet="card; context: ctx"></ng-container>
`,
imports: [ListItemComponent, NgOptimizedImage],
imports: [ListItemComponent, NgOptimizedImage, NgTemplateOutlet],
})
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);

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

readonly pictureType = input.required<string>();
addItem = output<void>();
deleteItem = output<number>();
readonly ctx = { cardType: this.type };
CardType = CardType;

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
delete(id: number) {
this.deleteItem.emit(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
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',
template: `
<div class="border-grey-300 flex justify-between border px-2 py-1">
{{ firstName() }} {{ lastName() }}
{{ name() }}
<button (click)="delete(id())">
<button (click)="onDelete()">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);

readonly id = input.required<number>();
readonly name = input.required<string>();
readonly type = input.required<CardType>();
readonly type = input<CardType>();
readonly name = input<string>();
readonly firstName = input<string>('');
readonly lastName = input<string>('');
deleteItem = output<number>();

delete(id: number) {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
}
onDelete() {
this.deleteItem.emit(this.id());
}
}