Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer: 1 #1226

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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,42 @@
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 { CardRowDirective } from '../../ui/card/card-row.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 [items]="cities()" (addNewItem)="onNewItemAdded()">
<img ngSrc="assets/img/student.webp" width="200" height="200" />

<ng-template [cardRow]="cities()" let-city>
<app-list-item (delete)="onDelete(city.id)">
<div>{{ city.name }}</div>
</app-list-item>
</ng-template>
</app-card>
`,
imports: [
CardComponent,
NgOptimizedImage,
CardRowDirective,
ListItemComponent,
],
providers: [CityStore],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent {
private store = inject(CityStore);

cities = this.store.cities;

onNewItemAdded() {
this.store.addOne();
}

onDelete(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
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 { StudentStore } from '../../data-access/student.store';
import { CardType } from '../../model/card.model';
import { CardRowDirective } from '../../ui/card/card-row.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" />
class="bg-light-green"
[items]="students()"
(addNewItem)="onNewItemAdded()">
<img ngSrc="assets/img/student.webp" width="200" height="200" />

<ng-template [cardRow]="students()" let-student>
<app-list-item (delete)="onDelete(student.id)">
<div>{{ student.firstName }}</div>
</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,
CardRowDirective,
ListItemComponent,
],
providers: [StudentStore],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
export class StudentCardComponent {
private store = inject(StudentStore);

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

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

onDelete(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
import { Component, inject, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { NgOptimizedImage } from '@angular/common';
import { Component, inject } from '@angular/core';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { CardRowDirective } from '../../ui/card/card-row.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>
(addNewItem)="onNewItemAdded()"
[items]="teachers()"
class="bg-light-red">
<img ngSrc="assets/img/teacher.png" priority width="200" height="200" />

<ng-template [cardRow]="teachers()" let-teacher>
<app-list-item (delete)="onDelete(teacher.id)">
<div>{{ teacher.firstName }}</div>
</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,
CardRowDirective,
ListItemComponent,
],
providers: [TeacherStore],
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
export class TeacherCardComponent {
private store = inject(TeacherStore);
protected readonly teachers = this.store.teachers;

teachers = this.store.teachers;
cardType = CardType.TEACHER;
onNewItemAdded() {
this.store.addOne();
}

ngOnInit(): void {
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));
onDelete(id: number) {
this.store.deleteOne(id);
}
}
22 changes: 14 additions & 8 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,27 @@
import { Injectable, signal } from '@angular/core';
import { inject, Injectable, signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { tap } from 'rxjs';
import { City } from '../model/city.model';
import { FakeHttpService, randomCity } from './fake-http.service';

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

addAll(cities: City[]) {
this.cities.set(cities);
}
private _cities = signal<City[]>([]);
public cities = this._cities.asReadonly();

fetchCities = toSignal(
this.http.fetchCities$.pipe(tap((cities) => this._cities.set(cities))),
);

addOne(student: City) {
this.cities.set([...this.cities(), student]);
addOne() {
this._cities.set([...this._cities(), randomCity()]);
}

deleteOne(id: number) {
this.cities.set(this.cities().filter((s) => s.id !== id));
this._cities.set(this._cities().filter((s) => s.id !== id));
}
}
28 changes: 19 additions & 9 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,31 @@
import { Injectable, signal } from '@angular/core';
import { inject, Injectable, signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { tap } from 'rxjs';
import { Student } from '../model/student.model';
import { FakeHttpService, randStudent } from './fake-http.service';

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

public readonly students = this._students.asReadonly();

fetchStudents = toSignal(
this.http.fetchStudents$.pipe(
tap((students) => this._students.set(students)),
),
);

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

addOne(student: Student) {
this.students.set([...this.students(), student]);
addOne() {
this._students.update((students) => [...students, randStudent()]);
}

deleteOne(id: number) {
this.students.set(this.students().filter((s) => s.id !== id));
this._students.set(this._students().filter((s) => s.id !== id));
}
}
29 changes: 18 additions & 11 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, signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { tap } from 'rxjs';
import { Teacher } from '../model/teacher.model';
import { FakeHttpService, randTeacher } from './fake-http.service';

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

addAll(teachers: Teacher[]) {
this.teachers.set(teachers);
}
public readonly teachers = this._teachers.asReadonly();

fetchTeachers = toSignal(
this.http.fetchTeachers$.pipe(
tap(() => console.log('fetchTeachers')),
tap((teachers) => this._teachers.set(teachers)),
),
);

addOne(teacher: Teacher) {
this.teachers.set([...this.teachers(), teacher]);
addOne() {
this._teachers.update((teachers) => [...teachers, randTeacher()]);
}

deleteOne(id: number) {
this.teachers.set(this.teachers().filter((t) => t.id !== id));
this._teachers.set(this._teachers().filter((t) => t.id !== id));
}
}
17 changes: 17 additions & 0 deletions apps/angular/1-projection/src/app/ui/card/card-row.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Directive, input } from '@angular/core';

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

@Directive({ selector: 'ng-template[cardRow]' })
export class CardRowDirective<T> {
public cardRow = input.required<T[]>();

static ngTemplateContextGuard<T>(
dir: CardRowDirective<T>,
ctx: any,
): ctx is CardRowContext<T> {
return true;
}
}
Loading
Loading