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 - Resolve projection challenge #1106

Open
wants to merge 3 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,62 @@
import { Component, OnInit } from '@angular/core';
import {
Component,
OnInit,
signal,
ViewEncapsulation,
WritableSignal,
} from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import { CardType } from '../../model/card.model';
import { City } from '../../model/city.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
template: `
<app-card
[list]="cities()"
[type]="cardType"
customClass="bg-light-blue"
(addItem)="addCity()"
(delete)="deleteItem($event)">
<img src="assets/img/city.png" width="200px" />
</app-card>
`,
standalone: true,
imports: [],
styles: [
`
.bg-light-blue {
background-color: rgba(0, 0, 250, 0.1);
}
`,
],
encapsulation: ViewEncapsulation.None,
imports: [CardComponent],
})
export class CityCardComponent implements OnInit {
constructor() {}
cities: WritableSignal<City[]> = signal([]);
cardType = CardType.CITY;

ngOnInit(): void {}
constructor(
private http: FakeHttpService,
private store: CityStore,
) {}

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

this.store.cities$.subscribe((c) => this.cities.set(c));
}

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

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import {
Component,
OnInit,
signal,
ViewEncapsulation,
WritableSignal,
} 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';
Expand All @@ -9,22 +18,27 @@ import { CardComponent } from '../../ui/card/card.component';
selector: 'app-student-card',
template: `
<app-card
[list]="students"
[list]="students()"
[type]="cardType"
customClass="bg-light-green"></app-card>
customClass="bg-light-green"
(addItem)="addStudent()"
(delete)="deleteItem($event)">
<img src="assets/img/student.webp" width="200px" />
</app-card>
`,
standalone: true,
styles: [
`
::ng-deep .bg-light-green {
.bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
],
encapsulation: ViewEncapsulation.None,
imports: [CardComponent],
})
export class StudentCardComponent implements OnInit {
students: Student[] = [];
students: WritableSignal<Student[]> = signal([]);
cardType = CardType.STUDENT;

constructor(
Expand All @@ -35,6 +49,14 @@ export class StudentCardComponent implements OnInit {
ngOnInit(): void {
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));

this.store.students$.subscribe((s) => (this.students = s));
this.store.students$.subscribe((s) => this.students.set(s));
}

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

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import {
Component,
OnInit,
signal,
ViewEncapsulation,
WritableSignal,
} 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';
Expand All @@ -9,22 +18,27 @@ import { CardComponent } from '../../ui/card/card.component';
selector: 'app-teacher-card',
template: `
<app-card
[list]="teachers"
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
customClass="bg-light-red"
(addItem)="addTeacher()"
(delete)="deleteItem($event)">
<img src="assets/img/teacher.png" width="200px" />
</app-card>
`,
styles: [
`
::ng-deep .bg-light-red {
.bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
encapsulation: ViewEncapsulation.None,
standalone: true,
imports: [CardComponent],
})
export class TeacherCardComponent implements OnInit {
teachers: Teacher[] = [];
teachers: WritableSignal<Teacher[]> = signal([]);
cardType = CardType.TEACHER;

constructor(
Expand All @@ -35,6 +49,14 @@ export class TeacherCardComponent implements OnInit {
ngOnInit(): void {
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));

this.store.teachers$.subscribe((t) => (this.teachers = t));
this.store.teachers$.subscribe((t) => this.teachers.set(t));
}

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

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
4 changes: 2 additions & 2 deletions apps/angular/1-projection/src/app/data-access/city.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class CityStore {
this.cities.next(cities);
}

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

deleteOne(id: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const students: Student[] = [
randStudent(),
randStudent(),
randStudent(),
randStudent(),
];

const factoryCity = incrementalNumber();
Expand All @@ -56,7 +55,7 @@ export const randomCity = (): City => ({
country: randCountry(),
});

const cities = [randomCity(), randomCity(), randomCity()];
const cities = [randomCity(), randomCity(), randomCity(), randomCity()];

@Injectable({
providedIn: 'root',
Expand Down
47 changes: 15 additions & 32 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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 { NgFor } from '@angular/common';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { CardType } from '../../model/card.model';
import { ListItemComponent } from '../list-item/list-item.component';

Expand All @@ -12,50 +9,36 @@ import { ListItemComponent } from '../list-item/list-item.component';
<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></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 list; track item.id) {
<app-list-item
[name]="item.firstName || item.name"
[id]="item.id"
[type]="type"
(deleteItem)="delete.emit($event)"></app-list-item>
}
</section>

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

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());
}
}
constructor() {}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Component, Input } from '@angular/core';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { CardType } from '../../model/card.model';

@Component({
selector: 'app-list-item',
template: `
<div class="border-grey-300 flex justify-between border px-2 py-1">
{{ name }}
<button (click)="delete(id)">
<button (click)="deleteItem.emit(id)">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
</div>
Expand All @@ -19,17 +17,7 @@ export class ListItemComponent {
@Input() id!: number;
@Input() name!: string;
@Input() type!: CardType;
@Output() deleteItem = new EventEmitter<number>();

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);
}
}
constructor() {}
}
Loading