From af07bb5043d6d721e0e8f9df40d5b44704fe1922 Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Thu, 27 Aug 2026 12:08:58 +0000 Subject: [PATCH] fdsa --- bookie/src/app/customers/customer.service.ts | 33 ++++++++----- .../modifier-category.service.ts | 28 +++++++---- .../app/sale-report/sale-report.component.ts | 4 +- bookie/src/app/sales/bill.service.ts | 26 ++++++---- .../sales/modifiers/modifiers.component.ts | 9 ++-- .../receive-payment.component.ts | 47 +++++++++---------- .../settle-option/settle-option.service.ts | 11 ++--- 7 files changed, 90 insertions(+), 68 deletions(-) diff --git a/bookie/src/app/customers/customer.service.ts b/bookie/src/app/customers/customer.service.ts index 8fb21a1..459b152 100644 --- a/bookie/src/app/customers/customer.service.ts +++ b/bookie/src/app/customers/customer.service.ts @@ -1,5 +1,5 @@ import { HttpClient, HttpHeaders, httpResource, HttpResourceRef } from '@angular/common/http'; -import { Injectable, Signal, inject } from '@angular/core'; +import { Injectable, Signal, inject, signal } from '@angular/core'; import { Observable } from 'rxjs'; import { catchError } from 'rxjs/operators'; @@ -27,17 +27,26 @@ export class CustomerService { }); } - list(q: string | null, page = 0, size = 50, field = 'name', direction = 'asc'): Observable> { - const params = { - ...(q ? { q } : {}), - p: page, - s: size, - f: field, - d: direction, - }; - return this.http - .get>(`${url}/list`, { params }) - .pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable>; + list( + q: Signal, + page: Signal = signal(0), + size: Signal = signal(50), + field: Signal = signal('name'), + direction: Signal = signal('asc'), + ): HttpResourceRef | undefined> { + return httpResource>(() => { + const qVal = q(); + return { + url: `${url}/list`, + params: { + ...(qVal ? { q: qVal } : {}), + p: page(), + s: size(), + f: field(), + d: direction(), + }, + }; + }); } query( diff --git a/bookie/src/app/modifier-categories/modifier-category.service.ts b/bookie/src/app/modifier-categories/modifier-category.service.ts index 7dc3b7e..74f91ad 100644 --- a/bookie/src/app/modifier-categories/modifier-category.service.ts +++ b/bookie/src/app/modifier-categories/modifier-category.service.ts @@ -1,5 +1,5 @@ import { HttpClient, HttpHeaders, httpResource, HttpResourceRef } from '@angular/common/http'; -import { Injectable, Signal, inject } from '@angular/core'; +import { Injectable, Injector, Signal, inject } from '@angular/core'; import { Observable } from 'rxjs'; import { catchError } from 'rxjs/operators'; @@ -18,22 +18,30 @@ const serviceName = 'ModifierCategoryService'; export class ModifierCategoryService { private http = inject(HttpClient); private log = inject(ErrorLoggerService); + private injector = inject(Injector); get(id: Signal): HttpResourceRef { - return httpResource(() => { - const idVal = id(); - return idVal === null || idVal === undefined ? url : `${url}/${idVal}`; - }); + return httpResource( + () => { + const idVal = id(); + return idVal === null || idVal === undefined ? { url } : { url: `${url}/${idVal}` }; + }, + { injector: this.injector }, + ); } list(): HttpResourceRef { - return httpResource(() => `${url}/list`); + return httpResource(() => ({ url: `${url}/list` }), { injector: this.injector }); } - listForSku(sku_id: string): Observable { - return this.http - .get(`${url}/for-sku/${sku_id}`) - .pipe(catchError(this.log.handleError(serviceName, 'listForSku'))) as Observable; + listForSku(skuId: Signal): HttpResourceRef { + return httpResource( + () => { + const idVal = skuId(); + return idVal ? { url: `${url}/for-sku/${idVal}` } : undefined; + }, + { injector: this.injector }, + ); } save(modifierCategory: ModifierCategory): Observable { diff --git a/bookie/src/app/sale-report/sale-report.component.ts b/bookie/src/app/sale-report/sale-report.component.ts index 5e7e30c..2941def 100644 --- a/bookie/src/app/sale-report/sale-report.component.ts +++ b/bookie/src/app/sale-report/sale-report.component.ts @@ -1,6 +1,6 @@ import { CurrencyPipe } from '@angular/common'; -import { Component, inject, computed, linkedSignal, input } from '@angular/core'; -import { form, FormField } from '@angular/forms/signals'; +import { Component, computed, inject, input, linkedSignal } from '@angular/core'; +import { FormField, form } from '@angular/forms/signals'; import { MatButtonModule } from '@angular/material/button'; import { MatOptionModule } from '@angular/material/core'; import { MatDatepickerModule } from '@angular/material/datepicker'; diff --git a/bookie/src/app/sales/bill.service.ts b/bookie/src/app/sales/bill.service.ts index ce574bb..bb9e05f 100644 --- a/bookie/src/app/sales/bill.service.ts +++ b/bookie/src/app/sales/bill.service.ts @@ -1,9 +1,10 @@ import { SelectionModel } from '@angular/cdk/collections'; -import { Injectable, inject, signal, computed } from '@angular/core'; +import { Injectable, Injector, computed, inject, signal } from '@angular/core'; +import { toObservable } from '@angular/core/rxjs-interop'; import { MatDialog } from '@angular/material/dialog'; import { MatSnackBar } from '@angular/material/snack-bar'; -import { throwError, Observable } from 'rxjs'; -import { tap } from 'rxjs/operators'; +import { Observable, throwError } from 'rxjs'; +import { filter, take, tap } from 'rxjs/operators'; import { ModifierCategory } from '../core/modifier-category'; import { ProductQuery } from '../core/product-query'; @@ -29,6 +30,7 @@ export class BillService { private math = inject(MathService); private ser = inject(VoucherService); private modifierCategoryService = inject(ModifierCategoryService); + private injector = inject(Injector); public data = signal([]); public bill: Bill = new Bill(); @@ -152,11 +154,17 @@ export class BillService { } } newKot.inventories.push(item); - this.modifierCategoryService.listForSku(sku.id as string).subscribe((result) => { - if (result.reduce((a: number, c: ModifierCategory) => a + c.minimum, 0)) { - this.showModifier(item); - } - }); + const modRes = this.modifierCategoryService.listForSku(signal(sku.id as string)); + toObservable(modRes.value, { injector: this.injector }) + .pipe( + filter((result): result is ModifierCategory[] => !!result), + take(1), + ) + .subscribe((result) => { + if (result.reduce((a: number, c: ModifierCategory) => a + c.minimum, 0)) { + this.showModifier(item); + } + }); } this.displayBill(); } @@ -171,7 +179,7 @@ export class BillService { maxWidth: 'none', maxHeight: 'none', data: { - list: this.modifierCategoryService.listForSku(item.sku.id as string), + list: this.modifierCategoryService.listForSku(signal(item.sku.id as string)), selected: Object.assign([], item.modifiers), }, }); diff --git a/bookie/src/app/sales/modifiers/modifiers.component.ts b/bookie/src/app/sales/modifiers/modifiers.component.ts index a74816b..44c6676 100644 --- a/bookie/src/app/sales/modifiers/modifiers.component.ts +++ b/bookie/src/app/sales/modifiers/modifiers.component.ts @@ -1,6 +1,6 @@ import { CdkScrollable } from '@angular/cdk/scrolling'; -import { Component, inject, signal, computed } from '@angular/core'; -import { toSignal } from '@angular/core/rxjs-interop'; +import { HttpResourceRef } from '@angular/common/http'; +import { Component, computed, inject, signal } from '@angular/core'; import { MatBadgeModule } from '@angular/material/badge'; import { MatButtonModule } from '@angular/material/button'; import { MatCardModule } from '@angular/material/card'; @@ -8,7 +8,6 @@ import { MatChipsModule } from '@angular/material/chips'; import { MatRippleModule } from '@angular/material/core'; import { MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog'; import { MatTabsModule } from '@angular/material/tabs'; -import { Observable } from 'rxjs'; import { Modifier } from '../../core/modifier'; import { ModifierCategory } from '../../core/modifier-category'; @@ -30,11 +29,11 @@ import { ModifierCategory } from '../../core/modifier-category'; }) export class ModifiersComponent { data = inject<{ - list: Observable; + list: HttpResourceRef; selected: Modifier[]; }>(MAT_DIALOG_DATA); - list = toSignal(this.data.list, { initialValue: [] }); + list = computed(() => this.data.list.value() ?? []); selected = signal(this.data.selected ? [...this.data.selected] : []); selectedIds = computed(() => this.selected().map((e) => e.id)); diff --git a/bookie/src/app/sales/receive-payment/receive-payment.component.ts b/bookie/src/app/sales/receive-payment/receive-payment.component.ts index a0fc0de..ecd6c36 100644 --- a/bookie/src/app/sales/receive-payment/receive-payment.component.ts +++ b/bookie/src/app/sales/receive-payment/receive-payment.component.ts @@ -1,16 +1,14 @@ import { CdkScrollableModule } from '@angular/cdk/scrolling'; import { CurrencyPipe } from '@angular/common'; -import { Component, inject, signal, computed } from '@angular/core'; -import { form, FormField } from '@angular/forms/signals'; +import { Component, computed, effect, inject, signal } from '@angular/core'; +import { FormField, form } from '@angular/forms/signals'; import { MatButtonModule } from '@angular/material/button'; -import { MAT_DIALOG_DATA, MatDialogRef, MatDialogModule } from '@angular/material/dialog'; +import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { MatTableModule } from '@angular/material/table'; -import { map, tap } from 'rxjs/operators'; import { ReceivePaymentItem } from '../../core/receive-payment-item'; -import { SettleOption } from '../../core/settle-option'; import { SettleOptionService } from '../../settle-option/settle-option.service'; import { VoucherType } from '../bills/voucher-type'; @@ -45,6 +43,9 @@ export class ReceivePaymentComponent { amount: number; }>(MAT_DIALOG_DATA); + typeSignal = signal(this.data.type); + settleOptionsResource = this.ser.listForType(this.typeSignal); + choices = signal([]); choicesWithAmount = computed(() => { const amounts = this.formModel().amounts; @@ -77,25 +78,23 @@ export class ReceivePaymentComponent { this.amount = data.amount; this.displayReason.set(false); this.displayTable.set(false); - this.ser - .listForType(data.type) - .pipe( - tap((x: SettleOption[]) => this.displayReason.set(x.reduce((o, n) => o || n.hasReason, this.displayReason()))), - tap((x: SettleOption[]) => this.displayTable.set(x.length > 1)), - map((x: SettleOption[]) => - x.map((y) => ({ ...y, amount: !this.displayTable() ? this.amount : 0 }) as ReceivePaymentItem), - ), - ) - .subscribe((x) => { - this.choices.set(x); - this.formModel.update((f) => ({ - ...f, - amounts: x.map((y) => ({ - name: y.name, - amount: y.amount === 0 ? 0 : this.amount, - })), - })); - }); + + effect(() => { + const x = this.settleOptionsResource.value(); + if (!x) return; + this.displayReason.set(x.reduce((o, n) => o || n.hasReason, false)); + this.displayTable.set(x.length > 1); + const isTable = x.length > 1; + const choices = x.map((y) => ({ ...y, amount: !isTable ? this.amount : 0 }) as ReceivePaymentItem); + this.choices.set(choices); + this.formModel.update((f) => ({ + ...f, + amounts: x.map((y) => ({ + name: y.name, + amount: !isTable ? this.amount : 0, + })), + })); + }); } select(reason: string) { diff --git a/bookie/src/app/settle-option/settle-option.service.ts b/bookie/src/app/settle-option/settle-option.service.ts index fc76988..20e8136 100644 --- a/bookie/src/app/settle-option/settle-option.service.ts +++ b/bookie/src/app/settle-option/settle-option.service.ts @@ -31,12 +31,11 @@ export class SettleOptionService { return httpResource(() => `${url}/list`); } - listForType(voucherType: VoucherType): Observable { - return this.http - .get(`${url}/for-type/${voucherType}`) - .pipe(catchError(this.log.handleError(serviceName, `listForType voucherType=${voucherType}`))) as Observable< - SettleOption[] - >; + listForType(voucherType: Signal): HttpResourceRef { + return httpResource(() => { + const typeVal = voucherType(); + return typeVal !== undefined && typeVal !== null ? `${url}/for-type/${typeVal}` : undefined; + }); } save(settleOption: SettleOption): Observable {