forked from tomalaforge/angular-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent-card.component.ts
More file actions
58 lines (52 loc) · 1.47 KB
/
student-card.component.ts
File metadata and controls
58 lines (52 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
OnInit,
} from '@angular/core';
import {
FakeHttpService,
randStudent,
} from '../../data-access/fake-http.service';
import { StudentStore } from '../../data-access/student.store';
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()"
[templateType]="student"
(itemAdded)="onStudentAdded()"
customClass="bg-light-green">
<img
ngSrc="assets/img/student.webp"
width="200"
height="200"
ngProjectAs="card-image" />
</app-card>
<ng-template #student let-item>
<app-list-item
[name]="item.firstName"
[id]="item.id"
(deleted)="onStudentDeleted($event)" />
</ng-template>
`,
imports: [CardComponent, ListItemComponent, NgOptimizedImage],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);
students = this.store.students.asReadonly();
ngOnInit(): void {
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));
}
onStudentAdded() {
this.store.addOne(randStudent());
}
onStudentDeleted(id: number) {
this.store.deleteOne(id);
}
}