You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.4 KiB
TypeScript
117 lines
4.4 KiB
TypeScript
3 years ago
|
import { Component, OnInit, OnDestroy } from '@angular/core';
|
||
2 years ago
|
import { Router, ResolveEnd, Event } from '@angular/router';
|
||
3 years ago
|
import { Subject } from 'rxjs';
|
||
|
import { takeUntil, filter } from 'rxjs/operators';
|
||
|
import { Store } from '@ngrx/store';
|
||
|
import { faInfinity } from '@fortawesome/free-solid-svg-icons';
|
||
|
|
||
2 years ago
|
import { LoopTypeEnum } from '../../../services/consts-enums-functions';
|
||
3 years ago
|
import { LoopModalComponent } from './loop-modal/loop-modal.component';
|
||
|
import { LoopQuote, LoopSwapStatus } from '../../../models/loopModels';
|
||
|
import { LoopService } from '../../../services/loop.service';
|
||
|
|
||
2 years ago
|
import { RTLState } from '../../../../store/rtl.state';
|
||
|
import { openAlert } from '../../../../store/rtl.actions';
|
||
3 years ago
|
|
||
|
@Component({
|
||
|
selector: 'rtl-loop',
|
||
|
templateUrl: './loop.component.html',
|
||
|
styleUrls: ['./loop.component.scss']
|
||
|
})
|
||
3 years ago
|
export class LoopComponent implements OnInit, OnDestroy {
|
||
3 years ago
|
|
||
3 years ago
|
public faInfinity = faInfinity;
|
||
|
private targetConf = 2;
|
||
|
public inAmount = 250000;
|
||
|
public quotes: LoopQuote[] = [];
|
||
|
public LoopTypeEnum = LoopTypeEnum;
|
||
|
public selectedSwapType: LoopTypeEnum = LoopTypeEnum.LOOP_OUT;
|
||
|
public storedSwaps: LoopSwapStatus[] = [];
|
||
|
public filteredSwaps: LoopSwapStatus[] = [];
|
||
|
public emptyTableMessage = 'No swap data available.';
|
||
3 years ago
|
public flgLoading: Array<Boolean | 'error'> = [true];
|
||
|
public links = [{ link: 'loopout', name: 'Loop Out' }, { link: 'loopin', name: 'Loop In' }];
|
||
3 years ago
|
public activeTab = this.links[0];
|
||
|
private unSubs: Array<Subject<void>> = [new Subject(), new Subject(), new Subject(), new Subject()];
|
||
|
|
||
2 years ago
|
constructor(private router: Router, private loopService: LoopService, private store: Store<RTLState>) { }
|
||
3 years ago
|
|
||
|
ngOnInit() {
|
||
|
this.loopService.listSwaps();
|
||
3 years ago
|
const linkFound = this.links.find((link) => this.router.url.includes(link.link));
|
||
3 years ago
|
this.activeTab = linkFound ? linkFound : this.links[0];
|
||
3 years ago
|
this.selectedSwapType = linkFound && linkFound.link === 'loopin' ? LoopTypeEnum.LOOP_IN : LoopTypeEnum.LOOP_OUT;
|
||
|
this.router.events.pipe(takeUntil(this.unSubs[0]), filter((e) => e instanceof ResolveEnd)).
|
||
2 years ago
|
subscribe({
|
||
|
next: (value: ResolveEnd | Event) => {
|
||
|
const linkFound = this.links.find((link) => (<ResolveEnd>value).urlAfterRedirects.includes(link.link));
|
||
|
this.activeTab = linkFound ? linkFound : this.links[0];
|
||
|
this.selectedSwapType = linkFound && linkFound.link === 'loopin' ? LoopTypeEnum.LOOP_IN : LoopTypeEnum.LOOP_OUT;
|
||
|
}
|
||
3 years ago
|
});
|
||
|
this.loopService.swapsChanged.
|
||
|
pipe(takeUntil(this.unSubs[1])).
|
||
|
subscribe({
|
||
|
next: (swaps: LoopSwapStatus[]) => {
|
||
|
this.flgLoading[0] = false;
|
||
|
this.storedSwaps = swaps;
|
||
2 years ago
|
this.filteredSwaps = this.storedSwaps?.filter((swap) => swap.type === this.selectedSwapType);
|
||
3 years ago
|
}, error: (err) => {
|
||
|
this.flgLoading[0] = 'error';
|
||
|
this.emptyTableMessage = err.message ? err.message : 'No loop ' + ((this.selectedSwapType === LoopTypeEnum.LOOP_IN) ? 'in' : 'out') + ' available.';
|
||
|
}
|
||
|
});
|
||
3 years ago
|
}
|
||
|
|
||
|
onSelectedIndexChange(activeTab: any) {
|
||
|
this.selectedSwapType = (activeTab.link === 'loopin') ? LoopTypeEnum.LOOP_IN : LoopTypeEnum.LOOP_OUT;
|
||
2 years ago
|
this.filteredSwaps = this.storedSwaps?.filter((swap) => swap.type === this.selectedSwapType);
|
||
3 years ago
|
}
|
||
|
|
||
|
onLoop(direction: LoopTypeEnum) {
|
||
3 years ago
|
if (direction === LoopTypeEnum.LOOP_IN) {
|
||
|
this.loopService.getLoopInTermsAndQuotes(this.targetConf).
|
||
|
pipe(takeUntil(this.unSubs[2])).
|
||
2 years ago
|
subscribe({
|
||
|
next: (response) => {
|
||
|
this.store.dispatch(openAlert({
|
||
|
payload: {
|
||
|
data: {
|
||
|
minQuote: response[0],
|
||
|
maxQuote: response[1],
|
||
|
direction: direction,
|
||
|
component: LoopModalComponent
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
}));
|
||
|
}
|
||
3 years ago
|
});
|
||
3 years ago
|
} else {
|
||
3 years ago
|
this.loopService.getLoopOutTermsAndQuotes(this.targetConf).
|
||
|
pipe(takeUntil(this.unSubs[3])).
|
||
2 years ago
|
subscribe({
|
||
|
next: (response) => {
|
||
|
this.store.dispatch(openAlert({
|
||
|
payload: {
|
||
|
data: {
|
||
|
minQuote: response[0],
|
||
|
maxQuote: response[1],
|
||
|
direction: direction,
|
||
|
component: LoopModalComponent
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
}));
|
||
|
}
|
||
3 years ago
|
});
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
|
ngOnDestroy() {
|
||
3 years ago
|
this.unSubs.forEach((completeSub) => {
|
||
2 years ago
|
completeSub.next(<any>null);
|
||
3 years ago
|
completeSub.complete();
|
||
|
});
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
}
|