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,60 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
WritableSignal,
} from '@angular/core';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import { City } from '../../model/city.model';
import { CardComponent } from '../../ui/card/card.component';
import { ListItemComponent } from '../../ui/list-item/list-item.component';
import { CityStore } from './../../data-access/city.store';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
template: `
<app-card
customClass="bg-light-blue"
[list]="cities()"
[template]="listItem"
(addNewItem)="addNewItem()">
<img
card-header
ngSrc="assets/img/city.png"
width="200"
height="200"
alt="" />
</app-card>

<ng-template #listItem let-item>
<app-list-item
[name]="item.name"
[id]="item.id"
(deleteItem)="deleteItem($event)" />
</ng-template>
`,
imports: [CardComponent, NgOptimizedImage, ListItemComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent {
private http: FakeHttpService = inject(FakeHttpService);
private store: CityStore = inject(CityStore);

protected cities: WritableSignal<City[]> = this.store.cities;

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

public addNewItem(): void {
this.store.addOne(randomCity());
}

public deleteItem(id: number): void {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,61 @@
import { NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
OnInit,
WritableSignal,
} 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';
import { ListItemComponent } from '../../ui/list-item/list-item.component';

@Component({
selector: 'app-student-card',
template: `
<app-card
customClass="bg-light-green"
[list]="students()"
[type]="cardType"
customClass="bg-light-green" />
[template]="listItem"
(addNewItem)="addNewItem()">
<img
card-header
ngSrc="assets/img/student.webp"
width="200"
height="200"
alt="" />
</app-card>

<ng-template #listItem let-item>
<app-list-item
[name]="item.firstName"
[id]="item.id"
(deleteItem)="deleteItem($event)" />
</ng-template>
`,
styles: [
`
::ng-deep .bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
],
imports: [CardComponent],
imports: [CardComponent, NgOptimizedImage, ListItemComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);
private http: FakeHttpService = inject(FakeHttpService);
private store: StudentStore = inject(StudentStore);

students = this.store.students;
cardType = CardType.STUDENT;
protected students: WritableSignal<Student[]> = this.store.students;

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

public addNewItem(): void {
this.store.addOne(randStudent());
}

public deleteItem(id: number): void {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,61 @@
import { NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
OnInit,
WritableSignal,
} 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';
import { Teacher } from './../../model/teacher.model';

@Component({
selector: 'app-teacher-card',
template: `
<app-card
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
[template]="listItem"
(addNewItem)="addNewItem()"
customClass="bg-light-red">
<img
card-header
ngSrc="assets/img/teacher.png"
width="200"
height="200"
alt="" />
</app-card>

<ng-template #listItem let-item>
<app-list-item
[name]="item.firstName"
[id]="item.id"
(deleteItem)="deleteItem($event)" />
</ng-template>
`,
styles: [
`
::ng-deep .bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
changeDetection: ChangeDetectionStrategy.Eager,
imports: [CardComponent],
imports: [CardComponent, NgOptimizedImage, ListItemComponent],
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);
private http: FakeHttpService = inject(FakeHttpService);
private store: TeacherStore = inject(TeacherStore);

teachers = this.store.teachers;
cardType = CardType.TEACHER;
protected teachers: WritableSignal<Teacher[]> = this.store.teachers;

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

public addNewItem(): void {
this.store.addOne(randTeacher());
}

public deleteItem(id: number): void {
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
5 changes: 0 additions & 5 deletions apps/angular/1-projection/src/app/model/card.model.ts

This file was deleted.

70 changes: 38 additions & 32 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,70 @@
import { NgOptimizedImage } from '@angular/common';
import { NgTemplateOutlet } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
input,
InputSignal,
output,
OutputEmitterRef,
TemplateRef,
} 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 { ListItemComponent } from '../list-item/list-item.component';
import { City } from '../../model/city.model';
import { Student } from '../../model/student.model';
import { Teacher } from '../../model/teacher.model';
interface CardTemplateContext {
$implicit: TCardTuple;
}

type TCardTuple = Student | Teacher | City;

@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" alt="" />
}
@if (type() === CardType.STUDENT) {
<img ngSrc="assets/img/student.webp" width="200" height="200" alt="" />
}
<ng-content select="[card-header]" />

<section>
@for (item of list(); track item) {
<app-list-item
[name]="item.firstName"
[id]="item.id"
[type]="type()"></app-list-item>
<ng-container
[ngTemplateOutlet]="template()"
[ngTemplateOutletContext]="{ $implicit: item }" />
}
</section>

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
(click)="onAddNewItem($event)">
Add
</button>
</div>
`,
styles: [
`
.bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
.bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
.bg-light-blue {
background-color: rgba(0, 0, 250, 0.1);
}
`,
],
changeDetection: ChangeDetectionStrategy.Eager,
imports: [ListItemComponent, NgOptimizedImage],
imports: [NgTemplateOutlet],
})
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
public addNewItem: OutputEmitterRef<Event> = output<Event>();

readonly list = input<any[] | null>(null);
readonly type = input.required<CardType>();
public template: InputSignal<TemplateRef<CardTemplateContext>> =
input.required<TemplateRef<CardTemplateContext>>();
readonly list = input<TCardTuple[] | null>(null);
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());
}
public onAddNewItem(event: Event): void {
this.addNewItem.emit(event);
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
import {
ChangeDetectionStrategy,
Component,
inject,
input,
InputSignal,
output,
OutputEmitterRef,
} 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="flex justify-between border border-gray-300 px-2 py-1">
{{ name() }}
<button (click)="delete(id())">
<button (click)="onDeleteItem(id())">
<img class="h-5" src="assets/svg/trash.svg" alt="trash" />
</button>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
public deleteItem: OutputEmitterRef<number> = output<number>();

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

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