currency-add.component.ts 3.33 KB
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { WebService } from './../../providers/web.service';
import { ValidationService } from './../../providers/validation.service';
import { FormControl, FormGroup, FormBuilder, Validators, ValidationErrors } from '@angular/forms';

@Component({
  selector: 'app-currency-add',
  templateUrl: './currency-add.component.html',
  styleUrls: ['./currency-add.component.scss']
})
export class CurrencyAddComponent implements OnInit {
  currencyForm: FormGroup;
  mobnumPattern = '^((\\+?)|0)?[0-9]{8,15}$';
  emailPattern = '^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$';
  currencySubmit: boolean;
  lang =  'en';
  error_msg: any[];
  funcName: any;
  loader: boolean;
  responseError: any;
  error: boolean;
  success: boolean;
  file: File;

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    public vs: ValidationService,
    public service: WebService,
    private formBuilder: FormBuilder
    ) {
      this.loader = false;
      this.error = false;
      this.success = false;
      this.currencySubmit = false;
      this.error_msg = this.vs.errorList[this.lang];
    }

  ngOnInit() {
    this.currencyForm = this.formBuilder.group({
      'name':  ['', Validators.compose([Validators.required])],
      'symbol':  ['', Validators.compose([Validators.required])],
      'flag':  ['', Validators.compose([Validators.required])],
      'rate':  ['', Validators.compose([Validators.required])],
      'start_from':  ['', Validators.compose([Validators.required])]
    });
  }

  onFileChange(event) {
    if (event.target.files.length > 0) {
        this.file = event.target.files[0];
    }
    console.log(this.file);
}

currencyProcess() {
    const This = this;
    this.error = false;
    this.success = false;
    console.log(this.currencyForm.value);
    this.currencySubmit = true;
    if (this.currencyForm.valid) {
      this.loader = true;
      const providerObj = this.currencyForm.value;
      const post_data = this.prepareSave(providerObj);
      this.funcName = 'currency_create';
      this.service.post_data(this.funcName, post_data).subscribe(response => {
        this.loader = false;
        console.log(response);
        if (response.code === 1) {
          this.success = true;
          this.currencySubmit = false;
          this.currencyForm.reset();
        } else {
          this.error = true;
          this.responseError = this.vs.errorCode[this.lang][response.errorCode];
        }
      }, (error) => {
        this.error = false;
        this.responseError = this.vs.errorCode[this.lang]['ER08'];
      });
    }
    console.log('tested');
  }

  prepareSave(formHasImage): any {
    const input = new FormData();
    input.append('flag', this.file);
    input.append('name', formHasImage.name);
    input.append('symbol', formHasImage.symbol);
    input.append('rate', formHasImage.rate);
    input.append('start_from', formHasImage.start_from);
    console.log(input);
    return input;
    }

    cancelClick() {
      this.router.navigate(['/checker/currency_list']);
    }

    goToPage(path: any, data = null) {
      console.log(data);
      this.router.navigateByUrl(path, {queryParams: data});
      document.body.scrollTop = document.documentElement.scrollTop = 0;
    }

    logout() {
      this.service.logout();
    }
}