refactor(challenge-1): augment challenge

Create city store
Camelcase some student and teacher interface's field to remove lint warnings
Move CardComponent's template to typescript file within decorator
This commit is contained in:
kabrunko-dev
2023-12-13 08:24:08 -03:00
parent 7e77b8a4a3
commit 89d9bf87fa
7 changed files with 61 additions and 38 deletions

View File

@@ -0,0 +1,23 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { City } from '../model/city.model';
@Injectable({
providedIn: 'root',
})
export class CityStore {
private cities = new BehaviorSubject<City[]>([]);
cities$ = this.cities.asObservable();
addAll(cities: City[]) {
this.cities.next(cities);
}
addOne(student: City) {
this.cities.next([...this.cities.value, student]);
}
deleteOne(id: number) {
this.cities.next(this.cities.value.filter((s) => s.id !== id));
}
}

View File

@@ -12,14 +12,14 @@ import {
import { map, timer } from 'rxjs';
import { City } from '../model/city.model';
import { Student } from '../model/student.model';
import { subject, Teacher } from '../model/teacher.model';
import { Teacher, subject } from '../model/teacher.model';
const factoryTeacher = incrementalNumber();
export const randTeacher = () => ({
id: factoryTeacher(),
firstname: randFirstName(),
lastname: randLastName(),
firstName: randFirstName(),
lastName: randLastName(),
subject: rand(subject),
});
@@ -34,8 +34,8 @@ const factoryStudent = incrementalNumber();
export const randStudent = (): Student => ({
id: factoryStudent(),
firstname: randFirstName(),
lastname: randLastName(),
firstName: randFirstName(),
lastName: randLastName(),
mainTeacher: teachers[randNumber({ max: teachers.length - 1 })],
school: randWord(),
});

View File

@@ -2,8 +2,8 @@ import { Teacher } from './teacher.model';
export interface Student {
id: number;
firstname: string;
lastname: string;
firstName: string;
lastName: string;
mainTeacher: Teacher;
school: string;
}

View File

@@ -9,7 +9,7 @@ export type Subject = (typeof subject)[number];
export interface Teacher {
id: number;
firstname: string;
lastname: string;
firstName: string;
lastName: string;
subject: Subject;
}

View File

@@ -1,27 +0,0 @@
<div
class="border-2 border-black rounded-md p-4 w-fit flex flex-col gap-3"
[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" />
<section>
<app-list-item
*ngFor="let item of list"
[name]="item.firstname"
[id]="item.id"
[type]="type">
</app-list-item>
</section>
<button
class="border border-blue-500 bg-blue-300 p-2 rounded-sm"
(click)="addNewItem()">
Add
</button>
</div>

View File

@@ -8,7 +8,34 @@ import { ListItemComponent } from '../list-item/list-item.component';
@Component({
selector: 'app-card',
templateUrl: './card.component.html',
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" />
<section>
<app-list-item
*ngFor="let item of list"
[name]="item.firstName"
[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>
</div>
`,
standalone: true,
imports: [NgIf, NgFor, ListItemComponent],
})
@@ -21,7 +48,7 @@ export class CardComponent {
constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore
private studentStore: StudentStore,
) {}
addNewItem() {

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB