From a789a10203af942825ac97ffa3bfdaa13f9a0fac Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Fri, 24 Feb 2023 16:57:25 +0100 Subject: [PATCH] Fin section 13 : Formulaires --- src/app/app.module.ts | 3 +- .../detail-pokemon.component.html | 1 + .../detail-pokemon.component.ts | 4 + .../edit-pokemon/edit-pokemon.component.html | 5 + .../edit-pokemon/edit-pokemon.component.ts | 25 ++++ .../pokemon-form/pokemon-form.component.css | 7 ++ .../pokemon-form/pokemon-form.component.html | 107 ++++++++++++++++++ .../pokemon-form/pokemon-form.component.ts | 49 ++++++++ src/app/pokemon/pokemon.module.ts | 10 +- 9 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 src/app/pokemon/edit-pokemon/edit-pokemon.component.html create mode 100644 src/app/pokemon/edit-pokemon/edit-pokemon.component.ts create mode 100644 src/app/pokemon/pokemon-form/pokemon-form.component.css create mode 100644 src/app/pokemon/pokemon-form/pokemon-form.component.html create mode 100644 src/app/pokemon/pokemon-form/pokemon-form.component.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 02fc907..8e27d34 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,4 +1,5 @@ import { NgModule } from '@angular/core'; +import { FormsModule } from '@angular/forms'; import { BrowserModule } from '@angular/platform-browser'; import { AppRoutingModule } from './app-routing.module'; @@ -8,7 +9,7 @@ import { PokemonModule } from './pokemon/pokemon.module'; @NgModule({ declarations: [AppComponent, PageNotFoundComponent], - imports: [BrowserModule, PokemonModule, AppRoutingModule], + imports: [BrowserModule, FormsModule, PokemonModule, AppRoutingModule], providers: [], bootstrap: [AppComponent], }) diff --git a/src/app/pokemon/detail-pokemon/detail-pokemon.component.html b/src/app/pokemon/detail-pokemon/detail-pokemon.component.html index c9abbcf..7a536c5 100644 --- a/src/app/pokemon/detail-pokemon/detail-pokemon.component.html +++ b/src/app/pokemon/detail-pokemon/detail-pokemon.component.html @@ -48,6 +48,7 @@
Retour + Modifier
diff --git a/src/app/pokemon/detail-pokemon/detail-pokemon.component.ts b/src/app/pokemon/detail-pokemon/detail-pokemon.component.ts index a88c2ee..02e0347 100644 --- a/src/app/pokemon/detail-pokemon/detail-pokemon.component.ts +++ b/src/app/pokemon/detail-pokemon/detail-pokemon.component.ts @@ -26,4 +26,8 @@ export class DetailPokemonComponent implements OnInit { goToPokemonList() { this.router.navigate(['/pokemons']); } + + goToEditPokemon(pokemon: Pokemon) { + this.router.navigate(['/edit/pokemon', pokemon.id]); + } } diff --git a/src/app/pokemon/edit-pokemon/edit-pokemon.component.html b/src/app/pokemon/edit-pokemon/edit-pokemon.component.html new file mode 100644 index 0000000..a8630eb --- /dev/null +++ b/src/app/pokemon/edit-pokemon/edit-pokemon.component.html @@ -0,0 +1,5 @@ +

Éditer {{ pokemon?.name }}

+

+ +

+ diff --git a/src/app/pokemon/edit-pokemon/edit-pokemon.component.ts b/src/app/pokemon/edit-pokemon/edit-pokemon.component.ts new file mode 100644 index 0000000..81964f9 --- /dev/null +++ b/src/app/pokemon/edit-pokemon/edit-pokemon.component.ts @@ -0,0 +1,25 @@ +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { Pokemon } from '../pokemon'; +import { PokemonService } from '../pokemon.service'; + +@Component({ + selector: 'app-edit-pokemon', + templateUrl: './edit-pokemon.component.html', + styles: [], +}) +export class EditPokemonComponent implements OnInit { + pokemon: Pokemon | undefined; + + constructor( + private route: ActivatedRoute, + private pokemonService: PokemonService + ) {} + + ngOnInit() { + const pokemonId: string | null = this.route.snapshot.paramMap.get('id'); + if (pokemonId) { + this.pokemon = this.pokemonService.getPokemonById(+pokemonId); + } + } +} diff --git a/src/app/pokemon/pokemon-form/pokemon-form.component.css b/src/app/pokemon/pokemon-form/pokemon-form.component.css new file mode 100644 index 0000000..0b3fa51 --- /dev/null +++ b/src/app/pokemon/pokemon-form/pokemon-form.component.css @@ -0,0 +1,7 @@ +.ng-valid[required] { + border-left: 5px solid #42a948; +} + +.ng-invalid:not(form) { + border-left: 5px solid #a94442; +} diff --git a/src/app/pokemon/pokemon-form/pokemon-form.component.html b/src/app/pokemon/pokemon-form/pokemon-form.component.html new file mode 100644 index 0000000..e27799f --- /dev/null +++ b/src/app/pokemon/pokemon-form/pokemon-form.component.html @@ -0,0 +1,107 @@ +
+
+
+
+ +
+ + + +
+ Le nom du pokémon est requis (1-25). +
+
+ + +
+ + +
+ Les points de vie du pokémon sont compris entre 0 et 999. +
+
+ + +
+ + +
+ Les dégâts du pokémon sont compris entre 0 et 99. +
+
+ + + + +

+

+ {{ type }} +
+ + +

+ + + +
+
+ +
+
+
+
+ +

Aucun pokémon à éditer...

diff --git a/src/app/pokemon/pokemon-form/pokemon-form.component.ts b/src/app/pokemon/pokemon-form/pokemon-form.component.ts new file mode 100644 index 0000000..ad0c5ae --- /dev/null +++ b/src/app/pokemon/pokemon-form/pokemon-form.component.ts @@ -0,0 +1,49 @@ +import { Component, Input, OnInit } from '@angular/core'; +import { Router } from '@angular/router'; +import { Pokemon } from '../pokemon'; +import { PokemonService } from '../pokemon.service'; + +@Component({ + selector: 'app-pokemon-form', + templateUrl: './pokemon-form.component.html', + styleUrls: ['./pokemon-form.component.css'], +}) +export class PokemonFormComponent implements OnInit { + @Input() pokemon: Pokemon; + types: string[]; + + constructor(private pokemonService: PokemonService, private router: Router) {} + + ngOnInit(): void { + this.types = this.pokemonService.getPokemonTypeList(); + } + + hasType(type: string) { + return this.pokemon.types.includes(type); + } + + selectType($event: Event, type: string) { + const isChecked: boolean = ($event.target as HTMLInputElement).checked; + if (isChecked) { + this.pokemon.types.push(type); + } else { + const index = this.pokemon.types.indexOf(type); + this.pokemon.types.splice(index, 1); + } + } + + onSubmit() { + console.log('Submit form!'); + this.router.navigate(['/pokemon', this.pokemon.id]); + } + + isTypesValid(type: string): boolean { + if (this.pokemon.types.length === 1 && this.hasType(type)) { + return false; + } + if (this.pokemon.types.length > 2 && !this.hasType(type)) { + return false; + } + return true; + } +} diff --git a/src/app/pokemon/pokemon.module.ts b/src/app/pokemon/pokemon.module.ts index 6edbc48..9a0f19b 100644 --- a/src/app/pokemon/pokemon.module.ts +++ b/src/app/pokemon/pokemon.module.ts @@ -7,10 +7,14 @@ import { ListPokemonComponent } from './list-pokemon/list-pokemon.component'; import { DetailPokemonComponent } from './detail-pokemon/detail-pokemon.component'; import { RouterModule, Routes } from '@angular/router'; import { PokemonService } from './pokemon.service'; +import { FormsModule } from '@angular/forms'; +import { PokemonFormComponent } from './pokemon-form/pokemon-form.component'; +import { EditPokemonComponent } from './edit-pokemon/edit-pokemon.component'; const pokemonRoutes: Routes = [ - { path: 'pokemons', component: ListPokemonComponent }, + { path: 'edit/pokemon/:id', component: EditPokemonComponent }, { path: 'pokemon/:id', component: DetailPokemonComponent }, + { path: 'pokemons', component: ListPokemonComponent }, ]; @NgModule({ @@ -19,8 +23,10 @@ const pokemonRoutes: Routes = [ PokemonTypeColorPipe, ListPokemonComponent, DetailPokemonComponent, + PokemonFormComponent, + EditPokemonComponent, ], - imports: [CommonModule, RouterModule.forChild(pokemonRoutes)], + imports: [CommonModule, FormsModule, RouterModule.forChild(pokemonRoutes)], providers: [PokemonService], }) export class PokemonModule {}