From 9a1530c6106db0f5027395f8646a4f6ba9249c31 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Fri, 19 Jan 2024 13:16:22 +0100 Subject: [PATCH] refactor(admin-ui): Extract PaymentForRefundSelectorComponent --- .../src/common/get-refundable-payments.ts | 31 +++++++++++++ .../payment-for-refund-selector.component.css | 0 ...payment-for-refund-selector.component.html | 38 ++++++++++++++++ .../payment-for-refund-selector.component.ts | 15 +++++++ .../refund-order-dialog.component.html | 43 +++---------------- .../refund-order-dialog.component.ts | 26 +---------- .../src/lib/order/src/order.module.ts | 2 + 7 files changed, 93 insertions(+), 62 deletions(-) create mode 100644 packages/admin-ui/src/lib/order/src/common/get-refundable-payments.ts create mode 100644 packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.css create mode 100644 packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.html create mode 100644 packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.ts diff --git a/packages/admin-ui/src/lib/order/src/common/get-refundable-payments.ts b/packages/admin-ui/src/lib/order/src/common/get-refundable-payments.ts new file mode 100644 index 0000000000..1add613b9b --- /dev/null +++ b/packages/admin-ui/src/lib/order/src/common/get-refundable-payments.ts @@ -0,0 +1,31 @@ +import { FormControl, Validators } from '@angular/forms'; +import { OrderDetailFragment } from '@vendure/admin-ui/core'; +import { summate } from '@vendure/common/lib/shared-utils'; + +export type Payment = NonNullable[number]; +export type RefundablePayment = Payment & { + refundableAmount: number; + amountToRefundControl: FormControl; + selected: boolean; +}; + +export function getRefundablePayments(payments: OrderDetailFragment['payments']): RefundablePayment[] { + const settledPayments = (payments || []).filter(p => p.state === 'Settled'); + return settledPayments.map((payment, index) => { + const refundableAmount = + payment.amount - + summate( + payment.refunds.filter(r => r.state !== 'Failed'), + 'total', + ); + return { + ...payment, + refundableAmount, + amountToRefundControl: new FormControl(0, { + nonNullable: true, + validators: [Validators.min(0), Validators.max(refundableAmount)], + }), + selected: index === 0, + }; + }); +} diff --git a/packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.css b/packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.html b/packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.html new file mode 100644 index 0000000000..2f54a4f927 --- /dev/null +++ b/packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.html @@ -0,0 +1,38 @@ + + + + +
+ + {{ payment.method }} + + + {{ payment.transactionId }} + + + {{ payment.amount | localeCurrency : order.currencyCode }} + + + {{ payment.refundableAmount | localeCurrency : order.currencyCode }} + +
+ + + +
\ No newline at end of file diff --git a/packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.ts b/packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.ts new file mode 100644 index 0000000000..06cf60b3c7 --- /dev/null +++ b/packages/admin-ui/src/lib/order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.ts @@ -0,0 +1,15 @@ +import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core'; +import { OrderDetailFragment } from '@vendure/admin-ui/core'; +import { RefundablePayment } from '../../common/get-refundable-payments'; + +@Component({ + selector: 'vdr-payment-for-refund-selector', + templateUrl: './payment-for-refund-selector.component.html', + styleUrls: ['./payment-for-refund-selector.component.css'], + changeDetection: ChangeDetectionStrategy.Default, +}) +export class PaymentForRefundSelectorComponent { + @Input() refundablePayments: RefundablePayment[]; + @Input() order: OrderDetailFragment; + @Output() paymentSelected = new EventEmitter<{ payment: RefundablePayment; selected: boolean }>(); +} diff --git a/packages/admin-ui/src/lib/order/src/components/refund-order-dialog/refund-order-dialog.component.html b/packages/admin-ui/src/lib/order/src/components/refund-order-dialog/refund-order-dialog.component.html index 0ba19a269d..6a25c740df 100644 --- a/packages/admin-ui/src/lib/order/src/components/refund-order-dialog/refund-order-dialog.component.html +++ b/packages/admin-ui/src/lib/order/src/components/refund-order-dialog/refund-order-dialog.component.html @@ -147,44 +147,11 @@
- - - - -
- - {{ payment.method }} - - - {{ payment.transactionId }} - - - {{ payment.amount | localeCurrency : order.currencyCode }} - - - {{ payment.refundableAmount | localeCurrency : order.currencyCode }} - -
- - - -
+
diff --git a/packages/admin-ui/src/lib/order/src/components/refund-order-dialog/refund-order-dialog.component.ts b/packages/admin-ui/src/lib/order/src/components/refund-order-dialog/refund-order-dialog.component.ts index 1a5932ff36..da553e0ebb 100644 --- a/packages/admin-ui/src/lib/order/src/components/refund-order-dialog/refund-order-dialog.component.ts +++ b/packages/admin-ui/src/lib/order/src/components/refund-order-dialog/refund-order-dialog.component.ts @@ -12,14 +12,9 @@ import { RefundOrderInput, } from '@vendure/admin-ui/core'; import { summate } from '@vendure/common/lib/shared-utils'; +import { getRefundablePayments, RefundablePayment } from '../../common/get-refundable-payments'; type SelectionLine = { quantity: number; cancel: boolean }; -type Payment = NonNullable[number]; -type RefundablePayment = Payment & { - refundableAmount: number; - amountToRefundControl: FormControl; - selected: boolean; -}; @Component({ selector: 'vdr-refund-order-dialog', @@ -88,24 +83,7 @@ export class RefundOrderDialogComponent }), {}, ); - const settledPayments = (this.order.payments || []).filter(p => p.state === 'Settled'); - this.refundablePayments = settledPayments.map((payment, index) => { - const refundableAmount = - payment.amount - - summate( - payment.refunds.filter(r => r.state !== 'Failed'), - 'total', - ); - return { - ...payment, - refundableAmount, - amountToRefundControl: new FormControl(0, { - nonNullable: true, - validators: [Validators.min(0), Validators.max(refundableAmount)], - }), - selected: index === 0, - }; - }); + this.refundablePayments = getRefundablePayments(this.order.payments); } updateRefundTotal() { diff --git a/packages/admin-ui/src/lib/order/src/order.module.ts b/packages/admin-ui/src/lib/order/src/order.module.ts index af659328e3..1cbba179a6 100644 --- a/packages/admin-ui/src/lib/order/src/order.module.ts +++ b/packages/admin-ui/src/lib/order/src/order.module.ts @@ -48,6 +48,7 @@ import { SettleRefundDialogComponent } from './components/settle-refund-dialog/s import { SimpleItemListComponent } from './components/simple-item-list/simple-item-list.component'; import { createRoutes } from './order.routes'; import { OrderDataTableComponent } from './components/order-data-table/order-data-table.component'; +import { PaymentForRefundSelectorComponent } from './components/payment-for-refund-selector/payment-for-refund-selector.component'; @NgModule({ imports: [SharedModule, RouterModule.forChild([])], @@ -98,6 +99,7 @@ import { OrderDataTableComponent } from './components/order-data-table/order-dat SellerOrdersCardComponent, OrderDataTableComponent, OrderTotalColumnComponent, + PaymentForRefundSelectorComponent, ], exports: [OrderCustomFieldsCardComponent], })