diff --git a/apps/angular/projection/src/app/data-access/city.store.ts b/apps/angular/projection/src/app/data-access/city.store.ts new file mode 100644 index 0000000..711dad1 --- /dev/null +++ b/apps/angular/projection/src/app/data-access/city.store.ts @@ -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([]); + 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)); + } +} diff --git a/apps/angular/projection/src/app/data-access/fake-http.service.ts b/apps/angular/projection/src/app/data-access/fake-http.service.ts index e21ce03..82a8f18 100644 --- a/apps/angular/projection/src/app/data-access/fake-http.service.ts +++ b/apps/angular/projection/src/app/data-access/fake-http.service.ts @@ -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(), }); diff --git a/apps/angular/projection/src/app/model/student.model.ts b/apps/angular/projection/src/app/model/student.model.ts index 00592d4..bc18e46 100644 --- a/apps/angular/projection/src/app/model/student.model.ts +++ b/apps/angular/projection/src/app/model/student.model.ts @@ -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; } diff --git a/apps/angular/projection/src/app/model/teacher.model.ts b/apps/angular/projection/src/app/model/teacher.model.ts index e904388..34b4241 100644 --- a/apps/angular/projection/src/app/model/teacher.model.ts +++ b/apps/angular/projection/src/app/model/teacher.model.ts @@ -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; } diff --git a/apps/angular/projection/src/app/ui/card/card.component.html b/apps/angular/projection/src/app/ui/card/card.component.html deleted file mode 100644 index 81affa1..0000000 --- a/apps/angular/projection/src/app/ui/card/card.component.html +++ /dev/null @@ -1,27 +0,0 @@ -
- - - -
- - -
- - -
diff --git a/apps/angular/projection/src/app/ui/card/card.component.ts b/apps/angular/projection/src/app/ui/card/card.component.ts index 37dd708..f06c9ae 100644 --- a/apps/angular/projection/src/app/ui/card/card.component.ts +++ b/apps/angular/projection/src/app/ui/card/card.component.ts @@ -8,7 +8,34 @@ import { ListItemComponent } from '../list-item/list-item.component'; @Component({ selector: 'app-card', - templateUrl: './card.component.html', + template: ` +
+ + + +
+ +
+ + +
+ `, standalone: true, imports: [NgIf, NgFor, ListItemComponent], }) @@ -21,7 +48,7 @@ export class CardComponent { constructor( private teacherStore: TeacherStore, - private studentStore: StudentStore + private studentStore: StudentStore, ) {} addNewItem() { diff --git a/apps/angular/projection/src/assets/img/city.png b/apps/angular/projection/src/assets/img/city.png new file mode 100644 index 0000000..c600f44 Binary files /dev/null and b/apps/angular/projection/src/assets/img/city.png differ diff --git a/docs/src/content/docs/challenges/angular/1-projection.md b/docs/src/content/docs/challenges/angular/1-projection.md index 33eff99..1602510 100644 --- a/docs/src/content/docs/challenges/angular/1-projection.md +++ b/docs/src/content/docs/challenges/angular/1-projection.md @@ -35,3 +35,9 @@ While the application works, the developer experience is far from being optimal. - The `NgFor` directive must be declared and remain inside the `CardComponent`. You might be tempted to move it to the `ParentCardComponent` like `TeacherCardComponent`. - `CardComponent` should not contain any `NgIf` or `NgSwitch`. - CSS: try to avoid using `::ng-deep`. Find a better way to handle CSS styling. + +## Bonus Challenges + +- Try to work with the new built-in control flow syntax for loops and conditionals (documentation [here](https://angular.dev/guide/templates/control-flow)) +- Use the signal API to manage your components state (documentation [here](https://angular.dev/guide/signals)) +- To reference the template, use a directive instead of magic strings diff --git a/docs/src/content/docs/pt-br/challenges/angular/1-projection.md b/docs/src/content/docs/pt-br/challenges/angular/1-projection.md index d507cc9..352c175 100644 --- a/docs/src/content/docs/pt-br/challenges/angular/1-projection.md +++ b/docs/src/content/docs/pt-br/challenges/angular/1-projection.md @@ -35,3 +35,9 @@ Apesar da aplicação funcionar, a experiência do desenvolvedor (DX) está nem - A diretiva `NgFor` deve ser declarada e permanecer dentro do `CardComponent`. Você pode ficar instigado em querer mover ela para o `ParentCardComponent` como `TeacherCardComponent`. - `CardComponent` não deve conter nenhum `NgIf` ou `NgSwitch`. - CSS: tente evitar usar `::ng-deep`. Ache uma maneira melhor para lidar com o CSS. + +## Desafios Bônus + +- Tente trabalhar com a nova sintaxe nativa de controle de fluxo para laços e condicionais (documentação [aqui](https://angular.dev/guide/templates/control-flow)) +- Usa a signal API para gerenciar o estado de seus componentes (documentação [aqui](https://angular.dev/guide/signals)) +- Para referenciar o template, use uma diretiva ao invés de strings mágicas