import { Component, OnInit, Input, EventEmitter } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { BsModalRef } from 'ngx-bootstrap'; import { AuthenticationService } from '../../../services/authentication.service'; import { ToastrService } from 'ngx-toastr'; @Component({ selector: 'app-wallet-credits', templateUrl: './wallet-credits.component.html', styleUrls: ['./wallet-credits.component.css'] }) export class WalletCreditsComponent implements OnInit { walletCreditsForm: FormGroup; public event: EventEmitter = new EventEmitter(); @Input() selectedUser; constructor(private fb: FormBuilder,private toastr: ToastrService, public bsModalRef: BsModalRef, private authService: AuthenticationService) { } ngOnInit(): void { this.walletCreditsForm = this.fb.group({ credits: ['', { validators: [Validators.required, Validators.pattern("^-?[0-9]*$")]}] }); } async onSubmit(){ if (this.walletCreditsForm.get('credits').value === '') this.walletCreditsForm.controls['credits'].markAllAsTouched(); if(this.walletCreditsForm.get('credits').value === '0' || this.walletCreditsForm.get('credits').value === '-0'){ this.toastr.error("Credits Amount must be greater than or less than Zero!"); }else{ if(this.walletCreditsForm.valid){ let creditsValue = Number(this.walletCreditsForm.value.credits), index = 0, invalidCredits; this.walletCreditsForm.value.credits = creditsValue; this.walletCreditsForm.value.sso = []; for(index; index < this.selectedUser.length; index++){ let updateUserCredits = creditsValue + this.selectedUser[index].wallet_value; if(updateUserCredits < 0){ invalidCredits = true; break; }else{ this.walletCreditsForm.value.sso.push(this.selectedUser[index].sso); } } if(invalidCredits){ this.toastr.error("Wallet Value can not be less than zero!"); }else{ this.authService.updateWalletValue(this.walletCreditsForm.value).subscribe((updateResult: { status: string, message: string, data }) => { if (updateResult.status === "success") { this.toastr.success("User Wallet updated successfully!"); this.event.emit(true); } }, (error) => { this.toastr.error("User Wallet update failed. Please try again!"); }); } this.bsModalRef.hide(); } } } get credits() { return this.walletCreditsForm.get('credits') } }