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 #1166

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,12 +1,48 @@
import { Component, OnInit } from '@angular/core';
import { Component, inject, OnInit, Signal } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import { City } from '../../model/city.model';
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()"
[customClass]="'rgba(0, 0, 250, 0.1)'"
[templateRef]="city"
(add)="addCity()">
<img src="assets/img/city.png" width="200px" />
</app-card>

<ng-template #city let-city>
<app-list-item
[name]="city.name"
[id]="city.id"
(delete)="deleteCity(city.id)" />
</ng-template>
`,
imports: [CardComponent, ListItemComponent],
})
export class CityCardComponent implements OnInit {
constructor() {}
private http: FakeHttpService = inject(FakeHttpService);
private store: CityStore = inject(CityStore);

cities: Signal<City[]> = this.store.elements$;

ngOnInit(): void {
this.http.fetchCities$.subscribe((c) => this.store.addAll(c));
}

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

ngOnInit(): void {}
deleteCity(cityId: number) {
this.store.deleteOne(cityId);
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { Component, inject, OnInit, Signal } 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>
[list]="students()"
[customClass]="'rgba(0, 250, 0, 0.1)'"
[templateRef]="student"
(add)="addStudent()">
<img src="assets/img/student.webp" width="200px" />
</app-card>

<ng-template #student let-student>
<app-list-item
[name]="student.firstName"
[id]="student.id"
(delete)="deleteStudent(student.id)" />
</ng-template>
`,
styles: [
`
::ng-deep .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;
private http: FakeHttpService = inject(FakeHttpService);
private store: StudentStore = inject(StudentStore);

constructor(
private http: FakeHttpService,
private store: StudentStore,
) {}
students: Signal<Student[]> = this.store.elements$;

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

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

this.store.students$.subscribe((s) => (this.students = s));
deleteStudent(studentId: number) {
this.store.deleteOne(studentId);
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { Component, inject, OnInit, Signal } 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>
[list]="teachers()"
[customClass]="'rgba(250, 0, 0, 0.1)'"
[templateRef]="teacher"
(add)="addTeacher()">
<img src="assets/img/teacher.png" width="200px" />
</app-card>

<ng-template #teacher let-teacher>
<app-list-item
[name]="teacher.firstName"
[id]="teacher.id"
(delete)="deleteTeacher(teacher.id)" />
</ng-template>
`,
styles: [
`
::ng-deep .bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
imports: [CardComponent],
imports: [CardComponent, ListItemComponent],
})
export class TeacherCardComponent implements OnInit {
teachers: Teacher[] = [];
cardType = CardType.TEACHER;
private http: FakeHttpService = inject(FakeHttpService);
private store: TeacherStore = inject(TeacherStore);

constructor(
private http: FakeHttpService,
private store: TeacherStore,
) {}
teachers: Signal<Teacher[]> = this.store.elements$;

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

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

this.store.teachers$.subscribe((t) => (this.teachers = t));
deleteTeacher(teacherId: number) {
this.store.deleteOne(teacherId);
}
}
19 changes: 2 additions & 17 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,8 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { City } from '../model/city.model';
import { Store } from './store';

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

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

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

deleteOne(id: number) {
this.cities.next(this.cities.value.filter((s) => s.id !== id));
}
}
export class CityStore extends Store<City> {}
23 changes: 23 additions & 0 deletions apps/angular/1-projection/src/app/data-access/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { signal } from '@angular/core';

export class Store<T extends { id: number }> {
private elements = signal<T[]>([]);
elements$ = this.elements.asReadonly();

addAll(elements: T[]) {
this.elements.update((currentElements) => [
...currentElements,
...elements,
]);
}

addOne(element: T) {
this.elements.update((currentElements) => [...currentElements, element]);
}

deleteOne(id: number) {
this.elements.update((currentElements) =>
currentElements.filter((t) => t.id !== id),
);
}
}
19 changes: 2 additions & 17 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,8 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Student } from '../model/student.model';
import { Store } from './store';

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

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

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

deleteOne(id: number) {
this.students.next(this.students.value.filter((s) => s.id !== id));
}
}
export class StudentStore extends Store<Student> {}
19 changes: 2 additions & 17 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,8 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Teacher } from '../model/teacher.model';
import { Store } from './store';

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

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

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

deleteOne(id: number) {
this.teachers.next(this.teachers.value.filter((t) => t.id !== id));
}
}
export class TeacherStore extends Store<Teacher> {}
55 changes: 15 additions & 40 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,35 @@
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 { Component, 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" />
[style.background-color]="customClass()">
<ng-content select="img" />

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

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

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());
}
}
add = output<void>();
}
Loading
Loading