Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: answer1 for challenge projection #1139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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,13 +1,56 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
import { CardType } from '../../model/card.model';
import { City } from '../../model/city.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
template: `
<app-card
[list]="cities"
[type]="cardType"
[image]="'assets/img/city.png'"
[customClass]="
'flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4 bg-light-yellow'
">
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="add()">
Add
</button>
</app-card>
`,
styles: [
`
.bg-light-yellow {
background-color: rgba(250, 250, 0, 0.5);
}
`,
],
standalone: true,
imports: [],
imports: [CardComponent],
encapsulation: ViewEncapsulation.None,
})
export class CityCardComponent implements OnInit {
constructor() {}
cities: City[] = [];
cardType = CardType.CITY;

ngOnInit(): void {}
constructor(
private http: FakeHttpService,
private store: CityStore,
) {}

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

public add() {
this.store.addOne(randomCity());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import {
FakeHttpService,
randStudent,
} from '../../data-access/fake-http.service';
import { StudentStore } from '../../data-access/student.store';
import { CardType } from '../../model/card.model';
import { Student } from '../../model/student.model';
Expand All @@ -11,16 +14,26 @@ import { CardComponent } from '../../ui/card/card.component';
<app-card
[list]="students"
[type]="cardType"
customClass="bg-light-green"></app-card>
[image]="'assets/img/student.webp'"
[customClass]="
'flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4 bg-light-blue'
">
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="add()">
Add
</button>
</app-card>
`,
standalone: true,
styles: [
`
::ng-deep .bg-light-green {
background-color: rgba(0, 250, 0, 0.1);
.bg-light-blue {
background-color: rgba(0, 0, 250, 0.1);
}
`,
],
encapsulation: ViewEncapsulation.None,
imports: [CardComponent],
})
export class StudentCardComponent implements OnInit {
Expand All @@ -37,4 +50,7 @@ export class StudentCardComponent implements OnInit {

this.store.students$.subscribe((s) => (this.students = s));
}
public add() {
this.store.addOne(randStudent());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { Component, 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 { Teacher } from '../../model/teacher.model';
Expand All @@ -11,16 +14,26 @@ import { CardComponent } from '../../ui/card/card.component';
<app-card
[list]="teachers"
[type]="cardType"
customClass="bg-light-red"></app-card>
[image]="'assets/img/teacher.png'"
[customClass]="
'flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4 bg-light-red'
">
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="add()">
Add
</button>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-red {
.bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
standalone: true,
encapsulation: ViewEncapsulation.None,
imports: [CardComponent],
})
export class TeacherCardComponent implements OnInit {
Expand All @@ -37,4 +50,8 @@ export class TeacherCardComponent implements OnInit {

this.store.teachers$.subscribe((t) => (this.teachers = t));
}

public add() {
this.store.addOne(randTeacher());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { City } from '../model/city.model';
import { Student } from '../model/student.model';
import { Teacher, subject } from '../model/teacher.model';

// Teacher
const factoryTeacher = incrementalNumber();

export const randTeacher = () => ({
Expand All @@ -30,6 +31,7 @@ const teachers: Teacher[] = [
randTeacher(),
];

// Student
const factoryStudent = incrementalNumber();

export const randStudent = (): Student => ({
Expand All @@ -48,6 +50,7 @@ const students: Student[] = [
randStudent(),
];

// City
const factoryCity = incrementalNumber();

export const randomCity = (): City => ({
Expand All @@ -56,13 +59,13 @@ export const randomCity = (): City => ({
country: randCountry(),
});

const cities = [randomCity(), randomCity(), randomCity()];
const cities: City[] = [randomCity(), randomCity(), randomCity()];

@Injectable({
providedIn: 'root',
})
export class FakeHttpService {
fetchTeachers$ = timer(500).pipe(map(() => teachers));
fetchStudents$ = timer(500).pipe(map(() => students));
fetchCities$ = timer(500).pipe(map(() => cities));
fetchTeachers$ = timer(100).pipe(map(() => teachers));
fetchStudents$ = timer(100).pipe(map(() => students));
fetchCities$ = timer(100).pipe(map(() => cities));
}
55 changes: 12 additions & 43 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,30 @@
import { NgFor, NgIf } from '@angular/common';
import { NgFor } from '@angular/common';
import { Component, 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';

@Component({
selector: 'app-card',
template: `
<div
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
[class]="customClass">
<img
*ngIf="type === CardType.TEACHER"
src="assets/img/teacher.png"
width="200px" />
<img
*ngIf="type === CardType.STUDENT"
src="assets/img/student.webp"
width="200px" />

<div [class]="customClass">
<img [src]="image" />
<section>
<app-list-item
*ngFor="let item of list"
[name]="item.firstName"
[id]="item.id"
[type]="type"></app-list-item>
@for (item of list; track item.id) {
<app-list-item
[name]="item.firstName || item.name"
[id]="item.id"
[type]="type"></app-list-item>
}
</section>

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
<ng-content></ng-content>
</div>
`,
standalone: true,
imports: [NgIf, NgFor, ListItemComponent],
imports: [NgFor, ListItemComponent],
})
export class CardComponent {
@Input() list: any[] | null = null;
@Input() type!: CardType;
@Input() customClass = '';

CardType = CardType;

constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
) {}

addNewItem() {
if (this.type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (this.type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
}
@Input() image = '';
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, Input } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
Expand All @@ -9,7 +10,7 @@ import { CardType } from '../../model/card.model';
<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" />
<img class="h-5" src="assets/svg/trash.svg" width="10px" />
</button>
</div>
`,
Expand All @@ -23,13 +24,16 @@ export class ListItemComponent {
constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
private cityStore: CityStore,
) {}

delete(id: number) {
if (this.type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (this.type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
} else {
this.cityStore.deleteOne(id);
}
}
}