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,51 @@
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
[list]="cities()"
[itemTemplateRef]="listItemTemplate"
(itemAdded)="addItem()"
customClass="bg-light-red">
<img src="assets/img/teacher.png" card-image width="200" height="200" />
</app-card>
<ng-template #listItemTemplate let-item>
{{ item.name }}
<button (click)="onItemDeleted(item)">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
</ng-template>
`,
imports: [CardComponent],
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(CityStore);
cities = this.store.cities;

ngOnInit() {
this.http.fetchCities$.subscribe((cityArray) =>
this.store.addAll(cityArray),
);
}
onItemDeleted(item: any) {
this.store.deleteOne(item.id);
}
addItem() {
this.store.addOne(randomCity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
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';
Expand All @@ -14,8 +17,18 @@ import { CardComponent } from '../../ui/card/card.component';
template: `
<app-card
[list]="students()"
[type]="cardType"
customClass="bg-light-green" />
[itemTemplateRef]="listItemTemplate"
(itemAdded)="addItem()"
customClass="bg-light-green">
<!-- <h1 ngProjectAs="card-image">Come on</h1>-->
<img src="assets/img/student.webp" card-image width="200" height="200" />
</app-card>
<ng-template #listItemTemplate let-item>
{{ item.firstName }}
<button (click)="onItemDeleted(item)">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
</ng-template>
`,
styles: [
`
Expand All @@ -37,4 +50,10 @@ export class StudentCardComponent implements OnInit {
ngOnInit(): void {
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));
}
onItemDeleted(item: any) {
this.store.deleteOne(item.id);
}
addItem() {
this.store.addOne(randStudent());
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
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';

@Component({
selector: 'app-teacher-card',
template: `
<app-card
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
[itemTemplateRef]="listItemTemplate"
(itemAdded)="addItem()"
customClass="bg-light-red">
<img src="assets/img/teacher.png" card-image width="200" height="200" />
</app-card>
<ng-template #listItemTemplate let-item>
{{ item.firstName }}
<button (click)="onItemDeleted(item)">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
</ng-template>
`,
styles: [
`
Expand All @@ -26,9 +37,14 @@ export class TeacherCardComponent implements OnInit {
private store = inject(TeacherStore);

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

ngOnInit(): void {
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));
}
onItemDeleted(item: any) {
this.store.deleteOne(item.id);
}
addItem() {
this.store.addOne(randTeacher());
}
}
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: 26 additions & 22 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, TemplateRef } from '@angular/core';
import { CardType } from '../../model/card.model';
import { ListItemComponent } from '../list-item/list-item.component';

Expand All @@ -12,19 +9,19 @@ 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" />
}
<ng-content select="[card-image]" />
<!-- @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" />-->
<!-- }-->

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

Expand All @@ -38,21 +35,28 @@ import { ListItemComponent } from '../list-item/list-item.component';
imports: [ListItemComponent, NgOptimizedImage],
})
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
// private teacherStore = inject(TeacherStore);
// private studentStore = inject(StudentStore);

readonly list = input<any[] | null>(null);
readonly type = input.required<CardType>();
// readonly type = input.required<CardType>();
readonly customClass = input('');
listItemDeleted = output<any>();
readonly itemTemplateRef = input.required<TemplateRef<any>>();
itemAdded = output();

CardType = CardType;

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
this.itemAdded.emit();
// const type = this.type();
// if (type === CardType.TEACHER) {
// this.teacherStore.addOne(randTeacher());
// } else if (type === CardType.STUDENT) {
// this.studentStore.addOne(randStudent());
// }
}
deleteListItem(item: any) {
this.listItemDeleted.emit(item);
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
import { CommonModule } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
inject,
input,
TemplateRef,
} 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',
template: `
<div class="border-grey-300 flex justify-between border px-2 py-1">
{{ name() }}
<button (click)="delete(id())">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
<ng-container
*ngTemplateOutlet="
itemTemplate();
context: { $implicit: item() }
"></ng-container>
<!-- {{ name() }}-->
<!-- <button (click)="delete(id())">-->
<!-- <img class="h-5" src="assets/svg/trash.svg" />-->
<!-- </button>-->
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [CommonModule],
})
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
// private teacherStore = inject(TeacherStore);
// private studentStore = inject(StudentStore);

readonly id = input.required<number>();
readonly name = input.required<string>();
readonly type = input.required<CardType>();
readonly item = input.required<any>();
readonly itemTemplate = input.required<TemplateRef<any>>();
// itemDeleted=output<any>();
// readonly type = input.required<CardType>();
// itemDeleted=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);
}
}
// 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.itemDeleted.emit(id)
// }
}
Loading