Skip to content

Answer:1 #1352

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

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,9 +1,42 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { CityStore } from './../../data-access/city.store';
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { CardComponent } from '../../ui/card/card.component';
import { FakeHttpService, randomCity } from '../../data-access/fake-http.service';
import { CardType } from '../../model/card.model';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
template: `
<app-card
[store]="cityStore"
customClass="bg-light-green">
<img src="assets/img/city.png" ngProjectAs="card-header" width="200" height="200" />

<button
ngProjectAs="card-footer"
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
</app-card>
`,
imports: [CardComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent implements OnInit {

constructor(
protected cityStore: CityStore,
private fakeHttpService: FakeHttpService
) {}

cardType = CardType.CITY;

ngOnInit() {
this.fakeHttpService.fetchCities$.subscribe(c => this.cityStore.addAll(c));
}

addNewItem() {
this.cityStore.addOne(randomCity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
inject,
OnInit,
} from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { FakeHttpService, randStudent } from '../../data-access/fake-http.service';
import { StudentStore } from '../../data-access/student.store';
import { CardType } from '../../model/card.model';
import { CardComponent } from '../../ui/card/card.component';
Expand All @@ -13,9 +13,17 @@ import { CardComponent } from '../../ui/card/card.component';
selector: 'app-student-card',
template: `
<app-card
[list]="students()"
[type]="cardType"
customClass="bg-light-green" />
[store]="store"
customClass="bg-light-green">
<img src="assets/img/student.webp" ngProjectAs="card-header" width="200" height="200" />

<button
ngProjectAs="card-footer"
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
</app-card>
`,
styles: [
`
Expand All @@ -29,12 +37,15 @@ import { CardComponent } from '../../ui/card/card.component';
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);
protected store = inject(StudentStore);

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

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

addNewItem() {
this.store.addOne(randStudent());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, inject, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { FakeHttpService, randTeacher } from '../../data-access/fake-http.service';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { CardComponent } from '../../ui/card/card.component';
Expand All @@ -8,9 +8,18 @@ import { CardComponent } from '../../ui/card/card.component';
selector: 'app-teacher-card',
template: `
<app-card
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
[store]="store"
customClass="bg-light-red">
>
<img src="assets/img/teacher.png" ngProjectAs="card-header" width="200" height="200" />

<button
ngProjectAs="card-footer"
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
</app-card>
`,
styles: [
`
Expand All @@ -23,12 +32,15 @@ import { CardComponent } from '../../ui/card/card.component';
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);
protected store = inject(TeacherStore);

teachers = this.store.teachers;
cardType = CardType.TEACHER;

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

addNewItem() {
this.store.addOne(randTeacher());
}
}
5 changes: 4 additions & 1 deletion apps/angular/1-projection/src/app/data-access/city.store.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Injectable, signal } from '@angular/core';
import { City } from '../model/city.model';
import { Store } from './store';

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

getItems = () => this.cities;

addAll(cities: City[]) {
this.cities.set(cities);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const randTeacher = () => ({
firstName: randFirstName(),
lastName: randLastName(),
subject: rand(subject),
label() {
return this.firstName
},
});

const teachers: Teacher[] = [
Expand All @@ -38,6 +41,9 @@ export const randStudent = (): Student => ({
lastName: randLastName(),
mainTeacher: teachers[randNumber({ max: teachers.length - 1 })],
school: randWord(),
label() {
return this.firstName
},
});

const students: Student[] = [
Expand All @@ -54,6 +60,9 @@ export const randomCity = (): City => ({
id: factoryCity(),
name: randCity(),
country: randCountry(),
label() {
return this.name
},
});

const cities = [randomCity(), randomCity(), randomCity()];
Expand Down
7 changes: 7 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,7 @@
import { signal, WritableSignal } from '@angular/core';


export interface Store<T> {
getItems(): WritableSignal<T[]>;
deleteOne(id: number): void;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Injectable, signal } from '@angular/core';
import { Student } from '../model/student.model';
import { Store } from './store';

@Injectable({
providedIn: 'root',
})
export class StudentStore {
public students = signal<Student[]>([]);
export class StudentStore implements Store<Student> {
private students = signal<Student[]>([]);

getItems = () => this.students;

addAll(students: Student[]) {
this.students.set(students);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Injectable, signal } from '@angular/core';
import { Teacher } from '../model/teacher.model';
import { Store } from './store';

@Injectable({
providedIn: 'root',
})
export class TeacherStore {
public teachers = signal<Teacher[]>([]);
export class TeacherStore implements Store<Teacher> {
private teachers = signal<Teacher[]>([]);

getItems = () => this.teachers;

addAll(teachers: Teacher[]) {
this.teachers.set(teachers);
Expand Down
4 changes: 4 additions & 0 deletions apps/angular/1-projection/src/app/model/card.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export enum CardType {
STUDENT,
CITY,
}

export interface CardItem {
label(): string
}
4 changes: 3 additions & 1 deletion apps/angular/1-projection/src/app/model/city.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface City {
import { CardItem } from "./card.model";

export interface City extends CardItem {
id: number;
name: string;
country: string;
Expand Down
3 changes: 2 additions & 1 deletion apps/angular/1-projection/src/app/model/student.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CardItem } from './card.model';
import { Teacher } from './teacher.model';

export interface Student {
export interface Student extends CardItem {
id: number;
firstName: string;
lastName: string;
Expand Down
4 changes: 3 additions & 1 deletion apps/angular/1-projection/src/app/model/teacher.model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CardItem } from "./card.model";

export const subject = [
'Sciences',
'History',
Expand All @@ -7,7 +9,7 @@ export const subject = [
] as const;
export type Subject = (typeof subject)[number];

export interface Teacher {
export interface Teacher extends CardItem {
id: number;
firstName: string;
lastName: string;
Expand Down
46 changes: 14 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,58 +1,40 @@
import { NgOptimizedImage } from '@angular/common';
import { Component, inject, 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 { Component, WritableSignal, input, signal } from '@angular/core';
import { CardType } from '../../model/card.model';
import { ListItemComponent } from '../list-item/list-item.component';
import { Store } from '../../data-access/store';

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

<ng-content select="card-header"></ng-content>

<section>
@for (item of list(); track item) {
<app-list-item
[name]="item.firstName"
[name]="item.label()"
[id]="item.id"
[type]="type()"></app-list-item>
[store]="store()"></app-list-item>
}
</section>

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>

<ng-content select="card-footer"></ng-content>
</div>
`,
imports: [ListItemComponent, NgOptimizedImage],
imports: [ListItemComponent],
})
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);

readonly list = input<any[] | null>(null);
readonly type = input.required<CardType>();

readonly customClass = input('');
readonly store = input.required<Store<any>>();
protected list: WritableSignal<any[]> = signal([]);

CardType = CardType;

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
ngOnInit() {
this.list = this.store().getItems();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import {
ChangeDetectionStrategy,
Component,
inject,
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 { Store } from '../../data-access/store';

@Component({
selector: 'app-list-item',
Expand All @@ -22,19 +19,12 @@ import { CardType } from '../../model/card.model';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);

readonly id = input.required<number>();
readonly name = input.required<string>();
readonly type = input.required<CardType>();
readonly store = input.required<Store<any>>();

delete(id: number) {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
}
this.store().deleteOne(id);
}
}
Loading