Skip to content

Commit 175b7cf

Browse files
committed
feat(projection): refactor card component using slots
1 parent 543770b commit 175b7cf

3 files changed

Lines changed: 59 additions & 38 deletions

File tree

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { NgOptimizedImage } from '@angular/common';
12
import {
23
ChangeDetectionStrategy,
34
Component,
45
inject,
56
OnInit,
67
} from '@angular/core';
7-
import { FakeHttpService } from '../../data-access/fake-http.service';
8+
import {
9+
FakeHttpService,
10+
randStudent,
11+
} from '../../data-access/fake-http.service';
812
import { StudentStore } from '../../data-access/student.store';
913
import { CardType } from '../../model/card.model';
1014
import { CardComponent } from '../../ui/card/card.component';
@@ -15,7 +19,24 @@ import { CardComponent } from '../../ui/card/card.component';
1519
<app-card
1620
[list]="students()"
1721
[type]="cardType"
18-
customClass="bg-light-green" />
22+
customClass="bg-light-green">
23+
<div card-header class="flex flex-col items-center">
24+
<img
25+
ngSrc="assets/img/student.webp"
26+
width="200"
27+
height="200"
28+
alt="Students list view banner"
29+
priority />
30+
</div>
31+
32+
<div card-action>
33+
<button
34+
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
35+
(click)="addNew()">
36+
Add
37+
</button>
38+
</div>
39+
</app-card>
1940
`,
2041
styles: [
2142
`
@@ -24,10 +45,12 @@ import { CardComponent } from '../../ui/card/card.component';
2445
}
2546
`,
2647
],
27-
imports: [CardComponent],
48+
imports: [CardComponent, NgOptimizedImage],
2849
changeDetection: ChangeDetectionStrategy.OnPush,
2950
})
3051
export class StudentCardComponent implements OnInit {
52+
private readonly studentStore = inject(StudentStore);
53+
3154
private http = inject(FakeHttpService);
3255
private store = inject(StudentStore);
3356

@@ -37,4 +60,8 @@ export class StudentCardComponent implements OnInit {
3760
ngOnInit(): void {
3861
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));
3962
}
63+
64+
addNew(): void {
65+
this.studentStore.addOne(randStudent());
66+
}
4067
}
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1+
import { NgOptimizedImage } from '@angular/common';
12
import { Component, inject, OnInit } from '@angular/core';
2-
import { FakeHttpService } from '../../data-access/fake-http.service';
3+
import {
4+
FakeHttpService,
5+
randTeacher,
6+
} from '../../data-access/fake-http.service';
37
import { TeacherStore } from '../../data-access/teacher.store';
48
import { CardType } from '../../model/card.model';
59
import { CardComponent } from '../../ui/card/card.component';
610

711
@Component({
812
selector: 'app-teacher-card',
913
template: `
10-
<app-card
11-
[list]="teachers()"
12-
[type]="cardType"
13-
customClass="bg-light-red"></app-card>
14+
<app-card [list]="teachers()" [type]="cardType" customClass="bg-light-red">
15+
<div card-header class="flex flex-col items-center">
16+
<img ngSrc="assets/img/teacher.png" width="200" height="200" alt="" />
17+
</div>
18+
19+
<div card-action>
20+
<button
21+
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
22+
(click)="addNew()">
23+
Add
24+
</button>
25+
</div>
26+
</app-card>
1427
`,
1528
styles: [
1629
`
@@ -19,9 +32,11 @@ import { CardComponent } from '../../ui/card/card.component';
1932
}
2033
`,
2134
],
22-
imports: [CardComponent],
35+
imports: [CardComponent, NgOptimizedImage],
2336
})
2437
export class TeacherCardComponent implements OnInit {
38+
private readonly teacherStore = inject(TeacherStore);
39+
2540
private http = inject(FakeHttpService);
2641
private store = inject(TeacherStore);
2742

@@ -31,4 +46,8 @@ export class TeacherCardComponent implements OnInit {
3146
ngOnInit(): void {
3247
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));
3348
}
49+
50+
addNew(): void {
51+
this.teacherStore.addOne(randTeacher());
52+
}
3453
}
Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import { NgOptimizedImage } from '@angular/common';
2-
import { Component, inject, input } from '@angular/core';
3-
import { randStudent, randTeacher } from '../../data-access/fake-http.service';
4-
import { StudentStore } from '../../data-access/student.store';
5-
import { TeacherStore } from '../../data-access/teacher.store';
1+
import { Component, input } from '@angular/core';
62
import { CardType } from '../../model/card.model';
73
import { ListItemComponent } from '../list-item/list-item.component';
84

@@ -12,12 +8,7 @@ import { ListItemComponent } from '../list-item/list-item.component';
128
<div
139
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
1410
[class]="customClass()">
15-
@if (type() === CardType.TEACHER) {
16-
<img ngSrc="assets/img/teacher.png" width="200" height="200" alt="" />
17-
}
18-
@if (type() === CardType.STUDENT) {
19-
<img ngSrc="assets/img/student.webp" width="200" height="200" alt="" />
20-
}
11+
<ng-content select="[card-header]"></ng-content>
2112
2213
<section>
2314
@for (item of list(); track item) {
@@ -28,31 +19,15 @@ import { ListItemComponent } from '../list-item/list-item.component';
2819
}
2920
</section>
3021
31-
<button
32-
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
33-
(click)="addNewItem()">
34-
Add
35-
</button>
22+
<ng-content select="[card-action]"></ng-content>
3623
</div>
3724
`,
38-
imports: [ListItemComponent, NgOptimizedImage],
25+
imports: [ListItemComponent],
3926
})
4027
export class CardComponent {
41-
private teacherStore = inject(TeacherStore);
42-
private studentStore = inject(StudentStore);
43-
4428
readonly list = input<any[] | null>(null);
4529
readonly type = input.required<CardType>();
4630
readonly customClass = input('');
4731

4832
CardType = CardType;
49-
50-
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-
}
57-
}
5833
}

0 commit comments

Comments
 (0)