Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer:1 #1131

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
import { Component, OnInit } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import { CardItemDirective } from '../../ui/card/card-item.directive';
import { CardComponent } from '../../ui/card/card.component';
import { ListItemComponent } from '../../ui/list-item/list-item.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
template: `
<app-card [list]="cities()" class="bg-light-green" (add)="add()">
<img src="assets/img/student.webp" width="200px" />
<app-list-item *app-card-item="let student" (delete)="delete(student.id)">
{{ student.name }},
<em>{{ student.country }}</em>
</app-list-item>
</app-card>
`,
standalone: true,
imports: [],
imports: [CardComponent, ListItemComponent, CardItemDirective],
})
export class CityCardComponent implements OnInit {
constructor() {}
cities = toSignal(this.store.cities$, { initialValue: [] });
constructor(
private http: FakeHttpService,
private store: CityStore,
) {}

ngOnInit(): void {}
ngOnInit(): void {
this.http.fetchCities$.subscribe((s) => this.store.addAll(s));
}

add() {
this.store.addOne(randomCity());
}
delete(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { toSignal } from '@angular/core/rxjs-interop';
import {
FakeHttpService,
randStudent,
} from '../../data-access/fake-http.service';
import { StudentStore } from '../../data-access/student.store';
import { CardType } from '../../model/card.model';
import { Student } from '../../model/student.model';
import { CardItemDirective } from '../../ui/card/card-item.directive';
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"
[type]="cardType"
customClass="bg-light-green"></app-card>
<app-card [list]="students()" class="bg-light-green" (add)="add()">
<img src="assets/img/student.webp" width="200px" />
<app-list-item *app-card-item="let student" (delete)="delete(student.id)">
{{ student.firstName }} {{ student.lastName }}
</app-list-item>
</app-card>
`,
standalone: true,
styles: [
`
::ng-deep .bg-light-green {
.bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
],
imports: [CardComponent],
imports: [CardComponent, ListItemComponent, CardItemDirective],
})
export class StudentCardComponent implements OnInit {
students: Student[] = [];
cardType = CardType.STUDENT;
students = toSignal(this.store.students$, {
initialValue: [],
});

constructor(
private http: FakeHttpService,
Expand All @@ -34,7 +41,11 @@ export class StudentCardComponent implements OnInit {

ngOnInit(): void {
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));

this.store.students$.subscribe((s) => (this.students = s));
}
add() {
this.store.addOne(randStudent());
}
delete(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { toSignal } from '@angular/core/rxjs-interop';
import {
FakeHttpService,
randTeacher,
} from '../../data-access/fake-http.service';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { Teacher } from '../../model/teacher.model';
import { CardItemDirective } from '../../ui/card/card-item.directive';
import { CardComponent } from '../../ui/card/card.component';
import { ListItemComponent } from '../../ui/list-item/list-item.component';

@Component({
selector: 'app-teacher-card',
template: `
<app-card
[list]="teachers"
[type]="cardType"
customClass="bg-light-red"></app-card>
<app-card [list]="teachers()" class="bg-light-red" (add)="add()">
<img src="assets/img/teacher.png" width="200px" />
<app-list-item *app-card-item="let teacher" (delete)="delete(teacher.id)">
{{ teacher.firstName }} {{ teacher.lastName }}
</app-list-item>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-red {
.bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
standalone: true,
imports: [CardComponent],
imports: [CardComponent, ListItemComponent, CardItemDirective],
})
export class TeacherCardComponent implements OnInit {
teachers: Teacher[] = [];
cardType = CardType.TEACHER;
teachers = toSignal(this.store.teachers$, { initialValue: [] });

constructor(
private http: FakeHttpService,
Expand All @@ -34,7 +39,12 @@ export class TeacherCardComponent implements OnInit {

ngOnInit(): void {
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));
}

this.store.teachers$.subscribe((t) => (this.teachers = t));
add() {
this.store.addOne(randTeacher());
}
delete(id: number) {
this.store.deleteOne(id);
}
}
23 changes: 23 additions & 0 deletions apps/angular/1-projection/src/app/ui/card/card-item.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
Directive,
inject,
TemplateRef,
ViewContainerRef,
} from '@angular/core';

export interface CardItem<T> {
$implicit: T;
}

@Directive({ selector: '[app-card-item]', standalone: true })
export class CardItemDirective<T> {
templateRef = inject(TemplateRef<T>);
containerRef = inject(ViewContainerRef);

static ngTemplateContextGuard<T>(
_dir: CardItemDirective<T>,
_ctx: unknown,
): _ctx is CardItem<T> {
return true;
}
}
81 changes: 32 additions & 49 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,44 @@
import { NgFor, NgIf } from '@angular/common';
import { Component, Input } from '@angular/core';
import { randStudent, randTeacher } from '../../data-access/fake-http.service';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { NgTemplateOutlet } from '@angular/common';
import {
Component,
contentChild,
input,
output,
TemplateRef,
} from '@angular/core';
import { ListItemComponent } from '../list-item/list-item.component';
import { CardItemDirective } from './card-item.directive';

@Component({
selector: 'app-card',
template: `
<div
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
[class]="customClass">
<img
*ngIf="type === CardType.TEACHER"
src="assets/img/teacher.png"
width="200px" />
<img
*ngIf="type === CardType.STUDENT"
src="assets/img/student.webp"
width="200px" />
<ng-content select="img"></ng-content>

<section>
<app-list-item
*ngFor="let item of list"
[name]="item.firstName"
[id]="item.id"
[type]="type"></app-list-item>
</section>
<section>
@for (item of list(); track item.id) {
<ng-container
[ngTemplateOutlet]="cardItemRef()"
[ngTemplateOutletContext]="{ $implicit: item }"></ng-container>
}
</section>

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
</div>
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="add.emit()">
Add
</button>
`,
standalone: true,
imports: [NgIf, NgFor, ListItemComponent],
imports: [NgTemplateOutlet, ListItemComponent],
host: {
class: 'flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4',
},
})
export class CardComponent {
@Input() list: any[] | null = null;
@Input() type!: CardType;
@Input() customClass = '';
export class CardComponent<T extends { id: number | string }> {
list = input<T[] | null>(null);
add = output();

CardType = CardType;

constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
) {}

addNewItem() {
if (this.type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (this.type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
}
cardItemRef = contentChild.required(CardItemDirective<T>, {
read: TemplateRef<ListItemComponent>,
});
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
import { Component, Input } from '@angular/core';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { Component, output } from '@angular/core';

@Component({
selector: 'app-list-item',
template: `
<div class="border-grey-300 flex justify-between border px-2 py-1">
{{ name }}
<button (click)="delete(id)">
<ng-content></ng-content>
<button (click)="delete.emit()">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
</div>
`,
standalone: true,
})
export class ListItemComponent {
@Input() id!: number;
@Input() name!: string;
@Input() type!: CardType;

constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
) {}

delete(id: number) {
if (this.type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (this.type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
}
}
delete = output();
}
Loading