web-dave / angular-starter-v2

6 stars 3 forks source link

dispatch action #34

Open web-dave opened 7 years ago

web-dave commented 7 years ago

dont forget to dispatch on save!!

web-dave commented 7 years ago

book-new.component.html

<form [formGroup]="form" (ngSubmit)="saveBook()" >
 <div class="form-group">
   <label for="title">Title</label>
   <input type="text" id="title" formControlName="title" (blur)="checkForm()">
   <div [hidden]="!form.get('title').hasError('required')">
     Enter a tilte
   </div>
 </div>
web-dave commented 7 years ago

book-new.component.ts

import { FORM_DIRTY, FORM_PRISTINE, IAppState } from '../../redux/store';
import { NgRedux } from '@angular-redux/store';
...
@Component({
 selector: 'book-new',
 templateUrl: './book-new.component.html',
 styleUrls: ['./book-new.component.scss']
})
export class BookNewComponent implements OnInit {
...
  constructor(...,
   private ngRedux: NgRedux<IAppState>
 ) { }
}
web-dave commented 7 years ago

book-new.component.ts


 checkForm() {
   if (this.form.dirty) {
     this.ngRedux.dispatch(FORM_DIRTY)
   } else {
     this.ngRedux.dispatch(FORM_PRISTINE)
   }
 }
web-dave commented 7 years ago

book-new.component.ts


 saveBook() {
   this.booksService.createBook(this.form.value)
     .subscribe(() => {
       this.ngRedux.dispatch(FORM_PRISTINE)
       this.router.navigate(['/books']);
     });
 }