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 projection with signals and all v17 features #775

Closed
wants to merge 3 commits into from
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,40 +1,52 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { AsyncPipe } from '@angular/common';
import { ChangeDetectionStrategy, 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 { 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"></app-card>
<app-card [items]="students()" (add)="addStudent()" class="bg-light-green">
<img src="assets/img/student.webp" width="200px" />
<ng-template [cardRow]="students()" let-student>
<app-list-item (delete)="deleteStudent(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, AsyncPipe, CardRowDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
students: Student[] = [];
cardType = CardType.STUDENT;
export class StudentCardComponent {
private http = inject(FakeHttpService);
private store = inject(StudentStore);

constructor(
private http: FakeHttpService,
private store: StudentStore,
) {}
students = this.store.students;

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

addStudent() {
this.store.addOne(randStudent());
}

this.store.students$.subscribe((s) => (this.students = s));
deleteStudent(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { AsyncPipe } from '@angular/common';
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 { 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>
<app-card [items]="teachers()" class="bg-light-red" (add)="addTeacher()">
<img src="assets/img/teacher.png" width="200px" />
<ng-template [cardRow]="teachers()" let-teacher>
<app-list-item (delete)="deleteTeacher(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: [ListItemComponent, AsyncPipe, CardRowDirective, CardComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TeacherCardComponent implements OnInit {
teachers: Teacher[] = [];
cardType = CardType.TEACHER;
export class TeacherCardComponent {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);

constructor(
private http: FakeHttpService,
private store: TeacherStore,
) {}
teachers = this.store.teachers;

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

addTeacher() {
this.store.addOne(randTeacher());
}

this.store.teachers$.subscribe((t) => (this.teachers = t));
deleteTeacher(id: number) {
this.store.deleteOne(id);
}
}
12 changes: 5 additions & 7 deletions apps/angular/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]);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use update here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use "update" too to get previous value and add new student, but since u current have the students list, u can also use the set() method


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/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));
}
}
5 changes: 0 additions & 5 deletions apps/angular/projection/src/app/model/card.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
export enum CardType {
TEACHER,
STUDENT,
CITY,
}
17 changes: 17 additions & 0 deletions apps/angular/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';

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

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

static ngTemplateContextGuard<TContext>(
dir: CardRowDirective<TContext>,
ctx: unknown,
): ctx is CardRowContext<TContext> {
return true;
}
}
82 changes: 32 additions & 50 deletions apps/angular/projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@
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';
import { CardRowDirective } from './card-row.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">
<img
*ngIf="type === CardType.TEACHER"
src="assets/img/teacher.png"
width="200px" />
<img
*ngIf="type === CardType.STUDENT"
src="assets/img/student.webp"
width="200px" />
<ng-content select="img" />

<section>
<app-list-item
*ngFor="let item of list"
[name]="item.firstName"
[id]="item.id"
[type]="type"></app-list-item>
</section>
<section>
@for (item of items(); track item.id) {
<ng-template
[ngTemplateOutlet]="rowTemplate()"
[ngTemplateOutletContext]="{ $implicit: item }"></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)="add.emit()">
Add
</button>
`,
standalone: true,
imports: [NgIf, NgFor, ListItemComponent],
imports: [NgTemplateOutlet],
host: {
class: 'border-2 border-black rounded-md p-4 w-fit flex flex-col gap-3',
},
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CardComponent {
@Input() list: any[] | null = null;
@Input() type!: CardType;
@Input() customClass = '';
export class CardComponent<T extends { id: number }> {
items = input.required<T[]>();
add = output();

CardType = CardType;

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

addNewItem() {
if (this.type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (this.type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
}
rowTemplate = contentChild.required(CardRowDirective, { read: TemplateRef });
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
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 { ChangeDetectionStrategy, 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,
changeDetection: ChangeDetectionStrategy.OnPush,
})
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>();
}
Loading