zgo/src/app/items/item-list/item-list.component.ts

181 lines
4.6 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, Input } from '@angular/core';
import { MatDialog, MatDialogConfig} from '@angular/material/dialog';
2021-10-21 21:23:33 +00:00
import { Observable } from 'rxjs';
import { Item } from '../item.model';
import { Owner } from '../../owner.model';
import { FullnodeService } from '../../fullnode.service';
2021-10-21 21:23:33 +00:00
import { UserService } from '../../user.service';
import { ItemService } from '../items.service';
2021-10-27 12:59:43 +00:00
import { OrderService} from '../../order/order.service';
import { ItemCreateComponent } from '../item-create/item-create.component';
2021-10-26 17:58:39 +00:00
import { ItemDeleteComponent } from '../item-delete/item-delete.component';
2021-10-26 20:07:51 +00:00
import { ItemAddComponent } from '../item-add/item-add.component';
2021-10-21 21:23:33 +00:00
@Component({
selector: 'app-item-list',
templateUrl: './item-list.component.html',
styleUrls: ['./item-list.component.css']
2021-10-21 21:23:33 +00:00
})
export class ItemListComponent implements OnInit{
@Input()
zecPrice: number = 1;
2021-10-21 21:23:33 +00:00
public items: Item[] = [];
2022-03-07 20:53:10 +00:00
owner: Owner = {
2021-11-22 20:37:45 +00:00
_id: '',
name: '',
address: '',
currency: 'usd',
tax: false,
taxValue: 0,
vat: false,
2022-01-17 20:49:08 +00:00
vatValue: 0,
2022-01-28 20:03:35 +00:00
first: '',
last: '',
2022-01-17 20:49:08 +00:00
email: '',
street: '',
city: '',
state: '',
postal: '',
phone: '',
2022-01-18 22:40:20 +00:00
paid: false,
2022-01-19 16:26:25 +00:00
website: '',
2022-03-07 17:14:29 +00:00
country: '',
2022-05-18 20:51:39 +00:00
zats: false,
invoices: false,
2022-07-14 16:11:04 +00:00
expiration: new Date(Date.now()).toISOString(),
viewkey: ''
2021-11-22 20:37:45 +00:00
};
2021-10-21 21:23:33 +00:00
public ownerUpdate: Observable<Owner>;
public itemsUpdate: Observable<Item[]>;
constructor(
public itemService: ItemService,
userService: UserService,
2021-10-27 12:59:43 +00:00
public orderService: OrderService,
public fullnodeService: FullnodeService,
private dialog: MatDialog
2021-10-21 21:23:33 +00:00
) {
this.ownerUpdate = userService.ownerUpdate;
this.itemsUpdate = itemService.itemsUpdated;
this.ownerUpdate.subscribe((owner) => {
this.owner = owner;
itemService.getItems(this.owner.address);
this.itemsUpdate.subscribe((items) => {
this.items = items;
});
});
}
ngOnInit(){
2021-10-28 18:22:54 +00:00
this.itemsUpdate.subscribe((items) => {
this.items = items;
});
2021-10-21 21:23:33 +00:00
}
openDialog(){
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
2021-10-26 15:09:34 +00:00
dialogConfig.data = {_id: '', name: '' , user: '', description: '', cost: 0}
const dialogRef = this.dialog.open(ItemCreateComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => {
2021-10-26 15:09:34 +00:00
if(val != null) {
2022-05-18 20:51:39 +00:00
var item:Item = {_id: '', name: val.name, description: val.description, cost: val.cost, owner: this.owner.address};
2021-10-26 15:09:34 +00:00
this.itemService.addItem(item);
}
this.itemService.getItems(this.owner.address);
2021-10-28 18:22:54 +00:00
this.itemsUpdate.subscribe((items) => {
this.items = items;
});
2021-10-26 15:09:34 +00:00
});
}
edit(id: string) {
2022-05-18 20:51:39 +00:00
console.log('Edit:', id);
2021-10-26 15:09:34 +00:00
const item = this.items.find(element => element._id == id);
2022-05-18 20:51:39 +00:00
console.log(item);
2021-10-26 15:09:34 +00:00
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = item;
const dialogRef = this.dialog.open(ItemCreateComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => {
if (val != null) {
var editItem: Item = {
_id: val.id,
name: val.name,
description: val.description,
cost: val.cost,
2022-05-18 20:51:39 +00:00
owner: this.owner.address
2021-10-26 15:09:34 +00:00
};
//console.log('Edit:', editItem);
this.itemService.addItem(editItem).subscribe((response) => {
this.itemService.getItems(this.owner.address);
});;
}
this.itemService.getItems(this.owner.address);
});
}
2021-10-26 17:58:39 +00:00
delete(id: string) {
//console.log('Edit:', id);
const item = this.items.find(element => element._id == id);
//console.log(item);
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = item;
const dialogRef = this.dialog.open(ItemDeleteComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => {
if (val != null) {
console.log('Deleting', val);
2021-10-26 18:34:52 +00:00
this.itemService.deleteItem(val);
2021-10-28 18:22:54 +00:00
this.items = [];
2021-10-26 17:58:39 +00:00
}
this.itemService.getItems(this.owner.address);
2021-10-28 18:22:54 +00:00
this.itemsUpdate.subscribe((items) => {
this.items = items;
});
2021-10-26 17:58:39 +00:00
});
}
2021-10-26 20:07:51 +00:00
addToOrder(id: string) {
const item = this.items.find(element => element._id == id);
//console.log(item);
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = {
qty: 1,
name: item!.name,
cost: item!.cost
};
const dialogRef = this.dialog.open(ItemAddComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => {
if (val != null) {
console.log('Adding to order', val);
2021-10-27 12:59:43 +00:00
this.orderService.addToOrder(val);
2021-10-26 20:07:51 +00:00
}
this.itemService.getItems(this.owner.address);
});
}
2021-11-22 20:37:45 +00:00
getCurrency(){
return this.owner.currency.toUpperCase();
}
2021-10-21 21:23:33 +00:00
}