Skip to content

Answer:1 content projection #1309

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 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,9 +1,52 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
inject,
OnInit,
} from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
template: `
<app-card
customClass="bg-color"
[imgTitle]="cardImg"
[list]="cities()"
(addNew)="addNewItem()"
(itemId)="deleteItem($event)" />
`,
styles: [
`
:host {
--card-background: rgba(79, 64, 242, 0.1);
}
`,
],
imports: [CardComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent implements OnInit {
private readonly http = inject(FakeHttpService);
private readonly store = inject(CityStore);

cities = this.store.cities;
cardImg = 'assets/img/city.png';

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

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

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,57 @@
import {
ChangeDetectionStrategy,
Component,
computed,
inject,
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 { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-student-card',
template: `
<app-card
customClass="bg-color"
[imgTitle]="cardImg"
[list]="students()"
[type]="cardType"
customClass="bg-light-green" />
(addNew)="addNewItem()"
(itemId)="deleteItem($event)" />
`,
styles: [
`
::ng-deep .bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
:host {
--card-background: rgba(0, 250, 0, 0.1);
}
`,
],
imports: [CardComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);
private readonly http = inject(FakeHttpService);
private readonly store = inject(StudentStore);

students = this.store.students;
cardType = CardType.STUDENT;
students = computed(() => {
return this.store.students().map((item) => {
return { ...item, name: item.firstName };
});
});
cardImg = 'assets/img/student.webp';

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

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

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
import { Component, inject, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { Component, computed, inject, OnInit } from '@angular/core';
import {
FakeHttpService,
randTeacher,
} from '../../data-access/fake-http.service';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-teacher-card',
template: `
<app-card
customClass="bg-color"
[imgTitle]="cardImg"
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
(addNew)="addNewItem()"
(itemId)="deleteItem($event)" />
`,
styles: [
`
::ng-deep .bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
:host {
--card-background: rgba(250, 0, 0, 0.1);
}
`,
],
imports: [CardComponent],
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);
private readonly http = inject(FakeHttpService);
private readonly store = inject(TeacherStore);

teachers = this.store.teachers;
cardType = CardType.TEACHER;
teachers = computed(() => {
return this.store.teachers().map((item) => {
return { ...item, name: item.firstName };
});
});
cardImg = 'assets/img/teacher.png';

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

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

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { City } from '../model/city.model';
providedIn: 'root',
})
export class CityStore {
private cities = signal<City[]>([]);
public cities = signal<City[]>([]);

addAll(cities: City[]) {
this.cities.set(cities);
Expand Down
48 changes: 22 additions & 26 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { NgOptimizedImage } from '@angular/common';
import { Component, inject, 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 { Component, input, output } from '@angular/core';
import { CardType } from '../../model/card.model';
import { ListItemComponent } from '../list-item/list-item.component';

Expand All @@ -12,19 +9,14 @@ import { ListItemComponent } from '../list-item/list-item.component';
<div
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
[class]="customClass()">
@if (type() === CardType.TEACHER) {
<img ngSrc="assets/img/teacher.png" width="200" height="200" />
}
@if (type() === CardType.STUDENT) {
<img ngSrc="assets/img/student.webp" width="200" height="200" />
}
<img [ngSrc]="imgTitle()" width="200" height="200" priority />

<section>
@for (item of list(); track item) {
<app-list-item
[name]="item.firstName"
[id]="item.id"
[type]="type()"></app-list-item>
(deleteEvent)="onDelete($event)"
[name]="item.name"
[id]="item.id" />
}
</section>

Expand All @@ -35,24 +27,28 @@ import { ListItemComponent } from '../list-item/list-item.component';
</button>
</div>
`,
imports: [ListItemComponent, NgOptimizedImage],
styles: [
`
.bg-color {
background-color: var(--card-background);
}
`,
],
imports: [NgOptimizedImage, ListItemComponent],
})
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);

readonly list = input<any[] | null>(null);
readonly type = input.required<CardType>();
readonly type = input<CardType>();
readonly customClass = input('');

CardType = CardType;
readonly imgTitle = input.required<string>();
readonly addNew = output<void>();
readonly itemId = output<number>();

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
this.addNew.emit();
}

onDelete(id: number) {
this.itemId.emit(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import {
ChangeDetectionStrategy,
Component,
inject,
input,
output,
} from '@angular/core';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';

@Component({
selector: 'app-list-item',
Expand All @@ -22,19 +19,11 @@ import { CardType } from '../../model/card.model';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);

readonly id = input.required<number>();
readonly name = input.required<string>();
readonly type = input.required<CardType>();
deleteEvent = output<number>();

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