mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 04:43:03 -05:00
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:
23
apps/angular/projection/src/app/data-access/city.store.ts
Normal file
23
apps/angular/projection/src/app/data-access/city.store.ts
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,14 +12,14 @@ import {
|
|||||||
import { map, timer } from 'rxjs';
|
import { map, timer } from 'rxjs';
|
||||||
import { City } from '../model/city.model';
|
import { City } from '../model/city.model';
|
||||||
import { Student } from '../model/student.model';
|
import { Student } from '../model/student.model';
|
||||||
import { subject, Teacher } from '../model/teacher.model';
|
import { Teacher, subject } from '../model/teacher.model';
|
||||||
|
|
||||||
const factoryTeacher = incrementalNumber();
|
const factoryTeacher = incrementalNumber();
|
||||||
|
|
||||||
export const randTeacher = () => ({
|
export const randTeacher = () => ({
|
||||||
id: factoryTeacher(),
|
id: factoryTeacher(),
|
||||||
firstname: randFirstName(),
|
firstName: randFirstName(),
|
||||||
lastname: randLastName(),
|
lastName: randLastName(),
|
||||||
subject: rand(subject),
|
subject: rand(subject),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -34,8 +34,8 @@ const factoryStudent = incrementalNumber();
|
|||||||
|
|
||||||
export const randStudent = (): Student => ({
|
export const randStudent = (): Student => ({
|
||||||
id: factoryStudent(),
|
id: factoryStudent(),
|
||||||
firstname: randFirstName(),
|
firstName: randFirstName(),
|
||||||
lastname: randLastName(),
|
lastName: randLastName(),
|
||||||
mainTeacher: teachers[randNumber({ max: teachers.length - 1 })],
|
mainTeacher: teachers[randNumber({ max: teachers.length - 1 })],
|
||||||
school: randWord(),
|
school: randWord(),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ import { Teacher } from './teacher.model';
|
|||||||
|
|
||||||
export interface Student {
|
export interface Student {
|
||||||
id: number;
|
id: number;
|
||||||
firstname: string;
|
firstName: string;
|
||||||
lastname: string;
|
lastName: string;
|
||||||
mainTeacher: Teacher;
|
mainTeacher: Teacher;
|
||||||
school: string;
|
school: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export type Subject = (typeof subject)[number];
|
|||||||
|
|
||||||
export interface Teacher {
|
export interface Teacher {
|
||||||
id: number;
|
id: number;
|
||||||
firstname: string;
|
firstName: string;
|
||||||
lastname: string;
|
lastName: string;
|
||||||
subject: Subject;
|
subject: Subject;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
|
||||||
@@ -8,7 +8,34 @@ import { ListItemComponent } from '../list-item/list-item.component';
|
|||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-card',
|
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,
|
standalone: true,
|
||||||
imports: [NgIf, NgFor, ListItemComponent],
|
imports: [NgIf, NgFor, ListItemComponent],
|
||||||
})
|
})
|
||||||
@@ -21,7 +48,7 @@ export class CardComponent {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private teacherStore: TeacherStore,
|
private teacherStore: TeacherStore,
|
||||||
private studentStore: StudentStore
|
private studentStore: StudentStore,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
addNewItem() {
|
addNewItem() {
|
||||||
|
|||||||
BIN
apps/angular/projection/src/assets/img/city.png
Normal file
BIN
apps/angular/projection/src/assets/img/city.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 20 KiB |
Reference in New Issue
Block a user