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,63 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
OnInit,
} from '@angular/core';
import { CityStore } from '../../data-access/city.store';
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 { ItemRowDirective } from '../../directive/item-row.directive';

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

<ng-template [itemRow]="cities()" let-city>
<app-list-item (delete)="deleteCity(city.id)">
!{{ city.name }}
</app-list-item>
</ng-template>
</app-card>
`,
styles: [
`
.bg-light-blue {
background-color: rgba(0, 0, 250, 0.1);
}
`,
],
imports: [CardComponent, NgOptimizedImage, ListItemComponent, ItemRowDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent implements OnInit {
private readonly http = inject(FakeHttpService);
private readonly store = inject(CityStore);

protected cities = this.store.cities;

public ngOnInit(): void {
this.http.fetchCities$.subscribe((cities: City[]) =>
this.store.addAll(cities),
);
}

protected addCity(): void {
this.store.addOne(randomCity());
}

protected deleteCity(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,
} 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';
import { ItemRowDirective } from '../../directive/item-row.directive';

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

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

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

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

protected addStudent(): void {
this.store.addOne(randStudent());
}

protected deleteStudent(id: number): void {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
import { Component, inject, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
OnInit,
} from '@angular/core';
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 { ItemRowDirective } from '../../directive/item-row.directive';

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

<ng-template [itemRow]="teachers()" let-teacher let-character="character">
<app-list-item
(delete)="deleteTeacher(teacher.id)" >
{{character}}{{ teacher.firstName }}
</app-list-item>
</ng-template>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-red {
.bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
imports: [CardComponent],
imports: [CardComponent, NgOptimizedImage, ListItemComponent, ItemRowDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);
private readonly http = inject(FakeHttpService);
private readonly store = inject(TeacherStore);

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

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

protected addTeacher(): void {
this.store.addOne(randTeacher());
}

protected deleteTeacher(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
17 changes: 17 additions & 0 deletions apps/angular/1-projection/src/app/directive/item-row.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Directive, input } from "@angular/core";

interface ItemRowContext<T> {
$implicit: T;
character: string;
}

@Directive({
selector: '[itemRow]'
})
export class ItemRowDirective<T> {
public itemRow = input.required<T[]>();

static ngTemplateContextGuard<TContext>(directive: ItemRowDirective<TContext>, ctx: unknown): ctx is ItemRowContext<TContext> {
return true;
}
}
5 changes: 0 additions & 5 deletions apps/angular/1-projection/src/app/model/card.model.ts

This file was deleted.

90 changes: 44 additions & 46 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,56 @@
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 { ListItemComponent } from '../list-item/list-item.component';
import { NgTemplateOutlet } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
computed,
contentChild,
input,
output,
TemplateRef,
} from '@angular/core';
import { ItemRowDirective } from '../../directive/item-row.directive';

@Component({
selector: 'app-card',
host: {
'[class]': 'classList()',
},
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-content select="img"></ng-content>

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

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
</div>
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNew.emit()">
Add
</button>
`,
imports: [ListItemComponent, NgOptimizedImage],
imports: [NgTemplateOutlet],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
export class CardComponent<T extends { id: number }> {
public readonly list = input<T[] | null>(null);
public readonly customClass = input('');

public readonly addNew = output();

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

CardType = CardType;
public readonly rowTemplate = contentChild.required(ItemRowDirective, {
read: TemplateRef,
});

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
}
protected classList = computed(
() =>
`flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4 ${this.customClass()}`,
);
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
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">
{{ name() }}
<button (click)="delete(id())">
<ng-content />
<button (click)="delete.emit()">
<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>();

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 readonly delete = output();
}