Skip to content

Answer:1 - straightforward, no usage of ngTemplateOutlet #1175

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

Open
wants to merge 2 commits 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,12 +1,59 @@
import { Component, OnInit } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import { CardListItem } from '../../model/card.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
template: `
<app-card
[list]="cityListItems"
class="bg-light-green"
(addNewItemEvent)="handleAddNewItemEvent()"
(deleteItemEvent)="handleDeleteItemEvent($event)">
<div card-img>
<img src="assets/img/city.png" width="200px" />
</div>
</app-card>
`,
styles: [
`
.bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
],
imports: [CardComponent],
})
export class CityCardComponent implements OnInit {
constructor() {}
cityListItems: CardListItem[] = [];

ngOnInit(): void {}
constructor(
private http: FakeHttpService,
private store: CityStore,
) {}

ngOnInit(): void {
this.http.fetchCities$.subscribe((e) => this.store.addAll(e));
this.store.cities$.subscribe((cities) => {
this.cityListItems = cities.map((city) => {
return {
name: city.name,
id: city.id,
};
});
});
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use imperative coding, try to code in a declaration way, which means try to do the same but without the subscribe.

And use signals. 😉


handleAddNewItemEvent() {
this.store.addOne(randomCity());
}

handleDeleteItemEvent(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
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 { CardListItem } from '../../model/card.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-student-card',
template: `
<app-card
[list]="students"
[type]="cardType"
customClass="bg-light-green"></app-card>
[list]="studentListItems"
class="bg-light-green"
(addNewItemEvent)="handleAddNewItemEvent()"
(deleteItemEvent)="handleDeleteItemEvent($event)">
<div card-img>
<img src="assets/img/student.webp" width="200px" />
</div>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-green {
.bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
],
imports: [CardComponent],
})
export class StudentCardComponent implements OnInit {
students: Student[] = [];
cardType = CardType.STUDENT;
studentListItems: CardListItem[] = [];

constructor(
private http: FakeHttpService,
Expand All @@ -34,6 +40,21 @@ export class StudentCardComponent implements OnInit {
ngOnInit(): void {
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));

this.store.students$.subscribe((s) => (this.students = s));
this.store.students$.subscribe((students) => {
this.studentListItems = students.map((student) => {
return {
name: student.firstName,
id: student.id,
};
});
});
}

handleAddNewItemEvent() {
this.store.addOne(randStudent());
}

handleDeleteItemEvent(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
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 { CardListItem } from '../../model/card.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-teacher-card',
template: `
<app-card
[list]="teachers"
[type]="cardType"
customClass="bg-light-red"></app-card>
[list]="teacherListItems"
class="bg-light-red"
(addNewItemEvent)="handleAddNewItemEvent()"
(deleteItemEvent)="handleDeleteItemEvent($event)">
<div card-img>
<img src="assets/img/teacher.png" width="200px" />
</div>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-red {
.bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
imports: [CardComponent],
})
export class TeacherCardComponent implements OnInit {
teachers: Teacher[] = [];
cardType = CardType.TEACHER;
teacherListItems: CardListItem[] = [];

constructor(
private http: FakeHttpService,
Expand All @@ -34,6 +40,21 @@ export class TeacherCardComponent implements OnInit {
ngOnInit(): void {
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));

this.store.teachers$.subscribe((t) => (this.teachers = t));
this.store.teachers$.subscribe((teachers) => {
this.teacherListItems = teachers.map((teacher) => {
return {
name: teacher.firstName,
id: teacher.id,
};
});
});
}

handleAddNewItemEvent() {
this.store.addOne(randTeacher());
}

handleDeleteItemEvent(id: number) {
this.store.deleteOne(id);
}
}
5 changes: 5 additions & 0 deletions apps/angular/1-projection/src/app/model/card.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ export enum CardType {
STUDENT,
CITY,
}

export interface CardListItem {
name: string;
id: number;
}
75 changes: 27 additions & 48 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,39 @@
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 { NgFor } from '@angular/common';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { CardListItem } from '../../model/card.model';
import { ListItemComponent } from '../list-item/list-item.component';

@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" />

<section>
<app-list-item
*ngFor="let item of list"
[name]="item.firstName"
[id]="item.id"
[type]="type"></app-list-item>
</section>

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
</div>
<ng-content select="[card-img]"></ng-content>
<section>
<app-list-item
*ngFor="let item of list"
[name]="item.name"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no more ngfor, use the new control flow

[id]="item.id"
(deleteItemEvent)="handleDeleteItemEvent($event)"></app-list-item>
</section>
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
`,
imports: [NgIf, NgFor, ListItemComponent],
imports: [NgFor, ListItemComponent],
host: {
class: 'border-2 border-black rounded-md p-4 w-fit flex flex-col gap-3',
},
})
export class CardComponent {
@Input() list: any[] | null = null;
@Input() type!: CardType;
@Input() customClass = '';

CardType = CardType;

constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
) {}
@Input() list: CardListItem[] | null = null;
@Output() deleteItemEvent = new EventEmitter<number>();
@Output() addNewItemEvent = new EventEmitter<void>();

addNewItem() {
if (this.type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (this.type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
this.addNewItemEvent.emit();
}
handleDeleteItemEvent(id: number) {
this.deleteItemEvent.emit(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
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, EventEmitter, Input, Output } from '@angular/core';

@Component({
selector: 'app-list-item',
Expand All @@ -18,18 +15,9 @@ import { CardType } from '../../model/card.model';
export class ListItemComponent {
@Input() id!: number;
@Input() name!: string;
@Input() type!: CardType;

constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
) {}
@Output() deleteItemEvent = new EventEmitter<number>();

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the new input and output function.

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