mempool/frontend/src/app/bisq/bisq-market/bisq-market.component.ts

103 lines
3.1 KiB
TypeScript
Raw Normal View History

import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { combineLatest, merge, Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
2021-03-14 23:24:06 +07:00
import { SeoService } from 'src/app/services/seo.service';
import { WebsocketService } from 'src/app/services/websocket.service';
import { BisqApiService } from '../bisq-api.service';
@Component({
selector: 'app-bisq-market',
templateUrl: './bisq-market.component.html',
styleUrls: ['./bisq-market.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class BisqMarketComponent implements OnInit, OnDestroy {
hlocData$: Observable<any>;
currency$: Observable<any>;
offers$: Observable<any>;
trades$: Observable<any>;
radioGroupForm: FormGroup;
defaultInterval = 'day';
2021-04-21 20:22:34 +04:00
isLoadingGraph = false;
constructor(
private websocketService: WebsocketService,
private route: ActivatedRoute,
private bisqApiService: BisqApiService,
private formBuilder: FormBuilder,
2021-03-14 23:24:06 +07:00
private seoService: SeoService,
) { }
ngOnInit(): void {
this.radioGroupForm = this.formBuilder.group({
interval: [this.defaultInterval],
});
this.currency$ = this.bisqApiService.getMarkets$()
.pipe(
switchMap((markets) => combineLatest([of(markets), this.route.paramMap])),
map(([markets, routeParams]) => {
const pair = routeParams.get('pair');
2021-03-14 23:24:06 +07:00
const pairUpperCase = pair.replace('_', '/').toUpperCase();
this.seoService.setTitle(`Bisq market: ${pairUpperCase}`);
return {
2021-03-14 23:24:06 +07:00
pair: pairUpperCase,
market: markets[pair],
};
})
);
this.trades$ = this.route.paramMap
.pipe(
map(routeParams => routeParams.get('pair')),
switchMap((marketPair) => this.bisqApiService.getMarketTrades$(marketPair)),
);
this.offers$ = this.route.paramMap
.pipe(
map(routeParams => routeParams.get('pair')),
switchMap((marketPair) => this.bisqApiService.getMarketOffers$(marketPair)),
2021-03-10 23:02:55 +07:00
map((offers) => offers[Object.keys(offers)[0]])
);
this.hlocData$ = combineLatest([
this.route.paramMap,
merge(this.radioGroupForm.get('interval').valueChanges, of(this.defaultInterval)),
])
.pipe(
switchMap(([routeParams, interval]) => {
2021-04-21 20:22:34 +04:00
this.isLoadingGraph = true;
const pair = routeParams.get('pair');
return this.bisqApiService.getMarketsHloc$(pair, interval);
}),
2021-03-10 23:02:55 +07:00
map((hlocData) => {
2021-04-21 20:22:34 +04:00
this.isLoadingGraph = false;
2021-03-10 23:02:55 +07:00
hlocData = hlocData.map((h) => {
h.time = h.period_start;
return h;
});
2021-03-10 23:02:55 +07:00
return {
hloc: hlocData,
volume: hlocData.map((h) => {
return {
time: h.time,
value: h.volume_right,
2021-04-19 17:39:47 +04:00
color: h.close > h.avg ? 'rgba(0, 41, 74, 0.7)' : 'rgba(0, 41, 74, 1)',
2021-03-10 23:02:55 +07:00
};
})
};
}),
);
}
ngOnDestroy(): void {
this.websocketService.stopTrackingBisqMarket();
}
}