-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathstudent-card.component.ts
More file actions
63 lines (58 loc) · 1.65 KB
/
student-card.component.ts
File metadata and controls
63 lines (58 loc) · 1.65 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
59
60
61
62
63
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()" class="bg-light-green">
<img ngSrc="assets/img/student.webp" width="200" height="200" />
<ng-template #rowRef let-student>
<app-list-item>
{{ student.firstName }}
<button (click)="deleteStudent(student.id)">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
</app-list-item>
</ng-template>
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addStudent()">
Add
</button>
</app-card>
`,
styles: [
`
.bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
],
imports: [CardComponent, NgOptimizedImage, ListItemComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);
students = this.store.students;
ngOnInit(): void {
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));
}
addStudent() {
this.store.addOne(randStudent());
}
deleteStudent(id: number) {
this.store.deleteOne(id);
}
}