Skip to content
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,63 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import {
CardAction,
CardActionType,
CardComponent,
} from '../../ui/card/card.component';
import { CardListItemTemplateDirective } from '../../ui/list-item/list-item-template.directive';
import { ListItemComponent } from '../../ui/list-item/list-item.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
template: `
<app-card
[list]="cities()"
customClass="bg-light-green"
(actions)="cardActions($event)">
<img ngSrc="assets/img/city.png" width="200" height="200" alt="" />

<ng-template cardListItem let-item let-onDeleteAction="onDeleteAction">
<app-list-item
[name]="item.name"
[id]="item.id"
(deleteEvent)="onDeleteAction(item.id)" />
</ng-template>
</app-card>
`,
imports: [
CardComponent,
CardListItemTemplateDirective,
ListItemComponent,
NgOptimizedImage,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent {
private http = inject(FakeHttpService);
private store = inject(CityStore);

cities = this.store.cities;

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

cardActions(action: CardAction) {
switch (action.type) {
case CardActionType.ADD:
this.store.addOne(randomCity());
break;
case CardActionType.DELETE:
this.store.deleteOne(action.payload.id);
break;
default:
console.log('Unknown');
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,77 @@
import { NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
OnInit,
ViewEncapsulation,
} 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';
import {
CardAction,
CardActionType,
CardComponent,
} from '../../ui/card/card.component';
import { CardListItemTemplateDirective } from '../../ui/list-item/list-item-template.directive';
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" />
customClass="bg-light-green"
(actions)="cardActions($event)">
<img ngSrc="assets/img/student.webp" width="200" height="200" alt="" />

<ng-template cardListItem let-item let-onDeleteAction="onDeleteAction">
<app-list-item
[name]="item.firstName"
[id]="item.id"
(deleteEvent)="onDeleteAction(item.id)" />
</ng-template>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-green {
.bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
],
imports: [CardComponent],
encapsulation: ViewEncapsulation.None,
imports: [
CardComponent,
CardListItemTemplateDirective,
ListItemComponent,
NgOptimizedImage,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);

students = this.store.students;
cardType = CardType.STUDENT;

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

cardActions(action: CardAction) {
switch (action.type) {
case CardActionType.ADD:
this.store.addOne(randStudent());
break;
case CardActionType.DELETE:
this.store.deleteOne(action.payload.id);
break;
default:
console.log('Unknown');
}
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,70 @@
import { Component, inject, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { NgOptimizedImage } from '@angular/common';
import { Component, inject, OnInit, ViewEncapsulation } 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';
import {
CardAction,
CardActionType,
CardComponent,
} from '../../ui/card/card.component';
import { CardListItemTemplateDirective } from '../../ui/list-item/list-item-template.directive';
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>
customClass="bg-light-red"
(actions)="cardActions($event)">
<img ngSrc="assets/img/teacher.png" width="200" height="200" alt="" />

<ng-template cardListItem let-item let-onDeleteAction="onDeleteAction">
<app-list-item
[name]="item.firstName"
[id]="item.id"
(deleteEvent)="onDeleteAction(item.id)" />
</ng-template>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-red {
.bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
imports: [CardComponent],
encapsulation: ViewEncapsulation.None,
imports: [
CardComponent,
CardListItemTemplateDirective,
ListItemComponent,
NgOptimizedImage,
],
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);

teachers = this.store.teachers;
cardType = CardType.TEACHER;

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

cardActions(action: CardAction) {
switch (action.type) {
case CardActionType.ADD:
this.store.addOne(randTeacher());
break;
case CardActionType.DELETE:
this.store.deleteOne(action.payload.id);
break;
default:
console.log('Unhandled action: ' + action);
}
}
}
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
61 changes: 32 additions & 29 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
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 { CardType } from '../../model/card.model';
import { ListItemComponent } from '../list-item/list-item.component';
import { NgTemplateOutlet } from '@angular/common';
import { Component, contentChild, input, output } from '@angular/core';
import { CardListItemTemplateDirective } from '../list-item/list-item-template.directive';

export enum CardActionType {
ADD = 'add',
DELETE = 'delete',
}

export type CardAction =
| { type: CardActionType.ADD }
| { type: CardActionType.DELETE; payload: { id: number } };

@Component({
selector: 'app-card',
template: `
<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" alt="" />
}
@if (type() === CardType.STUDENT) {
<img ngSrc="assets/img/student.webp" width="200" height="200" alt="" />
}
<ng-content />

<section>
@for (item of list(); track item) {
<app-list-item
[name]="item.firstName"
[id]="item.id"
[type]="type()"></app-list-item>
<ng-container
[ngTemplateOutlet]="listItemTemplate()?.template"
[ngTemplateOutletContext]="getItemContext(item)" />
}
</section>

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

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

CardType = CardType;
actions = output<CardAction>();

listItemTemplate = contentChild(CardListItemTemplateDirective);

getItemContext(item: any) {
return {
$implicit: item,
onDeleteAction: (id: number) => this.deleteItem(id),
};
}

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

deleteItem(id: number) {
this.actions.emit({ type: CardActionType.DELETE, payload: { id } });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Directive, inject, TemplateRef } from '@angular/core';

@Directive({
selector: 'ng-template[cardListItem]',
exportAs: 'cardListItem',
})
export class CardListItemTemplateDirective {
template: TemplateRef<{
$implicit: any;
onDeleteAction: (id: number) => void;
}> = inject(TemplateRef);
}
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 @@ -21,19 +18,12 @@ 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);
}
}