mempool/frontend/src/app/components/about/about.component.ts

92 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Component, OnDestroy, OnInit } from '@angular/core';
import { WebsocketService } from '../../services/websocket.service';
2020-03-23 18:52:08 +01:00
import { SeoService } from 'src/app/services/seo.service';
import { StateService } from 'src/app/services/state.service';
import { Observable, Subscription } from 'rxjs';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ApiService } from 'src/app/services/api.service';
2020-10-09 08:56:43 +02:00
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
import { delay, map, retryWhen, switchMap, tap } from 'rxjs/operators';
2019-07-21 16:59:47 +02:00
@Component({
selector: 'app-about',
templateUrl: './about.component.html',
styleUrls: ['./about.component.scss'],
2019-07-21 16:59:47 +02:00
})
export class AboutComponent implements OnInit, OnDestroy {
gitCommit$: Observable<string>;
donationForm: FormGroup;
2020-10-26 12:29:25 +01:00
paymentForm: FormGroup;
donationStatus = 1;
sponsors$: Observable<any>;
donationObj: any;
sponsorsEnabled = this.stateService.env.OFFICIAL_MEMPOOL_SPACE;
sponsors = null;
requestSubscription: Subscription | undefined;
2019-07-21 16:59:47 +02:00
constructor(
private websocketService: WebsocketService,
2020-03-23 18:52:08 +01:00
private seoService: SeoService,
private stateService: StateService,
private formBuilder: FormBuilder,
private apiService: ApiService,
2020-10-09 08:56:43 +02:00
private sanitizer: DomSanitizer,
) { }
2019-07-21 16:59:47 +02:00
ngOnInit() {
this.gitCommit$ = this.stateService.gitCommit$.pipe(map((str) => str.substr(0, 8)));
2020-12-03 12:34:19 +01:00
this.seoService.setTitle($localize`:@@004b222ff9ef9dd4771b777950ca1d0e4cd4348a:About`);
2020-02-17 14:39:20 +01:00
this.websocketService.want(['blocks']);
this.donationForm = this.formBuilder.group({
amount: [0.01, [Validators.min(0.001), Validators.required]],
handle: [''],
});
2020-10-26 12:29:25 +01:00
this.paymentForm = this.formBuilder.group({
'method': 'chain'
});
this.apiService.getDonation$()
.subscribe((sponsors) => {
this.sponsors = sponsors;
});
}
ngOnDestroy() {
if (this.requestSubscription) {
this.requestSubscription.unsubscribe();
}
}
submitDonation() {
if (this.donationForm.invalid) {
return;
}
this.requestSubscription = this.apiService.requestDonation$(
this.donationForm.get('amount').value,
this.donationForm.get('handle').value
)
.pipe(
tap((response) => {
this.donationObj = response;
this.donationStatus = 3;
}),
switchMap(() => this.apiService.checkDonation$(this.donationObj.id)
.pipe(
retryWhen((errors) => errors.pipe(delay(2000)))
)
)
).subscribe(() => {
this.donationStatus = 4;
if (this.donationForm.get('handle').value) {
this.sponsors.unshift({ handle: this.donationForm.get('handle').value });
}
});
}
bypassSecurityTrustUrl(text: string): SafeUrl {
return this.sanitizer.bypassSecurityTrustUrl(text);
}
2019-07-21 16:59:47 +02:00
}