Skip to content
Closed
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
4 changes: 2 additions & 2 deletions apps/angular/1-projection/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"main": "apps/angular/1-projection/src/main.ts",
"polyfills": ["apps/angular/1-projection/src/polyfills.ts"],
"tsConfig": "apps/angular/1-projection/tsconfig.app.json",
"inlineStyleLanguage": "scss",
"inlineStyleLanguage": "css",
"assets": [
"apps/angular/1-projection/src/favicon.ico",
"apps/angular/1-projection/src/assets"
],
"styles": ["apps/angular/1-projection/src/styles.scss"],
"styles": ["apps/angular/1-projection/src/styles.css"],
"scripts": [],
"allowedCommonJsDependencies": ["seedrandom"]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,72 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NgOptimizedImage } from '@angular/common';
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,
CardListItemDirective,
} from '../../ui/card/card.component';
import { ListItemComponent } from '../../ui/list-item/list-item.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
template: `
<app-card
[list]="cities()"
(addItemEvent)="addNewItem()"
(deleteItemEvent)="deleteItem($event)"
customClass="bg-light-blue">
<img
card-image
ngSrc="assets/img/city.png"
width="200"
height="200"
alt="" />
<ng-template card-list-item let-city>
<app-list-item (deleteEvent)="deleteItem(city.id)">
{{ city.name }}
</app-list-item>
</ng-template>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-blue {
background-color: rgba(0, 0, 250, 0.1);
}
`,
],
imports: [
CardComponent,
NgOptimizedImage,
CardListItemDirective,
ListItemComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(CityStore);

cities = this.store.cities;

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

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

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import { NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
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';
import {
CardComponent,
CardListItemDirective,
} 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" />
customClass="bg-light-green"
(addItemEvent)="addNewItem()"
(deleteItemEvent)="deleteItem($event)">
<img
card-image
ngSrc="assets/img/student.webp"
width="200"
height="200"
alt="" />
<ng-template card-list-item let-student>
<app-list-item (deleteEvent)="deleteItem(student.id)">
{{ student.firstName + ' ' + student.lastName }}
</app-list-item>
</ng-template>
</app-card>
`,
styles: [
`
Expand All @@ -24,17 +44,29 @@ import { CardComponent } from '../../ui/card/card.component';
}
`,
],
imports: [CardComponent],
imports: [
CardComponent,
NgOptimizedImage,
CardListItemDirective,
ListItemComponent,
],
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));
}

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

deleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import { NgOptimizedImage } from '@angular/common';
import { Component, inject, 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 { CardComponent } from '../../ui/card/card.component';
import {
CardComponent,
CardListItemDirective,
} 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>
customClass="bg-light-red"
(addItemEvent)="addNewItem()"
(deleteItemEvent)="deleteItem($event)">
<img
card-image
priority
ngSrc="assets/img/teacher.png"
width="200"
height="200"
alt="" />
<ng-template card-list-item let-teacher>
<app-list-item (deleteEvent)="deleteItem(teacher.id)">
{{ teacher.firstName + ' ' + teacher.lastName }}
</app-list-item>
</ng-template>
</app-card>
`,
styles: [
`
Expand All @@ -19,16 +40,27 @@ import { CardComponent } from '../../ui/card/card.component';
}
`,
],
imports: [CardComponent],
imports: [
CardComponent,
NgOptimizedImage,
CardListItemDirective,
ListItemComponent,
],
})
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));
}

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
59 changes: 27 additions & 32 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,31 @@
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,
Directive,
input,
output,
TemplateRef,
} from '@angular/core';

@Directive({
selector: 'ng-template [card-list-item]',
})
export class CardListItemDirective {}

@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 select="[card-image]" />

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

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

readonly list = input<any[] | null>(null);
readonly type = input.required<CardType>();
export class CardComponent<T> {
addItemEvent = output();
deleteItemEvent = output<number>();
readonly list = input<T[]>([]);
readonly customClass = input('');

CardType = CardType;
readonly listTemplateRef = contentChild(CardListItemDirective, {
read: TemplateRef<{ $implicit: T }>,
});

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
this.addItemEvent.emit();
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
import {
ChangeDetectionStrategy,
Component,
inject,
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 { ChangeDetectionStrategy, Component, output } from '@angular/core';

@Component({
selector: 'app-list-item',
template: `
<div class="flex justify-between border border-gray-300 px-2 py-1">
{{ name() }}
<button (click)="delete(id())">
<ng-content />
<button (click)="delete()">
<img class="h-5" src="assets/svg/trash.svg" alt="trash" />
</button>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
deleteEvent = output<void>();

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

delete(id: number) {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
}
delete() {
this.deleteEvent.emit();
}
}
4 changes: 2 additions & 2 deletions apps/angular/10-utility-wrapper-pipe/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"main": "apps/angular/10-utility-wrapper-pipe/src/main.ts",
"polyfills": "apps/angular/10-utility-wrapper-pipe/src/polyfills.ts",
"tsConfig": "apps/angular/10-utility-wrapper-pipe/tsconfig.app.json",
"inlineStyleLanguage": "scss",
"inlineStyleLanguage": "css",
"assets": [
"apps/angular/10-utility-wrapper-pipe/src/favicon.ico",
"apps/angular/10-utility-wrapper-pipe/src/assets"
],
"styles": ["apps/angular/10-utility-wrapper-pipe/src/styles.scss"],
"styles": ["apps/angular/10-utility-wrapper-pipe/src/styles.css"],
"scripts": []
},
"configurations": {
Expand Down
Loading
Loading