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,48 @@
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 { randomCity } from '../../data-access/fake-http.service';
import { CardType } from '../../model/card.model';
import { City } from '../../model/city.model';
import { CardItemDirective } from '../../ui/card/card-item-context.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]="cities.value()" (onAddNewItem)="addNewCity()">
<img ngSrc="assets/img/city.png" width="200" height="200" />

<ng-template [appCardItem]="_cityType" #itemTemplate let-item>
<app-list-item
[name]="item.name"
[id]="item.id"
[type]="cardType"
(onDeleteItem)="deleteCity(item.id)"></app-list-item>
</ng-template>
</app-card>
`,
imports: [
CardComponent,
NgOptimizedImage,
ListItemComponent,
CardItemDirective,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent {
private store = inject(CityStore);
readonly _cityType!: City;

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

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

deleteCity(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,58 @@
import {
ChangeDetectionStrategy,
Component,
inject,
OnInit,
} from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { 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 { CardItemDirective } from '../../ui/card/card-item-context.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]="students.value()"
class="bg-light-green"
(onAddNewItem)="addNewItem()">
<img ngSrc="assets/img/student.webp" width="200" height="200" />

<ng-template [appCardItem]="_studentType" #itemTemplate let-item>
<app-list-item
[name]="item.firstName"
[id]="item.id"
[type]="cardType"
(onDeleteItem)="deleteStudent(item.id)"></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,
CardItemDirective,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
export class StudentCardComponent {
private store = inject(StudentStore);
readonly _studentType!: Student;

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

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

deleteStudent(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
import { Component, inject, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { 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 { CardItemDirective } from '../../ui/card/card-item-context.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]="teachers.value()"
class="bg-light-red"
(onAddNewItem)="addNewTeacher()">
<img priority ngSrc="assets/img/teacher.png" width="200" height="200" />

<ng-template [appCardItem]="_teacherType" #itemTemplate let-item>
<app-list-item
[name]="item.firstName"
[id]="item.id"
[type]="cardType"
(onDeleteItem)="deleteTeacher(item.id)"></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,
CardItemDirective,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
export class TeacherCardComponent {
private store = inject(TeacherStore);
readonly _teacherType!: Teacher;

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

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

deleteTeacher(id: number) {
this.store.deleteOne(id);
}
}
15 changes: 11 additions & 4 deletions apps/angular/1-projection/src/app/data-access/city.store.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { Injectable, signal } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { rxResource } from '@angular/core/rxjs-interop';
import { City } from '../model/city.model';
import { FakeHttpService } from './fake-http.service';

@Injectable({
providedIn: 'root',
})
export class CityStore {
private cities = signal<City[]>([]);
http = inject(FakeHttpService);

cities = rxResource({
stream: () => this.http.fetchCities$,
defaultValue: [],
});

addAll(cities: City[]) {
this.cities.set(cities);
}

addOne(city: City) {
this.cities.set([...this.cities(), city]);
this.cities.set([...this.cities.value(), city]);
}

deleteOne(id: number) {
this.cities.set(this.cities().filter((s) => s.id !== id));
this.cities.set(this.cities.value().filter((s) => s.id !== id));
}
}
15 changes: 11 additions & 4 deletions apps/angular/1-projection/src/app/data-access/student.store.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { Injectable, signal } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { rxResource } from '@angular/core/rxjs-interop';
import { Student } from '../model/student.model';
import { FakeHttpService } from './fake-http.service';

@Injectable({
providedIn: 'root',
})
export class StudentStore {
public students = signal<Student[]>([]);
http = inject(FakeHttpService);

students = rxResource({
stream: () => this.http.fetchStudents$,
defaultValue: [],
});

addAll(students: Student[]) {
this.students.set(students);
}

addOne(student: Student) {
this.students.set([...this.students(), student]);
this.students.set([...this.students.value(), student]);
}

deleteOne(id: number) {
this.students.set(this.students().filter((s) => s.id !== id));
this.students.set(this.students.value().filter((s) => s.id !== id));
}
}
15 changes: 11 additions & 4 deletions apps/angular/1-projection/src/app/data-access/teacher.store.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { Injectable, signal } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { rxResource } from '@angular/core/rxjs-interop';
import { Teacher } from '../model/teacher.model';
import { FakeHttpService } from './fake-http.service';

@Injectable({
providedIn: 'root',
})
export class TeacherStore {
public teachers = signal<Teacher[]>([]);
http = inject(FakeHttpService);

teachers = rxResource({
stream: () => this.http.fetchTeachers$,
defaultValue: [],
});

addAll(teachers: Teacher[]) {
this.teachers.set(teachers);
}

addOne(teacher: Teacher) {
this.teachers.set([...this.teachers(), teacher]);
this.teachers.set([...this.teachers.value(), teacher]);
}

deleteOne(id: number) {
this.teachers.set(this.teachers().filter((t) => t.id !== id));
this.teachers.set(this.teachers.value().filter((t) => t.id !== id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Directive, inject, input, TemplateRef } from '@angular/core';

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

@Directive({
selector: 'ng-template[appCardItem]',
exportAs: 'appCardItem',
standalone: true,
})
export class CardItemDirective<T> {
readonly appCardItem = input<T>();

tpl = inject(TemplateRef<CardItemContext<T>>);

static ngTemplateContextGuard<T>(
_dir: CardItemDirective<T>,
_ctx: unknown,
): _ctx is CardItemContext<T> {
return true;
}
}
Loading