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

feat: angular projection challenge #1095

Open
wants to merge 2 commits 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,13 +1,41 @@
import { Component, OnInit } from '@angular/core';
import { Component, inject } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import { CardComponent } from '../../ui/card/card.component';
import { ListItemComponent } from '../../ui/list-item/list-item.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
template: `
<app-card [items]="cities()" (add)="onAdd()" class="bg-light-red">
<img src="../../../assets/img/city.png" width="200px" />
<ng-template #entry let-city>
<app-list-item (delete)="onDelete(city.id)">
{{ city.name }}
</app-list-item>
</ng-template>
</app-card>
`,
standalone: true,
imports: [],
imports: [CardComponent, ListItemComponent],
})
export class CityCardComponent implements OnInit {
constructor() {}
export class CityCardComponent {
private httpFakeService = inject(FakeHttpService);
private store = inject(CityStore);
cities = this.store.cities;

ngOnInit(): void {}
constructor() {
this.httpFakeService.fetchCities$.subscribe((t) => this.store.addAll(t));
}

onDelete(id: number): void {
this.store.deleteOne(id);
}

onAdd(): void {
this.store.addOne(randomCity());
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { Component, inject } from '@angular/core';
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
[list]="students"
[type]="cardType"
customClass="bg-light-green"></app-card>
<app-card [items]="students()" (add)="onAdd()" class="bg-light-red">
<img src="../../../assets/img/student.webp" width="200px" />
<ng-template #entry let-student>
<app-list-item (delete)="onDelete(student.id)">
{{ student.firstName }}
</app-list-item>
</ng-template>
</app-card>
`,
standalone: true,
styles: [
`
::ng-deep .bg-light-green {
.bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
],
imports: [CardComponent],
imports: [CardComponent, ListItemComponent],
})
export class StudentCardComponent implements OnInit {
students: Student[] = [];
cardType = CardType.STUDENT;
export class StudentCardComponent {
private httpFakeService = inject(FakeHttpService);
private store = inject(StudentStore);
students = this.store.students;

constructor(
private http: FakeHttpService,
private store: StudentStore,
) {}
constructor() {
this.httpFakeService.fetchStudents$.subscribe((t) => this.store.addAll(t));
}

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

this.store.students$.subscribe((s) => (this.students = s));
onAdd(): void {
this.store.addOne(randStudent());
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { ChangeDetectionStrategy, Component, inject } 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 { Teacher } from '../../model/teacher.model';
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>
<app-card [items]="teachers()" (add)="onAdd()" class="bg-light-red">
<img src="../../../assets/img/teacher.png" width="200px" />
<ng-template #entry let-teacher>
<app-list-item (delete)="onDelete(teacher.id)">
{{ 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);
}
`,
],
standalone: true,
imports: [CardComponent],
imports: [CardComponent, ListItemComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TeacherCardComponent implements OnInit {
teachers: Teacher[] = [];
cardType = CardType.TEACHER;
export class TeacherCardComponent {
private httpFakeService = inject(FakeHttpService);
private store = inject(TeacherStore);
teachers = this.store.teachers;

constructor(
private http: FakeHttpService,
private store: TeacherStore,
) {}
constructor() {
this.httpFakeService.fetchTeachers$.subscribe((t) => this.store.addAll(t));
}

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

this.store.teachers$.subscribe((t) => (this.teachers = t));
onAdd(): void {
this.store.addOne(randTeacher());
}
}
12 changes: 5 additions & 7 deletions apps/angular/1-projection/src/app/data-access/city.store.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Injectable, signal } from '@angular/core';
import { City } from '../model/city.model';

@Injectable({
providedIn: 'root',
})
export class CityStore {
private cities = new BehaviorSubject<City[]>([]);
cities$ = this.cities.asObservable();
cities = signal<City[]>([]);

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

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

deleteOne(id: number) {
this.cities.next(this.cities.value.filter((s) => s.id !== id));
this.cities.set(this.cities().filter((s) => s.id !== id));
}
}
12 changes: 5 additions & 7 deletions apps/angular/1-projection/src/app/data-access/student.store.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Injectable, signal } from '@angular/core';
import { Student } from '../model/student.model';

@Injectable({
providedIn: 'root',
})
export class StudentStore {
private students = new BehaviorSubject<Student[]>([]);
students$ = this.students.asObservable();
students = signal<Student[]>([]);

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

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

deleteOne(id: number) {
this.students.next(this.students.value.filter((s) => s.id !== id));
this.students.set(this.students().filter((s) => s.id !== id));
}
}
12 changes: 5 additions & 7 deletions apps/angular/1-projection/src/app/data-access/teacher.store.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Injectable, signal } from '@angular/core';
import { Teacher } from '../model/teacher.model';

@Injectable({
providedIn: 'root',
})
export class TeacherStore {
private teachers = new BehaviorSubject<Teacher[]>([]);
teachers$ = this.teachers.asObservable();
teachers = signal<Teacher[]>([]);

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

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

deleteOne(id: number) {
this.teachers.next(this.teachers.value.filter((t) => t.id !== id));
this.teachers.set(this.teachers().filter((t) => t.id !== id));
}
}
67 changes: 23 additions & 44 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,40 @@
import { NgFor, NgIf } from '@angular/common';
import { Component, 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,
TemplateRef,
} from '@angular/core';

@Component({
selector: 'app-card',
template: `
<div
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
[class]="customClass">
<img
*ngIf="type === CardType.TEACHER"
src="assets/img/teacher.png"
width="200px" />
<img
*ngIf="type === CardType.STUDENT"
src="assets/img/student.webp"
width="200px" />

<div class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4">
<ng-content select="img"></ng-content>
<section>
<app-list-item
*ngFor="let item of list"
[name]="item.firstName"
[id]="item.id"
[type]="type"></app-list-item>
@for (item of items(); track item.id) {
<ng-template
[ngTemplateOutlet]="templateRef()"
[ngTemplateOutletContext]="{ $implicit: item }"></ng-template>
}
</section>

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
(click)="add.emit()">
Add
</button>
</div>
`,
standalone: true,
imports: [NgIf, NgFor, ListItemComponent],
imports: [NgTemplateOutlet],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CardComponent {
@Input() list: any[] | null = null;
@Input() type!: CardType;
@Input() customClass = '';

CardType = CardType;

constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
) {}
export class CardComponent<T> {
items = input.required<any[]>();

addNewItem() {
if (this.type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (this.type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
}
templateRef = contentChild.required('entry', { read: TemplateRef });
add = output<void>();
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
import { Component, Input } from '@angular/core';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { Component, output } from '@angular/core';

@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>
`,
standalone: true,
})
export class ListItemComponent {
@Input() id!: number;
@Input() name!: string;
@Input() type!: CardType;

constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
) {}

delete(id: number) {
if (this.type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (this.type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
}
}
delete = output<void>();
}