optimize pos gold form

This commit is contained in:
2026-05-11 21:09:43 +03:30
parent dba6162427
commit fecdf4a06b
2 changed files with 10 additions and 11 deletions
@@ -78,11 +78,11 @@ export class PosGoldPayloadFormComponent extends AbstractForm<
form.controls.payload.controls.profit_amount.valueChanges.pipe(takeUntilDestroyed()).subscribe(() => { form.controls.payload.controls.profit_amount.valueChanges.pipe(takeUntilDestroyed()).subscribe(() => {
// if ((form.controls.discount_amount.value || 0) > (value || 0)) { // if ((form.controls.discount_amount.value || 0) > (value || 0)) {
form.controls.discount_amount.setValue(0); form.controls.discount_amount.setValue(0, { emitEvent: false });
// } // }
}); });
form.controls.payload.controls.profit_percentage.valueChanges.pipe(takeUntilDestroyed()).subscribe(() => { form.controls.payload.controls.profit_percentage.valueChanges.pipe(takeUntilDestroyed()).subscribe(() => {
form.controls.discount_amount.setValue(0); form.controls.discount_amount.setValue(0, { emitEvent: false });
}); });
return form; return form;
@@ -6,6 +6,7 @@ import { CommonModule } from '@angular/common';
import { import {
Component, Component,
ContentChild, ContentChild,
DestroyRef,
EventEmitter, EventEmitter,
inject, inject,
Input, Input,
@@ -13,6 +14,7 @@ import {
signal, signal,
TemplateRef, TemplateRef,
} from '@angular/core'; } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { InputGroup } from 'primeng/inputgroup'; import { InputGroup } from 'primeng/inputgroup';
import { InputGroupAddon } from 'primeng/inputgroupaddon'; import { InputGroupAddon } from 'primeng/inputgroupaddon';
@@ -42,6 +44,7 @@ interface IAmountPercentageInputSelect {
], ],
}) })
export class AmountPercentageInputComponent { export class AmountPercentageInputComponent {
private readonly destroyRef = inject(DestroyRef);
@Input() defaultType: TAmountPercentageInput = 'percentage'; @Input() defaultType: TAmountPercentageInput = 'percentage';
@Input({ required: true }) percentageControl!: FormControl<Maybe<any>>; @Input({ required: true }) percentageControl!: FormControl<Maybe<any>>;
@Input({ required: true }) amountControl!: FormControl<Maybe<any>>; @Input({ required: true }) amountControl!: FormControl<Maybe<any>>;
@@ -146,17 +149,17 @@ export class AmountPercentageInputComponent {
if (isPercentageType) { if (isPercentageType) {
const amountValue = (this.baseAmount * newValue) / 100; const amountValue = (this.baseAmount * newValue) / 100;
newValueToSet = newValue + ''; newValueToSet = newValue + '';
this.amountControl.setValue(isNaN(amountValue) ? 0 : amountValue); this.amountControl.setValue(isNaN(amountValue) ? 0 : amountValue, { emitEvent: false });
if (notValid) { if (notValid) {
this.percentageControl.setValue(newValue); this.percentageControl.setValue(newValue, { emitEvent: false });
} }
} else { } else {
const percentageValue = ((newValue / this.baseAmount) * 100).toFixed(2); const percentageValue = ((newValue / this.baseAmount) * 100).toFixed(2);
newValueToSet = newValue + ''; newValueToSet = newValue + '';
this.percentageControl.setValue(percentageValue); this.percentageControl.setValue(percentageValue, { emitEvent: false });
this.preparedLabel.set(`${this.label} (${percentageValue} درصد)`); this.preparedLabel.set(`${this.label} (${percentageValue} درصد)`);
if (notValid) { if (notValid) {
this.amountControl.setValue(newValue); this.amountControl.setValue(newValue, { emitEvent: false });
} }
} }
this.preparedLabel.set(this.prepareLabel(isPercentageType)); this.preparedLabel.set(this.prepareLabel(isPercentageType));
@@ -177,7 +180,7 @@ export class AmountPercentageInputComponent {
isPercentageType ? this.percentageControl.value : this.amountControl.value, isPercentageType ? this.percentageControl.value : this.amountControl.value,
); );
this.selectedType.valueChanges.subscribe((value) => { this.selectedType.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((value) => {
this.preparedLabel.set(this.prepareLabel(value === 'percentage')); this.preparedLabel.set(this.prepareLabel(value === 'percentage'));
}); });
} }
@@ -187,9 +190,5 @@ export class AmountPercentageInputComponent {
this.modifyControlsData( this.modifyControlsData(
isPercentageType ? this.percentageControl.value : this.amountControl.value, isPercentageType ? this.percentageControl.value : this.amountControl.value,
); );
this.selectedType.valueChanges.subscribe((value) => {
this.preparedLabel.set(this.prepareLabel(value === 'percentage'));
});
} }
} }