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,77 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
computed,
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
[image]="'assets/img/city.png'"
[list]="vm()"
customClass="city-card"
(add)="add()"
(delete)="delete($event)"></app-card>
`,
styles: [
`
.city-card {
background-color: rgba(0, 0, 250, 0.1);
}
`,
],
imports: [CardComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(CityStore);

vm = computed(() =>
this.store.cities().map((c /*: City */) => ({
id: c.id,
label: c.name,
})),
);

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

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

delete(id: number | Event) {
let idNumber: number | undefined;

if (typeof id === 'number') {
idNumber = id;
} else {
// If the emitter sends a CustomEvent with detail
const maybeCustom = id as CustomEvent;
if (maybeCustom?.detail !== undefined) {
idNumber = Number(maybeCustom.detail);
} else {
// Fallback: try to read a value from the event target (e.g. input/button)
const target = (id as Event).target as HTMLInputElement | null;
if (target && target.value !== undefined) {
idNumber = Number(target.value);
}
}
}

if (typeof idNumber === 'number' && !Number.isNaN(idNumber)) {
this.store.deleteOne(idNumber);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
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
[list]="students()"
[type]="cardType"
customClass="bg-light-green" />
[image]="'assets/img/student.webp'"
[list]="vm()"
customClass="student-card"
(add)="add()"
(delete)="delete($event)"></app-card>
`,
styles: [
`
::ng-deep .bg-light-green {
.student-card {
background-color: rgba(0, 250, 0, 0.1);
}
`,
Expand All @@ -31,10 +36,42 @@ export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);

students = this.store.students;
cardType = CardType.STUDENT;
vm = computed(() =>
this.store.students().map((s /*: Student */) => ({
id: s.id,
label: s.firstName,
})),
);

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

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

delete(id: number | Event) {
let idNumber: number | undefined;

if (typeof id === 'number') {
idNumber = id;
} else {
// If the emitter sends a CustomEvent with detail
const maybeCustom = id as CustomEvent;
if (maybeCustom?.detail !== undefined) {
idNumber = Number(maybeCustom.detail);
} else {
// Fallback: try to read a value from the event target (e.g. input/button)
const target = (id as Event).target as HTMLInputElement | null;
if (target && target.value !== undefined) {
idNumber = Number(target.value);
}
}
}

if (typeof idNumber === 'number' && !Number.isNaN(idNumber)) {
this.store.deleteOne(idNumber);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,77 @@
import { Component, inject, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import {
ChangeDetectionStrategy,
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
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
[image]="'assets/img/teacher.png'"
[list]="vm()"
customClass="teacher-card"
(add)="add()"
(delete)="delete($event)" />
`,
styles: [
`
::ng-deep .bg-light-red {
.teacher-card {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
imports: [CardComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);

teachers = this.store.teachers;
cardType = CardType.TEACHER;
vm = computed(() =>
this.store.teachers().map((t /*: Student */) => ({
id: t.id,
label: t.firstName,
})),
);

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

add() {
this.store.addOne(randTeacher()); // или store.addOne(randTeacher())
}

delete(id: number | Event) {
let idNumber: number | undefined;

if (typeof id === 'number') {
idNumber = id;
} else {
// If the emitter sends a CustomEvent with detail
const maybeCustom = id as CustomEvent;
if (maybeCustom?.detail !== undefined) {
idNumber = Number(maybeCustom.detail);
} else {
// Fallback: try to read a value from the event target (e.g. input/button)
const target = (id as Event).target as HTMLInputElement | null;
if (target && target.value !== undefined) {
idNumber = Number(target.value);
}
}
}

if (typeof idNumber === 'number' && !Number.isNaN(idNumber)) {
this.store.deleteOne(idNumber);
}
}
}
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
54 changes: 21 additions & 33 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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 { CommonModule, NgOptimizedImage } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
input,
output,
} from '@angular/core';
import { ListItemComponent } from '../list-item/list-item.component';

@Component({
Expand All @@ -12,47 +13,34 @@ 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 *ngIf="image()" [ngSrc]="image()!" width="200" height="200" />

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

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
(click)="add.emit()">
Add
</button>
</div>
`,
imports: [ListItemComponent, NgOptimizedImage],
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [ListItemComponent, NgOptimizedImage, CommonModule],
})
export class CardComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);
// ожидаем входы через helper `input` (Angular signals-style)
readonly list = input.required<any[]>(); // сигнал с массивом элементов
readonly image = input<string | null>(null);
readonly customClass = input<string>('');

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

CardType = CardType;

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
}
// выходы
readonly add = output<void>();
readonly delete = output<number>();
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
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',
template: `
<div class="border-grey-300 flex justify-between border px-2 py-1">
{{ name() }}
<button (click)="delete(id())">
{{ label() }}

<button (click)="delete.emit(id())">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
</div>
`,
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>();
readonly label = input.required<string>();

delete(id: number) {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
}
}
readonly delete = output<number>();
}
Loading