Commit a1c8b5b1 authored by Vladimir Trubachoff's avatar Vladimir Trubachoff

Fix modal add product

parent 053afa15
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
<td> <td>
<input <input
type="number" type="number"
min="1"
class="w-50 form-control" class="w-50 form-control"
[value]="productsCount(product.id)" [value]="productsCount(product.id)"
#count #count
......
import { Component, EventEmitter, Input, Output } from '@angular/core'; import { Component, EventEmitter, Input, Output } from '@angular/core';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { Goods } from '../../../../core/model/goods.model';
import { AbstractBasicComponent } from '../../../../core/abstract/abstract-component';
import { takeUntil } from 'rxjs'; import { takeUntil } from 'rxjs';
import { PagedResponseModel } from '../../../../core/model/paged-response.model'; import { AbstractBasicComponent } from '../../../../core/abstract/abstract-component';
import { Goods } from '../../../../core/model/goods.model';
import { Order } from '../../../../core/model/order.model'; import { Order } from '../../../../core/model/order.model';
import { PagedResponseModel } from '../../../../core/model/paged-response.model';
import { UpdateCount } from '../../order-page.component'; import { UpdateCount } from '../../order-page.component';
@Component({ @Component({
...@@ -30,16 +30,18 @@ export class AddProductModalComponent extends AbstractBasicComponent { ...@@ -30,16 +30,18 @@ export class AddProductModalComponent extends AbstractBasicComponent {
}); });
} }
addOrUpdate(goods_id: number, count: number) { addOrUpdate(goodsId: number, count: number) {
if (count) {
this.orderLineChange.emit({ this.orderLineChange.emit({
goods_id, goodsId,
count, count,
}); });
} }
}
productsCount(productId: number): number { productsCount(productId: number): number {
return ( return (
this.order.orderLines.find((ol) => ol.goods.id === productId)?.count ?? 0 this.order?.orderLines.find((ol) => ol.goods.id === productId)?.count ?? 0
); );
} }
......
<h3>Products</h3> <h3>Order Products</h3>
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
......
...@@ -57,14 +57,10 @@ export class OrderPageComponent ...@@ -57,14 +57,10 @@ export class OrderPageComponent
ngOnInit(): void { ngOnInit(): void {
if (!this.isNew) { if (!this.isNew) {
this.getData(this.id); this.orderService.getOrder(this.id).subscribe({
} next: (order) => {
} this.orderSubj$.next(order);
this.order = order;
deleteLine(id: number) {
this.orderLineService.delete(id).subscribe({
next: (res) => {
this.getData(this.order.id);
}, },
error: (err) => { error: (err) => {
if (DEV) { if (DEV) {
...@@ -73,11 +69,22 @@ export class OrderPageComponent ...@@ -73,11 +69,22 @@ export class OrderPageComponent
}, },
}); });
} }
}
getData(id: number) { deleteLine(id: number) {
this.orderService.getOrder(id).subscribe((order) => { this.orderLineService
this.order = order; .delete(id)
.pipe(concatMap(() => this.orderService.getOrder(this.id)))
.subscribe({
next: (order: Order) => {
this.orderSubj$.next(order); this.orderSubj$.next(order);
this.order = order;
},
error: (err) => {
if (DEV) {
console.dir(`Error: ${err}`);
}
},
}); });
} }
...@@ -85,10 +92,11 @@ export class OrderPageComponent ...@@ -85,10 +92,11 @@ export class OrderPageComponent
const orderModalRef = this.modalService.open(AddProductModalComponent, { const orderModalRef = this.modalService.open(AddProductModalComponent, {
size: 'xl', size: 'xl',
}); });
orderModalRef.componentInstance.order = this.order;
this.goodsService this.goodsService
.get() .get()
.subscribe((resp) => (orderModalRef.componentInstance.goods = resp)); .subscribe((resp) => (orderModalRef.componentInstance.goods = resp));
orderModalRef.componentInstance.order = this.order;
orderModalRef.componentInstance.pageNumber orderModalRef.componentInstance.pageNumber
.pipe(concatMap((page: number) => this.goodsService.get(page))) .pipe(concatMap((page: number) => this.goodsService.get(page)))
.subscribe((resp: PagedResponseModel<Goods>) => { .subscribe((resp: PagedResponseModel<Goods>) => {
...@@ -100,26 +108,29 @@ export class OrderPageComponent ...@@ -100,26 +108,29 @@ export class OrderPageComponent
takeUntil(this.destroy$), takeUntil(this.destroy$),
concatMap((v: UpdateCount) => { concatMap((v: UpdateCount) => {
const orderLineId = this.order.orderLines.find( const orderLineId = this.order.orderLines.find(
(ol) => ol.goods.id === v.goods_id, (ol) => ol.goods.id === v.goodsId,
)?.id; )?.id;
if (orderLineId) { if (orderLineId) {
return this.orderLineService.put({ return this.orderLineService.put({
id: orderLineId, id: orderLineId,
order_id: this.order.id, orderId: this.order.id,
goods_id: v.goods_id, goodsId: v.goodsId,
count: v.count, count: v.count,
}); });
} }
return this.orderLineService.post({ return this.orderLineService.post({
order_id: this.order.id, orderId: this.order.id,
goods_id: v.goods_id, goodsId: v.goodsId,
count: v.count, count: v.count,
}); });
}), }),
concatMap(() => this.orderService.getOrder(this.id)),
) )
.subscribe({ .subscribe({
next: (res: any) => { next: (order: Order) => {
this.getData(this.order.id); this.orderSubj$.next(order);
this.order = order;
orderModalRef.componentInstance.order = this.order;
}, },
error: (err: any) => { error: (err: any) => {
if (DEV) { if (DEV) {
...@@ -135,6 +146,6 @@ export class OrderPageComponent ...@@ -135,6 +146,6 @@ export class OrderPageComponent
} }
export interface UpdateCount { export interface UpdateCount {
goods_id: number; goodsId: number;
count: number; count: number;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment