zgo/src/app/order/order.component.ts

125 lines
3.2 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from '@angular/core';
import { MatDialog, MatDialogConfig} from '@angular/material/dialog';
import { Observable } from 'rxjs';
import { Order } from './order.model';
import { FullnodeService } from '../fullnode.service';
import { UserService } from '../user.service';
import { OrderService } from './order.service';
import { CancelComponent } from '../cancel/cancel.component';
2021-10-28 18:22:54 +00:00
import { CheckoutComponent} from '../checkout/checkout.component';
2022-04-08 23:23:13 +00:00
import { ReceiptQRComponent} from '../receipt-qr/receipt-qr.component';
@Component({
selector: 'app-order',
templateUrl: './order.component.html',
styleUrls: ['./order.component.css']
})
export class OrderComponent implements OnInit{
2021-11-22 20:37:45 +00:00
public order: Order = {
address: '',
session: '',
timestamp: '',
closed: false,
currency: '',
price:0,
total:0,
totalZec: 0,
lines: [
{
qty: 1,
name: '',
cost: 0
}
]
};
public price: number = 1;
public total: number = 0;
public orderUpdate: Observable<Order>;
public priceUpdate: Observable<number>;
public totalUpdate: Observable<number>;
constructor(
public fullnodeService: FullnodeService,
public orderService: OrderService,
private dialog: MatDialog
) {
this.priceUpdate = fullnodeService.priceUpdate;
this.priceUpdate.subscribe((price) => {
this.price = price;
});
this.orderUpdate = orderService.orderUpdate;
2021-10-27 12:59:43 +00:00
this.orderUpdate.subscribe((order) => {
this.order = order;
});
this.totalUpdate = orderService.totalUpdate;
this.totalUpdate.subscribe((total) => {
this.total = total;
});
}
ngOnInit() {
}
cancelOrder() {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
2022-02-01 18:56:02 +00:00
dialogConfig.data = {title: 'Cancel Order?', msg: 'Are you sure you want to cancel the order?'};
const dialogRef = this.dialog.open(CancelComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => {
if (val) {
console.log('Canceling');
this.orderService.cancelOrder(this.order._id!).subscribe((response) => {
this.orderService.getOrder();
});;
} else {
console.log('Returning to page');
}
this.orderService.getOrder();
});
}
2021-10-28 18:22:54 +00:00
checkout() {
2021-11-05 14:30:35 +00:00
var zec = this.total/this.price;
this.order.totalZec = parseFloat(zec.toFixed(6));
2021-10-28 18:22:54 +00:00
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = {
2021-11-05 14:30:35 +00:00
totalZec: this.order.totalZec,
2021-10-28 18:22:54 +00:00
addr: this.order.address,
orderId: this.order._id
};
const dialogRef = this.dialog.open(CheckoutComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => {
if (val) {
2022-04-08 23:23:13 +00:00
const dialogConfig2 = new MatDialogConfig();
dialogConfig2.disableClose = true;
dialogConfig2.autoFocus = true;
dialogConfig2.data = {
order: this.order._id
};
2021-10-28 18:22:54 +00:00
console.log('Payment confirmed!');
2021-11-02 15:35:22 +00:00
this.orderService.closeOrder();
2022-04-08 23:23:13 +00:00
const dialogRef2 = this.dialog.open(ReceiptQRComponent, dialogConfig2);
dialogRef2.afterClosed().subscribe( val => {
if (val) {
console.log('Receipt closed.');
}
});
2021-10-28 18:22:54 +00:00
} else {
console.log('Returning to order');
}
});
}
2021-11-22 20:37:45 +00:00
getCurrency(){
return this.order.currency.toUpperCase();
}
}