Skip to content

Commit 86688b2

Browse files
Fakhriddin AbdurakhimovFakhriddin Abdurakhimov
authored andcommitted
feat(Answer:1): refactor according to comments
1 parent c750d13 commit 86688b2

File tree

8 files changed

+64
-86
lines changed

8 files changed

+64
-86
lines changed

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, computed, inject } from '@angular/core';
22
import { CityStore } from '../../data-access/city.store';
33
import {
44
FakeHttpService,
@@ -11,7 +11,7 @@ import { CardComponent } from '../../ui/card/card.component';
1111
selector: 'app-city-card',
1212
template: `
1313
<app-card
14-
[list]="cityListItems"
14+
[list]="cityListItems()"
1515
class="bg-light-green"
1616
(addNewItemEvent)="handleAddNewItemEvent()"
1717
(deleteItemEvent)="handleDeleteItemEvent($event)">
@@ -30,23 +30,18 @@ import { CardComponent } from '../../ui/card/card.component';
3030
imports: [CardComponent],
3131
})
3232
export class CityCardComponent implements OnInit {
33-
cityListItems: CardListItem[] = [];
33+
private http = inject(FakeHttpService);
34+
private store = inject(CityStore);
3435

35-
constructor(
36-
private http: FakeHttpService,
37-
private store: CityStore,
38-
) {}
36+
cityListItems = computed<CardListItem[]>(() =>
37+
this.store.cities().map((city) => ({
38+
name: city.name,
39+
id: city.id,
40+
})),
41+
);
3942

4043
ngOnInit(): void {
4144
this.http.fetchCities$.subscribe((e) => this.store.addAll(e));
42-
this.store.cities$.subscribe((cities) => {
43-
this.cityListItems = cities.map((city) => {
44-
return {
45-
name: city.name,
46-
id: city.id,
47-
};
48-
});
49-
});
5045
}
5146

5247
handleAddNewItemEvent() {

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, computed, inject } from '@angular/core';
22
import {
33
FakeHttpService,
44
randStudent,
@@ -11,7 +11,7 @@ import { CardComponent } from '../../ui/card/card.component';
1111
selector: 'app-student-card',
1212
template: `
1313
<app-card
14-
[list]="studentListItems"
14+
[list]="studentListItems()"
1515
class="bg-light-green"
1616
(addNewItemEvent)="handleAddNewItemEvent()"
1717
(deleteItemEvent)="handleDeleteItemEvent($event)">
@@ -30,24 +30,20 @@ import { CardComponent } from '../../ui/card/card.component';
3030
imports: [CardComponent],
3131
})
3232
export class StudentCardComponent implements OnInit {
33-
studentListItems: CardListItem[] = [];
33+
private http = inject(FakeHttpService);
34+
private store = inject(StudentStore);
3435

35-
constructor(
36-
private http: FakeHttpService,
37-
private store: StudentStore,
38-
) {}
36+
students = this.store.students;
37+
38+
studentListItems = computed<CardListItem[]>(() =>
39+
this.store.students().map((student) => ({
40+
name: student.firstName,
41+
id: student.id,
42+
})),
43+
);
3944

4045
ngOnInit(): void {
4146
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));
42-
43-
this.store.students$.subscribe((students) => {
44-
this.studentListItems = students.map((student) => {
45-
return {
46-
name: student.firstName,
47-
id: student.id,
48-
};
49-
});
50-
});
5147
}
5248

5349
handleAddNewItemEvent() {

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, computed, inject } from '@angular/core';
22
import {
33
FakeHttpService,
44
randTeacher,
@@ -11,7 +11,7 @@ import { CardComponent } from '../../ui/card/card.component';
1111
selector: 'app-teacher-card',
1212
template: `
1313
<app-card
14-
[list]="teacherListItems"
14+
[list]="teacherListItems()"
1515
class="bg-light-red"
1616
(addNewItemEvent)="handleAddNewItemEvent()"
1717
(deleteItemEvent)="handleDeleteItemEvent($event)">
@@ -30,24 +30,17 @@ import { CardComponent } from '../../ui/card/card.component';
3030
imports: [CardComponent],
3131
})
3232
export class TeacherCardComponent implements OnInit {
33-
teacherListItems: CardListItem[] = [];
33+
private http = inject(FakeHttpService);
34+
private store = inject(TeacherStore);
3435

35-
constructor(
36-
private http: FakeHttpService,
37-
private store: TeacherStore,
38-
) {}
36+
teacherListItems = computed<CardListItem[]>(() =>
37+
this.store
38+
.teachers()
39+
.map((teacher) => ({ name: teacher.firstName, id: teacher.id })),
40+
);
3941

4042
ngOnInit(): void {
4143
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));
42-
43-
this.store.teachers$.subscribe((teachers) => {
44-
this.teacherListItems = teachers.map((teacher) => {
45-
return {
46-
name: teacher.firstName,
47-
id: teacher.id,
48-
};
49-
});
50-
});
5144
}
5245

5346
handleAddNewItemEvent() {
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import { Injectable } from '@angular/core';
2-
import { BehaviorSubject } from 'rxjs';
1+
import { Injectable, signal } from '@angular/core';
32
import { City } from '../model/city.model';
43

54
@Injectable({
65
providedIn: 'root',
76
})
87
export class CityStore {
9-
private cities = new BehaviorSubject<City[]>([]);
10-
cities$ = this.cities.asObservable();
8+
public cities = signal<City[]>([]);
119

1210
addAll(cities: City[]) {
13-
this.cities.next(cities);
11+
this.cities.set(cities);
1412
}
1513

16-
addOne(student: City) {
17-
this.cities.next([...this.cities.value, student]);
14+
addOne(city: City) {
15+
this.cities.set([...this.cities(), city]);
1816
}
1917

2018
deleteOne(id: number) {
21-
this.cities.next(this.cities.value.filter((s) => s.id !== id));
19+
this.cities.set(this.cities().filter((s) => s.id !== id));
2220
}
2321
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import { Injectable } from '@angular/core';
2-
import { BehaviorSubject } from 'rxjs';
1+
import { Injectable, signal } from '@angular/core';
32
import { Student } from '../model/student.model';
43

54
@Injectable({
65
providedIn: 'root',
76
})
87
export class StudentStore {
9-
private students = new BehaviorSubject<Student[]>([]);
10-
students$ = this.students.asObservable();
8+
public students = signal<Student[]>([]);
119

1210
addAll(students: Student[]) {
13-
this.students.next(students);
11+
this.students.set(students);
1412
}
1513

1614
addOne(student: Student) {
17-
this.students.next([...this.students.value, student]);
15+
this.students.set([...this.students(), student]);
1816
}
1917

2018
deleteOne(id: number) {
21-
this.students.next(this.students.value.filter((s) => s.id !== id));
19+
this.students.set(this.students().filter((s) => s.id !== id));
2220
}
2321
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import { Injectable } from '@angular/core';
2-
import { BehaviorSubject } from 'rxjs';
1+
import { Injectable, signal } from '@angular/core';
32
import { Teacher } from '../model/teacher.model';
43

54
@Injectable({
65
providedIn: 'root',
76
})
87
export class TeacherStore {
9-
private teachers = new BehaviorSubject<Teacher[]>([]);
10-
teachers$ = this.teachers.asObservable();
8+
public teachers = signal<Teacher[]>([]);
119

1210
addAll(teachers: Teacher[]) {
13-
this.teachers.next(teachers);
11+
this.teachers.set(teachers);
1412
}
1513

1614
addOne(teacher: Teacher) {
17-
this.teachers.next([...this.teachers.value, teacher]);
15+
this.teachers.set([...this.teachers(), teacher]);
1816
}
1917

2018
deleteOne(id: number) {
21-
this.teachers.next(this.teachers.value.filter((t) => t.id !== id));
19+
this.teachers.set(this.teachers().filter((t) => t.id !== id));
2220
}
2321
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { NgFor } from '@angular/common';
2-
import { Component, EventEmitter, Input, Output } from '@angular/core';
1+
import { Component, input, output } from '@angular/core';
32
import { CardListItem } from '../../model/card.model';
43
import { ListItemComponent } from '../list-item/list-item.component';
54

@@ -8,27 +7,28 @@ import { ListItemComponent } from '../list-item/list-item.component';
87
template: `
98
<ng-content select="[card-img]"></ng-content>
109
<section>
11-
<app-list-item
12-
*ngFor="let item of list"
13-
[name]="item.name"
14-
[id]="item.id"
15-
(deleteItemEvent)="handleDeleteItemEvent($event)"></app-list-item>
10+
@for (item of list(); track $index) {
11+
<app-list-item
12+
[name]="item.name"
13+
[id]="item.id"
14+
(deleteItemEvent)="handleDeleteItemEvent($event)" />
15+
}
1616
</section>
1717
<button
1818
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
1919
(click)="addNewItem()">
2020
Add
2121
</button>
2222
`,
23-
imports: [NgFor, ListItemComponent],
23+
imports: [ListItemComponent],
2424
host: {
2525
class: 'border-2 border-black rounded-md p-4 w-fit flex flex-col gap-3',
2626
},
2727
})
2828
export class CardComponent {
29-
@Input() list: CardListItem[] | null = null;
30-
@Output() deleteItemEvent = new EventEmitter<number>();
31-
@Output() addNewItemEvent = new EventEmitter<void>();
29+
list = input<CardListItem[]>([]);
30+
deleteItemEvent = output<number>();
31+
addNewItemEvent = output<void>();
3232

3333
addNewItem() {
3434
this.addNewItemEvent.emit();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { Component, EventEmitter, Input, Output } from '@angular/core';
1+
import { Component, input, output } from '@angular/core';
22

33
@Component({
44
selector: 'app-list-item',
55
template: `
66
<div class="border-grey-300 flex justify-between border px-2 py-1">
7-
{{ name }}
8-
<button (click)="delete(id)">
7+
{{ name() }}
8+
<button (click)="delete(id())">
99
<img class="h-5" src="assets/svg/trash.svg" />
1010
</button>
1111
</div>
1212
`,
1313
standalone: true,
1414
})
1515
export class ListItemComponent {
16-
@Input() id!: number;
17-
@Input() name!: string;
18-
@Output() deleteItemEvent = new EventEmitter<number>();
16+
id = input.required<number>();
17+
name = input.required<string>();
18+
deleteItemEvent = output<number>();
1919

2020
delete(id: number) {
2121
this.deleteItemEvent.emit(id);

0 commit comments

Comments
 (0)