Skip to content
Closed
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,43 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
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 { CardItemDirective } from '../../ui/card/card-item.directive';
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: `
<app-card
[list]="store.cities()"
customClass="bg-blue-100"
(add)="store.addOne(randomCity())">
<ng-template appCardItem let-city>
<app-list-item
[id]="$any(city).id"
[name]="$any(city).name"
(delete)="store.deleteOne($event)" />
</ng-template>
</app-card>
`,
imports: [CardComponent, CardItemDirective, ListItemComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent implements OnInit {
private http = inject(FakeHttpService);
protected store = inject(CityStore);

randomCity = randomCity;

ngOnInit(): void {
this.http.fetchCities$.subscribe((c) => this.store.addAll(c));
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
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 { CardItemDirective } from '../../ui/card/card-item.directive';
import { CardComponent } from '../../ui/card/card.component';
import { ListItemComponent } from '../../ui/list-item/list-item.component';

@Component({
selector: 'app-student-card',
template: `
<app-card
[list]="students()"
[type]="cardType"
customClass="bg-light-green" />
[list]="store.students()"
customClass="bg-green-100"
(add)="store.addOne(randStudent())">
<img
card-image
ngSrc="assets/img/student.webp"
width="200"
height="200"
alt="" />
<ng-template appCardItem let-student>
<app-list-item
[id]="$any(student).id"
[name]="$any(student).firstName"
(delete)="store.deleteOne($event)" />
</ng-template>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
imports: [
CardComponent,
CardItemDirective,
ListItemComponent,
NgOptimizedImage,
],
imports: [CardComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);
protected store = inject(StudentStore);

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

ngOnInit(): void {
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
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 { CardItemDirective } from '../../ui/card/card-item.directive';
import { CardComponent } from '../../ui/card/card.component';
import { ListItemComponent } from '../../ui/list-item/list-item.component';

@Component({
selector: 'app-teacher-card',
template: `
<app-card
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
[list]="store.teachers()"
customClass="bg-red-100"
(add)="store.addOne(randTeacher())">
<img
card-image
ngSrc="assets/img/teacher.png"
width="200"
height="200"
alt="" />
<ng-template appCardItem let-teacher>
<app-list-item
[id]="$any(teacher).id"
[name]="$any(teacher).firstName"
(delete)="store.deleteOne($event)" />
</ng-template>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
imports: [
CardComponent,
CardItemDirective,
ListItemComponent,
NgOptimizedImage,
],
imports: [CardComponent],
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);
protected store = inject(TeacherStore);

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

ngOnInit(): void {
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));
Expand Down
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
19 changes: 19 additions & 0 deletions apps/angular/1-projection/src/app/ui/card/card-item.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Directive, inject, TemplateRef } from '@angular/core';

export interface CardItemContext<T> {
$implicit: T;
}

@Directive({
selector: '[appCardItem]',
})
export class CardItemDirective<T> {
templateRef = inject<TemplateRef<CardItemContext<T>>>(TemplateRef);

static ngTemplateContextGuard<T>(
_dir: CardItemDirective<T>,
ctx: unknown,
): ctx is CardItemContext<T> {
return true;
}
}
56 changes: 21 additions & 35 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,44 @@
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,
contentChild,
input,
output,
} from '@angular/core';
import { CardItemDirective } from './card-item.directive';

@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-image]" />

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

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
(click)="add.emit()">
Add
</button>
</div>
`,
imports: [ListItemComponent, NgOptimizedImage],
imports: [NgTemplateOutlet],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);

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

CardType = CardType;
protected itemTemplate = contentChild.required(CardItemDirective);

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
}
add = output<void>();
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
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="flex justify-between border border-gray-300 px-2 py-1">
{{ name() }}
<button (click)="delete(id())">
<button (click)="delete.emit(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);

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