From 98c49ce26c60d82b1d7af08be4b6078f4bb8a4e9 Mon Sep 17 00:00:00 2001 From: A Rod Date: Wed, 14 Oct 2020 21:24:57 -0400 Subject: [PATCH] friend list to edit --- src/app/app-routing.module.ts | 2 +- .../friend-view/friend-view.component.html | 3 ++ .../friend-view/friend-view.component.ts | 11 ++++- src/app/friend/friend/friend.component.html | 37 ++++++++++++++++- src/app/friend/friend/friend.component.ts | 40 ++++++++++++++++--- 5 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index c4c8fa2..021655e 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -12,7 +12,7 @@ const routes: Routes = [ loadChildren: () => import('./friend/friend.module').then(m => m.FriendModule) }, - { path: '**', redirectTo: 'home'}, + // { path: '**', redirectTo: 'home'}, ]; @NgModule({ diff --git a/src/app/friend/friend-view/friend-view.component.html b/src/app/friend/friend-view/friend-view.component.html index 128eec4..d6ac736 100644 --- a/src/app/friend/friend-view/friend-view.component.html +++ b/src/app/friend/friend-view/friend-view.component.html @@ -5,6 +5,9 @@

List of Friends

{{i+1}} . {{friend.name}} + diff --git a/src/app/friend/friend-view/friend-view.component.ts b/src/app/friend/friend-view/friend-view.component.ts index 18fce4a..fe2a0bf 100644 --- a/src/app/friend/friend-view/friend-view.component.ts +++ b/src/app/friend/friend-view/friend-view.component.ts @@ -5,6 +5,8 @@ import { FriendState } from '../store/reducer/friend.reducer'; import {select, Store} from '@ngrx/store'; import { selectFriend } from '../store/selector/friend.selectors'; import { deleteFriends } from '../store/action/friend.actions'; +import { Router } from '@angular/router'; + @Component({ selector: 'app-friend-view', @@ -15,7 +17,10 @@ export class FriendViewComponent implements OnInit { friends$: Observable; - constructor(private store: Store) { + constructor( + private store: Store, + private router: Router + ) { this.friends$ = this.store.pipe(select(selectFriend)) } @@ -27,4 +32,8 @@ export class FriendViewComponent implements OnInit { this.store.dispatch(deleteFriends(friend)) } + edit(friend: Friend) { + this.router.navigate([friend.id,'detail']); + } + } diff --git a/src/app/friend/friend/friend.component.html b/src/app/friend/friend/friend.component.html index 66ca65f..b195116 100644 --- a/src/app/friend/friend/friend.component.html +++ b/src/app/friend/friend/friend.component.html @@ -1 +1,36 @@ -

friend works!

+
+
+
+ + Name + + + This field is mandatory. + This field is invalid. + + + + Friends + + + This field is mandatory. + + + + Age + + + This field is mandatory. + + + + Weight + + + This field is mandatory. + + + +
+
+
\ No newline at end of file diff --git a/src/app/friend/friend/friend.component.ts b/src/app/friend/friend/friend.component.ts index 6531fde..4bc9922 100644 --- a/src/app/friend/friend/friend.component.ts +++ b/src/app/friend/friend/friend.component.ts @@ -1,6 +1,13 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Friend, FriendResolved } from 'src/app/models/friend/friend.model'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import {Store} from '@ngrx/store'; +import { FriendState } from '../store/reducer/friend.reducer'; +import { addFriends } from '../store/action/friend.actions'; + + + @Component({ @@ -12,19 +19,40 @@ export class FriendComponent implements OnInit { friend: Friend; error: string; + addFrndForm: FormGroup; + nameRegex = /^[a-zA-Z]/; - constructor(private route: ActivatedRoute) { - console.log("Friend Comp crx") - } + constructor( + private route: ActivatedRoute, + private formBuilder: FormBuilder, + private store: Store + ) { } ngOnInit(): void { - console.log("Friend Comp init") - + this.addFrndForm = this.formBuilder.group({ + name: [null, [Validators.required, Validators.pattern(this.nameRegex)]], + friends: [null, [Validators.required]], + age: [null, [Validators.required]], + weight: [null, [Validators.required]] + }); const resolvedData: FriendResolved = this.route.snapshot.data['resolvedFriend']; this.friend = resolvedData.friend; this.error = resolvedData.error; + if(this.friend) { + this.addFrndForm.get('name').setValue(this.friend.name); + this.addFrndForm.get('friends').setValue(this.friend.name); + this.addFrndForm.get('age').setValue(this.friend.name); + this.addFrndForm.get('weight').setValue(this.friend.name); + } + } - console.log(this.friend, this.error); + submit() { + if(!this.addFrndForm.valid){ + return; + } + console.log(this.addFrndForm.value); + const newFriend = this.addFrndForm.value; + this.store.dispatch(addFriends(newFriend)) } }