Commit a1c8b5b1 authored by Vladimir Trubachoff's avatar Vladimir Trubachoff

Fix modal add product

parent 053afa15
......@@ -28,6 +28,7 @@
<td>
<input
type="number"
min="1"
class="w-50 form-control"
[value]="productsCount(product.id)"
#count
......
import { Component, EventEmitter, Input, Output } from '@angular/core';
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 { 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 { PagedResponseModel } from '../../../../core/model/paged-response.model';
import { UpdateCount } from '../../order-page.component';
@Component({
......@@ -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({
goods_id,
goodsId,
count,
});
}
}
productsCount(productId: number): number {
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">
<thead>
<tr>
......
......@@ -57,14 +57,10 @@ export class OrderPageComponent
ngOnInit(): void {
if (!this.isNew) {
this.getData(this.id);
}
}
deleteLine(id: number) {
this.orderLineService.delete(id).subscribe({
next: (res) => {
this.getData(this.order.id);
this.orderService.getOrder(this.id).subscribe({
next: (order) => {
this.orderSubj$.next(order);
this.order = order;
},
error: (err) => {
if (DEV) {
......@@ -73,11 +69,22 @@ export class OrderPageComponent
},
});
}
}
getData(id: number) {
this.orderService.getOrder(id).subscribe((order) => {
this.order = order;
deleteLine(id: number) {
this.orderLineService
.delete(id)
.pipe(concatMap(() => this.orderService.getOrder(this.id)))
.subscribe({
next: (order: Order) => {
this.orderSubj$.next(order);
this.order = order;
},
error: (err) => {
if (DEV) {
console.dir(`Error: ${err}`);
}
},
});
}
......@@ -85,10 +92,11 @@ export class OrderPageComponent
const orderModalRef = this.modalService.open(AddProductModalComponent, {
size: 'xl',
});
orderModalRef.componentInstance.order = this.order;
this.goodsService
.get()
.subscribe((resp) => (orderModalRef.componentInstance.goods = resp));
orderModalRef.componentInstance.order = this.order;
orderModalRef.componentInstance.pageNumber
.pipe(concatMap((page: number) => this.goodsService.get(page)))
.subscribe((resp: PagedResponseModel<Goods>) => {
......@@ -100,26 +108,29 @@ export class OrderPageComponent
takeUntil(this.destroy$),
concatMap((v: UpdateCount) => {
const orderLineId = this.order.orderLines.find(
(ol) => ol.goods.id === v.goods_id,
(ol) => ol.goods.id === v.goodsId,
)?.id;
if (orderLineId) {
return this.orderLineService.put({
id: orderLineId,
order_id: this.order.id,
goods_id: v.goods_id,
orderId: this.order.id,
goodsId: v.goodsId,
count: v.count,
});
}
return this.orderLineService.post({
order_id: this.order.id,
goods_id: v.goods_id,
orderId: this.order.id,
goodsId: v.goodsId,
count: v.count,
});
}),
concatMap(() => this.orderService.getOrder(this.id)),
)
.subscribe({
next: (res: any) => {
this.getData(this.order.id);
next: (order: Order) => {
this.orderSubj$.next(order);
this.order = order;
orderModalRef.componentInstance.order = this.order;
},
error: (err: any) => {
if (DEV) {
......@@ -135,6 +146,6 @@ export class OrderPageComponent
}
export interface UpdateCount {
goods_id: number;
goodsId: 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