-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Answer:1 [IN PROGRESS] #1312
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
Open
allan1989
wants to merge
5
commits into
tomalaforge:main
Choose a base branch
from
allan1989:projection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Answer:1 [IN PROGRESS] #1312
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cacb345
feat: add <ng-content> and input() and output()
allan1989 3792054
fix(student-card): error using $implicit
allan1989 1572100
chore: challenge complete
allan1989 8b837ce
chore: create directives person & templateOutlet
allan1989 a3d9682
Revert "chore: create directives person & templateOutlet"
allan1989 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 51 additions & 4 deletions
55
apps/angular/1-projection/src/app/component/city-card/city-card.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,56 @@ | ||
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 { CardComponent, CardListDirective } from '../../ui/card/card.component'; | ||
import { ListItemComponent } from '../../ui/list-item/list-item.component'; | ||
|
||
@Component({ | ||
selector: 'app-city-card', | ||
template: 'TODO City', | ||
imports: [], | ||
template: ` | ||
<app-card | ||
(addNewItem)="addNewItem()" | ||
[list]="cities()" | ||
class="bg-light-blue"> | ||
<img card-header ngSrc="assets/img/city.png" width="200" height="200" /> | ||
<ng-template card-list-item let-city> | ||
<app-list-item (deleteItem)="deleteItem(city.id)"> | ||
{{ city.country }} | ||
</app-list-item> | ||
</ng-template> | ||
</app-card> | ||
`, | ||
imports: [ | ||
NgOptimizedImage, | ||
CardComponent, | ||
ListItemComponent, | ||
CardListDirective, | ||
], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class CityCardComponent {} | ||
export class CityCardComponent implements OnInit { | ||
private readonly http = inject(FakeHttpService); | ||
private readonly store = inject(CityStore); | ||
|
||
cities = this.store.cities; | ||
|
||
ngOnInit(): void { | ||
this.http.fetchCities$.subscribe((t) => this.store.addAll(t)); | ||
} | ||
|
||
addNewItem() { | ||
this.store.addOne(randomCity()); | ||
} | ||
|
||
deleteItem(id: number) { | ||
this.store.deleteOne(id); | ||
} | ||
} |
49 changes: 38 additions & 11 deletions
49
apps/angular/1-projection/src/app/component/student-card/student-card.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,67 @@ | ||
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 { CardComponent, CardListDirective } from '../../ui/card/card.component'; | ||
import { ListItemComponent } from '../../ui/list-item/list-item.component'; | ||
|
||
@Component({ | ||
selector: 'app-student-card', | ||
template: ` | ||
<app-card | ||
(addNewItem)="addNewItem()" | ||
[list]="students()" | ||
[type]="cardType" | ||
customClass="bg-light-green" /> | ||
class="bg-light-green"> | ||
<img | ||
card-header | ||
ngSrc="assets/img/student.webp" | ||
width="200" | ||
height="200" /> | ||
<ng-template card-list-item let-student> | ||
<app-list-item (deleteItem)="deleteItem(student.id)"> | ||
{{ student.firstName }} | ||
</app-list-item> | ||
</ng-template> | ||
</app-card> | ||
`, | ||
styles: [ | ||
` | ||
::ng-deep .bg-light-green { | ||
background-color: rgba(0, 250, 0, 0.1); | ||
.bg-light-green { | ||
background-color: rgba(250, 0, 0, 0.1); | ||
} | ||
`, | ||
], | ||
imports: [CardComponent], | ||
imports: [ | ||
CardComponent, | ||
NgOptimizedImage, | ||
CardListDirective, | ||
ListItemComponent, | ||
], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class StudentCardComponent implements OnInit { | ||
private http = inject(FakeHttpService); | ||
private store = inject(StudentStore); | ||
private readonly http = inject(FakeHttpService); | ||
public readonly store = inject(StudentStore); | ||
|
||
students = this.store.students; | ||
cardType = CardType.STUDENT; | ||
|
||
ngOnInit(): void { | ||
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s)); | ||
} | ||
|
||
addNewItem() { | ||
this.store.addOne(randStudent()); | ||
} | ||
|
||
deleteItem(id: number) { | ||
this.store.deleteOne(id); | ||
} | ||
} |
57 changes: 45 additions & 12 deletions
57
apps/angular/1-projection/src/app/component/teacher-card/teacher-card.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,67 @@ | ||
import { Component, inject, OnInit } from '@angular/core'; | ||
import { FakeHttpService } from '../../data-access/fake-http.service'; | ||
import { NgOptimizedImage } from '@angular/common'; | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
inject, | ||
OnInit, | ||
} 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 { CardComponent } from '../../ui/card/card.component'; | ||
import { CardComponent, CardListDirective } from '../../ui/card/card.component'; | ||
import { ListItemComponent } from '../../ui/list-item/list-item.component'; | ||
|
||
@Component({ | ||
selector: 'app-teacher-card', | ||
template: ` | ||
<app-card | ||
(addNewItem)="addNewItem()" | ||
[list]="teachers()" | ||
[type]="cardType" | ||
customClass="bg-light-red"></app-card> | ||
class="bg-light-red"> | ||
<img | ||
card-header | ||
ngSrc="assets/img/teacher.png" | ||
width="200" | ||
height="200" /> | ||
<ng-template card-list-item let-teacher> | ||
<app-list-item (deleteItem)="deleteItem(teacher.id)"> | ||
{{ teacher.firstName }} | ||
</app-list-item> | ||
</ng-template> | ||
</app-card> | ||
`, | ||
styles: [ | ||
` | ||
::ng-deep .bg-light-red { | ||
background-color: rgba(250, 0, 0, 0.1); | ||
.bg-light-red { | ||
background-color: rgba(0, 250, 0, 0.1); | ||
} | ||
`, | ||
], | ||
imports: [CardComponent], | ||
imports: [ | ||
CardComponent, | ||
NgOptimizedImage, | ||
CardListDirective, | ||
ListItemComponent, | ||
], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class TeacherCardComponent implements OnInit { | ||
private http = inject(FakeHttpService); | ||
private store = inject(TeacherStore); | ||
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)); | ||
} | ||
|
||
addNewItem() { | ||
this.store.addOne(randTeacher()); | ||
} | ||
|
||
deleteItem(id: number) { | ||
this.store.deleteOne(id); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 37 additions & 49 deletions
86
apps/angular/1-projection/src/app/ui/card/card.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,46 @@ | ||
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, NgTemplateOutlet } from '@angular/common'; | ||
import { | ||
Component, | ||
contentChild, | ||
Directive, | ||
input, | ||
output, | ||
TemplateRef, | ||
} from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: 'ng-template [card-list-item]', | ||
standalone: true, | ||
}) | ||
export class CardListDirective {} | ||
|
||
@Component({ | ||
selector: 'app-card', | ||
host: { | ||
class: 'flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4', | ||
}, | ||
template: ` | ||
<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" /> | ||
<ng-content select="[card-header]"></ng-content> | ||
<section> | ||
@for (item of list(); track item) { | ||
<ng-template | ||
[ngTemplateOutlet]="cardRow()" | ||
[ngTemplateOutletContext]="{ $implicit: item }"></ng-template> | ||
} | ||
@if (type() === CardType.STUDENT) { | ||
<img ngSrc="assets/img/student.webp" width="200" height="200" /> | ||
} | ||
|
||
<section> | ||
@for (item of list(); track item) { | ||
<app-list-item | ||
[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> | ||
</section> | ||
<button | ||
class="rounded-sm border border-blue-500 bg-blue-300 p-2" | ||
(click)="addNewItem.emit()"> | ||
Add | ||
</button> | ||
`, | ||
imports: [ListItemComponent, NgOptimizedImage], | ||
imports: [CommonModule, NgTemplateOutlet], | ||
}) | ||
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> { | ||
readonly list = input<T[]>([]); | ||
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()); | ||
} | ||
} | ||
addNewItem = output(); | ||
cardRow = contentChild.required(CardListDirective, { | ||
read: TemplateRef<{ $implicit: T }>, | ||
}); | ||
} |
28 changes: 7 additions & 21 deletions
28
apps/angular/1-projection/src/app/ui/list-item/list-item.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,26 @@ | ||
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 /> | ||
<button (click)="deleteItem.emit()"> | ||
<img class="h-5" src="assets/svg/trash.svg" /> | ||
</button> | ||
</div> | ||
`, | ||
imports: [], | ||
standalone: true, | ||
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); | ||
} | ||
} | ||
readonly id = input<number>(); | ||
readonly name = input<string>(); | ||
deleteItem = output<void>(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
the goal is to handle all the logic in the parent. You should not input the store. Very bad practise to have a service as an input.
your html structure should look like:
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.
@tomalaforge Qu'est-ce que [cardRow]=students() ? Est-ce un input ? A part les variables locales, je ne vois pas d'autres attributs possibles sur un ng-template....
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.
En effet, ne fait pas attention a cela pour le moment,
Tu pourras l'implémenter si tu veux aller plus loin dans angular ensuite mais c'est plus avancé. c'est une directive pour typer student.