2021-03-14 09:23:20 +01:00
|
|
|
import {Component, OnInit, EventEmitter, Output, Input, ChangeDetectionStrategy} from '@angular/core';
|
2021-02-08 12:47:07 +01:00
|
|
|
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
2021-02-17 13:49:04 +01:00
|
|
|
import {CustomErrorStateMatcher} from '@app/_helpers';
|
|
|
|
import {TransactionService} from '@app/_services';
|
2020-11-25 08:57:17 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-disbursement',
|
|
|
|
templateUrl: './disbursement.component.html',
|
2021-03-14 09:23:20 +01:00
|
|
|
styleUrls: ['./disbursement.component.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
2020-11-25 08:57:17 +01:00
|
|
|
})
|
|
|
|
export class DisbursementComponent implements OnInit {
|
2021-02-08 12:47:07 +01:00
|
|
|
@Input() account;
|
2020-12-05 07:30:30 +01:00
|
|
|
@Output() cancelDisbursmentEvent = new EventEmitter();
|
2021-02-08 12:47:07 +01:00
|
|
|
disbursementForm: FormGroup;
|
|
|
|
matcher = new CustomErrorStateMatcher();
|
|
|
|
submitted: boolean = false;
|
2020-11-25 08:57:17 +01:00
|
|
|
|
2021-02-08 12:47:07 +01:00
|
|
|
constructor(
|
|
|
|
private formBuilder: FormBuilder,
|
|
|
|
private transactionService: TransactionService
|
|
|
|
) { }
|
2020-11-25 08:57:17 +01:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
2021-02-08 12:47:07 +01:00
|
|
|
this.disbursementForm = this.formBuilder.group({
|
|
|
|
transactionType: ['', Validators.required],
|
|
|
|
recipient: '',
|
|
|
|
amount: ['', Validators.required]
|
|
|
|
});
|
2020-11-25 08:57:17 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 12:47:07 +01:00
|
|
|
get disbursementFormStub(): any { return this.disbursementForm.controls; }
|
|
|
|
|
2021-03-03 14:43:33 +01:00
|
|
|
async createTransfer(): Promise<void> {
|
2021-02-08 12:47:07 +01:00
|
|
|
this.submitted = true;
|
2021-03-24 10:42:19 +01:00
|
|
|
if (this.disbursementForm.invalid || !confirm('Make transfer?')) { return; }
|
2021-02-08 12:47:07 +01:00
|
|
|
if (this.disbursementFormStub.transactionType.value === 'transfer') {
|
2021-03-03 14:43:33 +01:00
|
|
|
await this.transactionService.transferRequest(
|
2021-02-08 12:47:07 +01:00
|
|
|
this.account.token,
|
|
|
|
this.account.address,
|
|
|
|
this.disbursementFormStub.recipient.value,
|
|
|
|
this.disbursementFormStub.amount.value
|
2021-03-03 14:43:33 +01:00
|
|
|
);
|
2021-02-08 12:47:07 +01:00
|
|
|
}
|
|
|
|
this.submitted = false;
|
|
|
|
}
|
2020-12-05 07:30:30 +01:00
|
|
|
|
|
|
|
cancel(): void {
|
|
|
|
this.cancelDisbursmentEvent.emit();
|
|
|
|
}
|
2020-11-25 08:57:17 +01:00
|
|
|
}
|