-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Answer:1 Angular projection challenge #1334
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
base: main
Are you sure you want to change the base?
Changes from all commits
1f9ae1d
e3bd1d9
77984c8
e510ae8
7301d95
570a2d5
92f11c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,49 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
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 { CardComponent } from '../../ui/card/card.component'; | ||
|
||
@Component({ | ||
selector: 'app-city-card', | ||
template: 'TODO City', | ||
imports: [], | ||
template: ` | ||
<app-card | ||
[list]="cities()" | ||
[cardImgSrc]="'assets/img/teacher.png'" | ||
(delete)="delete($event)" | ||
(addNewItem)="addNewItem()" | ||
[nameTemplate]="customName" | ||
customClass="bg-light-red"></app-card> | ||
|
||
<ng-template #customName let-item> | ||
<span class="name">{{ item.name }}</span> | ||
</ng-template> | ||
`, | ||
imports: [CardComponent], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class CityCardComponent {} | ||
export class CityCardComponent implements OnInit { | ||
private http = inject(FakeHttpService); | ||
private store = inject(CityStore); | ||
|
||
cities = this.store.cities; | ||
|
||
ngOnInit(): void { | ||
this.http.fetchCities$.subscribe((t) => this.store.addAll(t)); | ||
} | ||
|
||
addNewItem() { | ||
this.store.addOne(randomCity()); | ||
} | ||
|
||
delete(id: number) { | ||
this.store.deleteOne(id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,38 @@ | ||
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'; | ||
|
||
@Component({ | ||
selector: 'app-teacher-card', | ||
template: ` | ||
<app-card | ||
[list]="teachers()" | ||
[type]="cardType" | ||
[cardImgSrc]="'assets/img/teacher.png'" | ||
(delete)="delete($event)" | ||
(addNewItem)="addNewItem()" | ||
customClass="bg-light-red"></app-card> | ||
`, | ||
styles: [ | ||
` | ||
::ng-deep .bg-light-red { | ||
background-color: rgba(250, 0, 0, 0.1); | ||
} | ||
`, | ||
], | ||
imports: [CardComponent], | ||
}) | ||
export class TeacherCardComponent implements OnInit { | ||
private http = inject(FakeHttpService); | ||
private store = inject(TeacherStore); | ||
|
||
teachers = this.store.teachers; | ||
cardType = CardType.TEACHER; | ||
|
||
ngOnInit(): void { | ||
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t)); | ||
} | ||
|
||
addNewItem() { | ||
this.store.addOne(randTeacher()); | ||
} | ||
|
||
delete(id: number) { | ||
this.store.deleteOne(id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
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 { NgOptimizedImage, NgTemplateOutlet } from '@angular/common'; | ||
import { Component, input, output, TemplateRef } from '@angular/core'; | ||
import { ListItemComponent } from '../list-item/list-item.component'; | ||
|
||
@Component({ | ||
|
@@ -12,47 +8,38 @@ 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 [ngSrc]="cardImgSrc()" width="200" height="200" priority /> | ||
<section> | ||
@for (item of list(); track item) { | ||
<app-list-item | ||
[name]="item.firstName" | ||
[id]="item.id" | ||
[type]="type()"></app-list-item> | ||
<app-list-item [id]="item.id" (delete)="delete.emit($event)"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could move the app-list-item directly in the parent. but this works |
||
<ng-container | ||
*ngTemplateOutlet=" | ||
nameTemplate() || defaultName; | ||
context: { $implicit: item } | ||
"></ng-container> | ||
</app-list-item> | ||
} | ||
</section> | ||
|
||
<button | ||
class="rounded-sm border border-blue-500 bg-blue-300 p-2" | ||
(click)="addNewItem()"> | ||
(click)="addNewItem.emit()"> | ||
Add | ||
</button> | ||
|
||
<ng-template #defaultName let-item> | ||
<span class="name">{{ item.firstName }}</span> | ||
</ng-template> | ||
</div> | ||
`, | ||
imports: [ListItemComponent, NgOptimizedImage], | ||
imports: [ListItemComponent, NgOptimizedImage, NgTemplateOutlet], | ||
}) | ||
export class CardComponent { | ||
private teacherStore = inject(TeacherStore); | ||
private studentStore = inject(StudentStore); | ||
|
||
readonly list = input<any[] | null>(null); | ||
readonly type = input.required<CardType>(); | ||
readonly cardImgSrc = input.required<string>(); | ||
readonly nameTemplate = input<TemplateRef<any>>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you use an input, most of the time you should use a contentchild with template |
||
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()); | ||
} | ||
} | ||
delete = output<number>(); | ||
addNewItem = output<void>(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,16 @@ | ||
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())"> | ||
<ng-content></ng-content> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: use self-closing tag. it's nicer 😅 |
||
<button (click)="delete.emit(id())"> | ||
<img class="h-5" src="assets/svg/trash.svg" /> | ||
</button> | ||
</div> | ||
|
@@ -22,19 +19,7 @@ import { CardType } from '../../model/card.model'; | |
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>(); | ||
|
||
delete(id: number) { | ||
const type = this.type(); | ||
if (type === CardType.TEACHER) { | ||
this.teacherStore.deleteOne(id); | ||
} else if (type === CardType.STUDENT) { | ||
this.studentStore.deleteOne(id); | ||
} | ||
} | ||
delete = output<number>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need to output the id, you can just emit an event and the parent will passs the id to the function |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
.bg-light-green { | ||
background-color: rgba(0, 250, 0, 0.1); | ||
} | ||
|
||
.bg-light-red { | ||
background-color: rgba(250, 0, 0, 0.1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could try to refactor this using httpRessource or rxRessource to be more declarative