Skip to content

Commit 302e82f

Browse files
chore: projection task done
1 parent dbf0e8a commit 302e82f

File tree

6 files changed

+77
-44
lines changed

6 files changed

+77
-44
lines changed
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1-
import { ChangeDetectionStrategy, Component } from '@angular/core';
1+
import {
2+
ChangeDetectionStrategy,
3+
Component,
4+
inject,
5+
OnInit,
6+
} from '@angular/core';
7+
import { CityStore } from '../../data-access/city.store';
8+
import { FakeHttpService } from '../../data-access/fake-http.service';
9+
import { CardType } from '../../model/card.model';
10+
import { CardComponent } from '../../ui/card/card.component';
211

312
@Component({
413
selector: 'app-city-card',
5-
template: 'TODO City',
6-
imports: [],
14+
template: `
15+
<app-card
16+
[list]="cities()"
17+
[type]="cardType"
18+
backgroundColor="rgba(39, 85, 129, 0.1)" />
19+
`,
20+
imports: [CardComponent],
721
changeDetection: ChangeDetectionStrategy.OnPush,
822
})
9-
export class CityCardComponent {}
23+
export class CityCardComponent implements OnInit {
24+
private http = inject(FakeHttpService);
25+
private store = inject(CityStore);
26+
27+
cities = this.store.cities;
28+
cardType = CardType.CITY;
29+
30+
ngOnInit(): void {
31+
this.http.fetchCities$.subscribe((c) => this.store.addAll(c));
32+
}
33+
}

apps/angular/1-projection/src/app/component/student-card/student-card.component.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@ import { CardComponent } from '../../ui/card/card.component';
1515
<app-card
1616
[list]="students()"
1717
[type]="cardType"
18-
customClass="bg-light-green" />
18+
backgroundColor="rgba(0, 250, 0, 0.1)" />
1919
`,
20-
styles: [
21-
`
22-
::ng-deep .bg-light-green {
23-
background-color: rgba(0, 250, 0, 0.1);
24-
}
25-
`,
26-
],
2720
imports: [CardComponent],
2821
changeDetection: ChangeDetectionStrategy.OnPush,
2922
})

apps/angular/1-projection/src/app/component/teacher-card/teacher-card.component.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,8 @@ import { CardComponent } from '../../ui/card/card.component';
1010
<app-card
1111
[list]="teachers()"
1212
[type]="cardType"
13-
customClass="bg-light-red"></app-card>
13+
backgroundColor="rgba(250, 0, 0, 0.1)"></app-card>
1414
`,
15-
styles: [
16-
`
17-
::ng-deep .bg-light-red {
18-
background-color: rgba(250, 0, 0, 0.1);
19-
}
20-
`,
21-
],
2215
imports: [CardComponent],
2316
})
2417
export class TeacherCardComponent implements OnInit {

apps/angular/1-projection/src/app/data-access/city.store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { City } from '../model/city.model';
55
providedIn: 'root',
66
})
77
export class CityStore {
8-
private cities = signal<City[]>([]);
8+
public cities = signal<City[]>([]);
99

1010
addAll(cities: City[]) {
1111
this.cities.set(cities);
Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { NgOptimizedImage } from '@angular/common';
2-
import { Component, inject, input } from '@angular/core';
3-
import { randStudent, randTeacher } from '../../data-access/fake-http.service';
2+
import { Component, computed, inject, input } from '@angular/core';
3+
import { CityStore } from '../../data-access/city.store';
4+
import {
5+
randomCity,
6+
randStudent,
7+
randTeacher,
8+
} from '../../data-access/fake-http.service';
49
import { StudentStore } from '../../data-access/student.store';
510
import { TeacherStore } from '../../data-access/teacher.store';
611
import { CardType } from '../../model/card.model';
@@ -11,18 +16,13 @@ import { ListItemComponent } from '../list-item/list-item.component';
1116
template: `
1217
<div
1318
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
14-
[class]="customClass()">
15-
@if (type() === CardType.TEACHER) {
16-
<img ngSrc="assets/img/teacher.png" width="200" height="200" />
17-
}
18-
@if (type() === CardType.STUDENT) {
19-
<img ngSrc="assets/img/student.webp" width="200" height="200" />
20-
}
19+
[style.backgroundColor]="backgroundColor()">
20+
<img [ngSrc]="imageSrc()" width="200" height="200" />
2121
2222
<section>
2323
@for (item of list(); track item) {
2424
<app-list-item
25-
[name]="item.firstName"
25+
[name]="itemName(item)"
2626
[id]="item.id"
2727
[type]="type()"></app-list-item>
2828
}
@@ -40,19 +40,39 @@ import { ListItemComponent } from '../list-item/list-item.component';
4040
export class CardComponent {
4141
private teacherStore = inject(TeacherStore);
4242
private studentStore = inject(StudentStore);
43+
private cityStore = inject(CityStore);
4344

4445
readonly list = input<any[] | null>(null);
4546
readonly type = input.required<CardType>();
46-
readonly customClass = input('');
47+
readonly backgroundColor = input('');
4748

4849
CardType = CardType;
4950

51+
addHandler: Record<CardType, () => void> = {
52+
[CardType.TEACHER]: () => this.teacherStore.addOne(randTeacher()),
53+
[CardType.STUDENT]: () => this.studentStore.addOne(randStudent()),
54+
[CardType.CITY]: () => this.cityStore.addOne(randomCity()),
55+
};
56+
57+
nameLookup: Record<CardType, (item: any) => string> = {
58+
[CardType.TEACHER]: (item: any) => item.firstName,
59+
[CardType.STUDENT]: (item: any) => item.firstName,
60+
[CardType.CITY]: (item: any) => item.name,
61+
};
62+
5063
addNewItem() {
51-
const type = this.type();
52-
if (type === CardType.TEACHER) {
53-
this.teacherStore.addOne(randTeacher());
54-
} else if (type === CardType.STUDENT) {
55-
this.studentStore.addOne(randStudent());
56-
}
64+
this.addHandler[this.type()]();
65+
}
66+
67+
itemName(item: any) {
68+
return this.nameLookup[this.type()](item);
5769
}
70+
71+
imageLookup: Record<CardType, string> = {
72+
[CardType.TEACHER]: 'assets/img/teacher.png',
73+
[CardType.STUDENT]: 'assets/img/student.webp',
74+
[CardType.CITY]: 'assets/img/city.png',
75+
};
76+
77+
imageSrc = computed(() => this.imageLookup[this.type()]);
5878
}

apps/angular/1-projection/src/app/ui/list-item/list-item.component.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
inject,
55
input,
66
} from '@angular/core';
7+
import { CityStore } from '../../data-access/city.store';
78
import { StudentStore } from '../../data-access/student.store';
89
import { TeacherStore } from '../../data-access/teacher.store';
910
import { CardType } from '../../model/card.model';
@@ -23,17 +24,19 @@ import { CardType } from '../../model/card.model';
2324
export class ListItemComponent {
2425
private teacherStore = inject(TeacherStore);
2526
private studentStore = inject(StudentStore);
27+
private cityStore = inject(CityStore);
2628

2729
readonly id = input.required<number>();
2830
readonly name = input.required<string>();
2931
readonly type = input.required<CardType>();
3032

33+
deleteItem: Record<CardType, (id: number) => void> = {
34+
[CardType.TEACHER]: (id: number) => this.teacherStore.deleteOne(id),
35+
[CardType.STUDENT]: (id: number) => this.studentStore.deleteOne(id),
36+
[CardType.CITY]: (id: number) => this.cityStore.deleteOne(id),
37+
};
38+
3139
delete(id: number) {
32-
const type = this.type();
33-
if (type === CardType.TEACHER) {
34-
this.teacherStore.deleteOne(id);
35-
} else if (type === CardType.STUDENT) {
36-
this.studentStore.deleteOne(id);
37-
}
40+
this.deleteItem[this.type()](id);
3841
}
3942
}

0 commit comments

Comments
 (0)