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 @@
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 @@
+
+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 {}