Advertisement
Guest User

customer-form.ts

a guest
May 17th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, OnDestroy } from '@angular/core';
  2. import { Router, ActivatedRoute } from '@angular/router';
  3. import { FormGroup, FormBuilder, Validators, FormArray, FormControl } from "@angular/forms";
  4. import { CustomValidators } from 'ng2-validation';
  5. import { ContainsValidators } from "../shared/contains-validator.directive";
  6. import { ToastrService } from 'ngx-toastr';
  7.  
  8. import { CustomerDataService} from '../model/customer-data.service';
  9. import { Customer } from '../model/customer';
  10.  
  11.  
  12. @Component({
  13.   templateUrl: './customer-form.component.html',
  14. })
  15. export class CustomerformComponent implements OnInit, OnDestroy {
  16.  
  17.     private _mode = '';
  18.  
  19.     private _id:number;
  20.     private _parameters:any;
  21.     private _customer:Customer;
  22.  
  23.     private _form:FormGroup;
  24.     private _formErrors:any;
  25.  
  26.     private _errorMessage:string;
  27.     private _submitted:boolean = false;
  28.  
  29.     constructor(private _customerDataService:CustomerDataService,
  30.                 private _router:Router,
  31.                 private _formBuilder:FormBuilder,
  32.                 private _toastr: ToastrService,
  33.                 private _activatedRoute:ActivatedRoute) {
  34.         this._form = _formBuilder.group({
  35.           customer_name: ['', [Validators.required, Validators.minLength(5)]],
  36.           customer_address: ['', [Validators.required]],
  37.           customer_phone1: ['', [Validators.required]],
  38.           customer_phone2: ['', []],
  39.           customer_contact_name: ['', []],
  40.           customer_contact_phone: ['', []],
  41.           customer_photo_url: ['', []],
  42.           customer_identity_number: ['', []],
  43.           customer_identity_photo_url: ['', []],
  44.           customer_npwp_number: ['', []],
  45.           customer_npwp_photo: ['', []],
  46.  
  47.           username: ['', [Validators.required]],
  48.           password: ['', [Validators.required]],
  49.  
  50.           tmp_customer_photo: [null, []],
  51.           tmp_customer_identity_photo: [null, []],
  52.           tmp_customer_npwp_photo: [null, []],
  53.         });
  54.  
  55.         this._form.valueChanges
  56.             .subscribe(data => this.onValueChanged(data));
  57.     }
  58.  
  59.     private _setFormErrors(errorFields:any):void{
  60.         for (let key in errorFields) {
  61.             let errorField = errorFields[key];
  62.             // skip loop if the property is from prototype
  63.             if (!this._formErrors.hasOwnProperty(key)) continue;
  64.  
  65.             // let message = errorFields[error.field];
  66.             this._formErrors[key].valid = false;
  67.             this._formErrors[key].message = errorField;
  68.         }
  69.     }
  70.  
  71.     private _resetFormErrors():void{
  72.         this._formErrors = {
  73.             customer_name: {valid: true, message: ''},
  74.             customer_address: {valid: true, message: ''},
  75.             customer_phone1: {valid: true, message: ''},
  76.             customer_phone2: {valid: true, message: ''},
  77.             customer_contact_name: {valid: true, message: ''},
  78.             customer_contact_phone: {valid: true, message: ''},
  79.             customer_photo_url: {valid: true, message: ''},
  80.             customer_identity_number: {valid: true, message: ''},
  81.             customer_identity_photo_url: {valid: true, message: ''},
  82.             customer_npwp_number: {valid: true, message: ''},
  83.             customer_npwp_photo: {valid: true, message: ''},
  84.             username: {valid: true, message: ''},
  85.             password: {valid: true, message: ''},
  86.         };
  87.     }
  88.  
  89.     private _isValid(field):boolean {
  90.         let isValid:boolean = false;
  91.  
  92.         if(this._form.controls[field].touched == true) {
  93.             isValid = true;
  94.         }
  95.         if(this._form.controls[field].touched == true && this._form.controls[field].valid == true) {
  96.             isValid = true;
  97.         }
  98.  
  99.         return isValid;
  100.     }
  101.  
  102.     public onValueChanged(data?: any) {
  103.         if (!this._form) { return; }
  104.         const form = this._form;
  105.         for (let field in this._formErrors) {
  106.             // clear previous error message (if any)
  107.             let control = form.get(field);
  108.             if (control && control.dirty) {
  109.                 this._formErrors[field].valid = true;
  110.                 this._formErrors[field].message = '';
  111.             }
  112.         }
  113.     }
  114.  
  115.     private _resetCustomer(){
  116.         this._customer = new Customer();
  117.         this._customer.customer_name = '';
  118.         this._customer.customer_address = '';
  119.         this._customer.customer_phone1 = '';
  120.         this._customer.customer_phone2 = '';
  121.         this._customer.customer_contact_name = '';
  122.         this._customer.customer_contact_phone = '';
  123.     }
  124.  
  125.     public ngOnInit() {
  126.         this._resetFormErrors();
  127.         this._resetCustomer();
  128.  
  129.         this._parameters = this._activatedRoute.params.subscribe(params => {
  130.             if(typeof params['id'] !== "undefined") {
  131.                 this._id = Number.parseInt(params['id']);
  132.                 this._errorMessage = "";
  133.                 this._customerDataService.getCustomerById(this._id)
  134.                     .subscribe(
  135.                         response => {
  136.                             this._customer = response;
  137.                             this._mode = 'update';
  138.                         },
  139.                         error => {
  140.                             this._errorMessage = error.data;
  141.                         }
  142.                     );
  143.             } else {
  144.                 this._mode = 'create';
  145.             }
  146.         });
  147.     }
  148.  
  149.     public ngOnDestroy() {
  150.         this._parameters.unsubscribe();
  151.         this._customer = new Customer();
  152.     }
  153.  
  154.     public showToast(title:any, msg:any){
  155.         return this._toastr.success(msg, title, {
  156.                 timeOut: 5000,
  157.                 closeButton: true,
  158.                 progressBar: true
  159.             });
  160.     }
  161.  
  162.     public onFileSelected(event , type:string){
  163.         const reader = new FileReader();
  164.         if(event.target.files && event.target.files.length){
  165.             if(type == 'photo'){
  166.                 const [tmp_customer_photo] = event.target.files;
  167.                 reader.readAsDataURL(tmp_customer_photo);
  168.                 reader.onload = () => {
  169.                     this._form.patchValue({
  170.                       customer_photo_url: reader.result
  171.                     });
  172.                 }
  173.             }
  174.             if(type == 'identity'){
  175.                 const [tmp_customer_identity_photo] = event.target.files;
  176.                 reader.readAsDataURL(tmp_customer_identity_photo);
  177.                 reader.onload = () => {
  178.                     this._form.patchValue({
  179.                       customer_identity_photo_url: reader.result
  180.                     });
  181.                 }
  182.             }
  183.             if(type == 'npwp'){
  184.                 const [tmp_customer_npwp_photo] = event.target.files;
  185.                 reader.readAsDataURL(tmp_customer_npwp_photo);
  186.                 reader.onload = () => {
  187.                     this._form.patchValue({
  188.                       customer_npwp_photo: reader.result
  189.                     });
  190.                 }
  191.             }
  192.         }
  193.     }
  194.  
  195.     public onSubmit() {
  196.         this._submitted = true;
  197.         if(this._mode == 'create') {
  198.             this._customerDataService.addCustomer(this._customer)
  199.                 .subscribe(
  200.                     result => {
  201.                         if(result.status) {
  202.                             this._router.navigate(['/customer']);
  203.                             this.showToast("Berhasil", result.message);
  204.                         } else {
  205.                             this._submitted = false;
  206.                             this._errorMessage = result.message;
  207.                         }
  208.                     },
  209.                     error => {
  210.                         this._submitted = false;
  211.                         if(error.status == 422) {
  212.                             let errorFields = JSON.parse(error.data.message);
  213.                             this._setFormErrors(errorFields);
  214.                         }
  215.                         else{
  216.                             this._errorMessage = error.data;
  217.                         }
  218.                     }
  219.                 );
  220.         } else if(this._mode == 'update') {
  221.             this._customerDataService.updateCustomer(this._customer)
  222.                 .subscribe(
  223.                     result => {
  224.                         if(result.status) {
  225.                             this._router.navigate(['/customer']);
  226.                             this.showToast("Berhasil", result.message);
  227.                         } else {
  228.                             this._submitted = false;
  229.                         }
  230.                     },
  231.                     error => {
  232.                         this._submitted = false;
  233.                         if(error.status == 422) {
  234.                             let errorFields = JSON.parse(error.data.message);
  235.                             this._setFormErrors(errorFields);
  236.                         }
  237.                         else{
  238.                             this._errorMessage = error.data;
  239.                         }
  240.                     }
  241.                 );
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement