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
Original file line number Diff line number Diff line change
@@ -1,9 +1,73 @@
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 { CardActions } from '../../model';
import {
CardComponent,
CardSectionDirective,
ListItemComponent,
} from '../../ui';

@Component({
selector: 'app-city-card',
template: 'TODO City',
imports: [],
template: `
<app-card
[list]="cities()"
customClass="bg-light-blue"
[cardItemTemplate]="cardItemTemplate">
<img
cardSection="header"
ngSrc="assets/img/city.png"
width="200"
height="200" />

<ng-template #cardItemTemplate let-city>
<app-list-item
[name]="city.name"
[id]="city.id"
(deleteEvent)="onDeleteItem($event)"></app-list-item>
</ng-template>

<button
cardSection="footer"
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="onAddNewItem()">
Add
</button>
</app-card>
`,
imports: [
CardComponent,
NgOptimizedImage,
ListItemComponent,
CardSectionDirective,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent implements OnInit, CardActions {
private readonly http = inject(FakeHttpService);
private readonly store = inject(CityStore);

cities = this.store.cities;

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

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

onDeleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,72 @@
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 { CardActions } from '../../model';
import {
CardComponent,
CardSectionDirective,
ListItemComponent,
} from '../../ui';
@Component({
selector: 'app-student-card',
template: `
<app-card
[list]="students()"
[type]="cardType"
customClass="bg-light-green" />
customClass="bg-light-green"
[cardItemTemplate]="cardItemTemplate">
<img
cardSection="header"
ngSrc="assets/img/student.webp"
width="200"
height="200" />

<ng-template #cardItemTemplate let-student>
<app-list-item
[name]="student.firstName"
[id]="student.id"
(deleteEvent)="onDeleteItem($event)"></app-list-item>
</ng-template>

<button
cardSection="footer"
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="onAddNewItem()">
Add
</button>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
}
`,
imports: [
CardComponent,
NgOptimizedImage,
ListItemComponent,
CardSectionDirective,
],
imports: [CardComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(StudentStore);
export class StudentCardComponent implements OnInit, CardActions {
private readonly http = inject(FakeHttpService);
private readonly store = inject(StudentStore);

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

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

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

onDeleteItem(id: number) {
this.store.deleteOne(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,67 @@
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 { CardActions } from '../../model';
import {
CardComponent,
CardSectionDirective,
ListItemComponent,
} from '../../ui';
@Component({
selector: 'app-teacher-card',
template: `
<app-card
[list]="teachers()"
[type]="cardType"
customClass="bg-light-red"></app-card>
customClass="bg-light-red"
[cardItemTemplate]="cardItemTemplate">
<img
priority
cardSection="header"
ngSrc="assets/img/teacher.png"
width="200"
height="200" />

<ng-template #cardItemTemplate let-teacher>
<app-list-item
[name]="teacher.firstName"
[id]="teacher.id"
(deleteEvent)="onDeleteItem($event)"></app-list-item>
</ng-template>

<button
cardSection="footer"
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="onAddNewItem()">
Add
</button>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
imports: [
CardComponent,
NgOptimizedImage,
ListItemComponent,
CardSectionDirective,
],
imports: [CardComponent],
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
private store = inject(TeacherStore);
export class TeacherCardComponent implements OnInit, CardActions {
private readonly http = inject(FakeHttpService);
private readonly store = inject(TeacherStore);

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

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

onDeleteItem(id: number) {
this.store.deleteOne(id);
}

onAddNewItem() {
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
3 changes: 3 additions & 0 deletions apps/angular/1-projection/src/app/model/base.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface BaseItem {
id: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface CardActions {
onDeleteItem(id: number): void;
onAddNewItem(): void;
}
5 changes: 3 additions & 2 deletions apps/angular/1-projection/src/app/model/city.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface City {
id: number;
import { BaseItem } from './base.model';

export interface City extends BaseItem {
name: string;
country: string;
}
5 changes: 5 additions & 0 deletions apps/angular/1-projection/src/app/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './card-actions.interface';
export * from './card.model';
export * from './city.model';
export * from './student.model';
export * from './teacher.model';
4 changes: 2 additions & 2 deletions apps/angular/1-projection/src/app/model/student.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseItem } from './base.model';
import { Teacher } from './teacher.model';

export interface Student {
id: number;
export interface Student extends BaseItem {
firstName: string;
lastName: string;
mainTeacher: Teacher;
Expand Down
6 changes: 4 additions & 2 deletions apps/angular/1-projection/src/app/model/teacher.model.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { BaseItem } from './base.model';

export const subject = [
'Sciences',
'History',
'English',
'Maths',
'Sport',
] as const;

export type Subject = (typeof subject)[number];

export interface Teacher {
id: number;
export interface Teacher extends BaseItem {
firstName: string;
lastName: string;
subject: Subject;
Expand Down
65 changes: 20 additions & 45 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,33 @@
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 { CommonModule } from '@angular/common';
import { Component, input, TemplateRef } from '@angular/core';
import { BaseItem } from '../../model/base.model';
@Component({
selector: 'app-card',
template: `
<div
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
class="card 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" />
}
<div class="card-header">
<ng-content select="[cardSection='header']"></ng-content>
</div>

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

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
<div class="card-footer">
<ng-content select="[cardSection='footer']"></ng-content>
</div>
</div>
`,
imports: [ListItemComponent, NgOptimizedImage],
imports: [CommonModule],
})
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 extends BaseItem> {
readonly list = input<T[] | null>(null);
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 cardItemTemplate = input.required<TemplateRef<any>>();
}
Loading