Browse Source

Refactoring

pull/4035/head
Thomas Kaul 9 months ago
parent
commit
039233ed56
  1. 73
      apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts

73
apps/client/src/app/pages/portfolio/activities/create-or-update-activity-dialog/create-or-update-activity-dialog.component.ts

@ -148,6 +148,7 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
], ],
updateAccountBalance: [false] updateAccountBalance: [false]
}); });
this.activityForm.valueChanges this.activityForm.valueChanges
.pipe( .pipe(
// Slightly delay until the more specific form control value changes have // Slightly delay until the more specific form control value changes have
@ -156,23 +157,30 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
takeUntil(this.unsubscribeSubject) takeUntil(this.unsubscribeSubject)
) )
.subscribe(async () => { .subscribe(async () => {
const activity = { quantity: this.activityForm.get('quantity').value };
try { try {
if (this.mode === 'create') { if (this.mode === 'create') {
const activity = this.getActivity() as CreateOrderDto;
await validateObjectForForm({ await validateObjectForForm({
classDto: CreateOrderDto, classDto: CreateOrderDto,
form: this.activityForm, form: this.activityForm,
ignoreFields: ['dataSource', 'date'],
object: activity object: activity
}); });
} else { } else {
const activity = this.getActivity() as UpdateOrderDto;
await validateObjectForForm({ await validateObjectForForm({
classDto: UpdateOrderDto, classDto: UpdateOrderDto,
form: this.activityForm, form: this.activityForm,
ignoreFields: ['dataSource', 'date'],
object: activity object: activity
}); });
} }
} catch (error) {} } catch (error) {
console.error(error);
}
let exchangeRateOfUnitPrice = 1; let exchangeRateOfUnitPrice = 1;
this.activityForm.get('feeInCustomCurrency').setErrors(null); this.activityForm.get('feeInCustomCurrency').setErrors(null);
@ -480,31 +488,13 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
} }
public async onSubmit() { public async onSubmit() {
const activity: CreateOrderDto | UpdateOrderDto = {
accountId: this.activityForm.get('accountId').value,
assetClass: this.activityForm.get('assetClass').value,
assetSubClass: this.activityForm.get('assetSubClass').value,
comment: this.activityForm.get('comment').value || null,
currency: this.activityForm.get('currency').value,
customCurrency: this.activityForm.get('currencyOfUnitPrice').value,
date: this.activityForm.get('date').value,
dataSource: this.activityForm.get('dataSource').value,
fee: this.activityForm.get('fee').value,
quantity: this.activityForm.get('quantity').value,
symbol:
this.activityForm.get('searchSymbol').value?.symbol === undefined ||
isUUID(this.activityForm.get('searchSymbol').value?.symbol)
? this.activityForm.get('name').value
: this.activityForm.get('searchSymbol').value.symbol,
tags: this.activityForm.get('tags').value,
type: this.activityForm.get('type').value,
unitPrice: this.activityForm.get('unitPrice').value
};
try { try {
if (this.mode === 'create') { if (this.mode === 'create') {
(activity as CreateOrderDto).updateAccountBalance = const activity = this.getActivity() as CreateOrderDto;
this.activityForm.get('updateAccountBalance').value;
activity.updateAccountBalance = this.activityForm.get(
'updateAccountBalance'
).value;
await validateObjectForForm({ await validateObjectForForm({
classDto: CreateOrderDto, classDto: CreateOrderDto,
@ -515,16 +505,18 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
this.dialogRef.close(activity as CreateOrderDto); this.dialogRef.close(activity as CreateOrderDto);
} else { } else {
(activity as UpdateOrderDto).id = this.data.activity.id; const activity = this.getActivity() as UpdateOrderDto;
activity.id = this.data.activity.id;
await validateObjectForForm({ await validateObjectForForm({
classDto: UpdateOrderDto, classDto: UpdateOrderDto,
form: this.activityForm, form: this.activityForm,
ignoreFields: ['dataSource', 'date'], ignoreFields: ['dataSource', 'date'],
object: activity as UpdateOrderDto object: activity
}); });
this.dialogRef.close(activity as UpdateOrderDto); this.dialogRef.close(activity);
} }
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@ -546,6 +538,29 @@ export class CreateOrUpdateActivityDialog implements OnDestroy {
}); });
} }
private getActivity(): CreateOrderDto | UpdateOrderDto {
return {
accountId: this.activityForm.get('accountId').value,
assetClass: this.activityForm.get('assetClass').value,
assetSubClass: this.activityForm.get('assetSubClass').value,
comment: this.activityForm.get('comment').value || null,
currency: this.activityForm.get('currency').value,
customCurrency: this.activityForm.get('currencyOfUnitPrice').value,
date: this.activityForm.get('date').value,
dataSource: this.activityForm.get('dataSource').value,
fee: this.activityForm.get('fee').value,
quantity: this.activityForm.get('quantity').value,
symbol:
this.activityForm.get('searchSymbol').value?.symbol === undefined ||
isUUID(this.activityForm.get('searchSymbol').value?.symbol)
? this.activityForm.get('name').value
: this.activityForm.get('searchSymbol').value.symbol,
tags: this.activityForm.get('tags').value,
type: this.activityForm.get('type').value,
unitPrice: this.activityForm.get('unitPrice').value
};
}
private updateSymbol() { private updateSymbol() {
this.isLoading = true; this.isLoading = true;
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();

Loading…
Cancel
Save