-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(admin-ui): Extract PaymentForRefundSelectorComponent
- Loading branch information
1 parent
ac4c762
commit 9a1530c
Showing
7 changed files
with
93 additions
and
62 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
packages/admin-ui/src/lib/order/src/common/get-refundable-payments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OrderDetailFragment['payments']>[number]; | ||
export type RefundablePayment = Payment & { | ||
refundableAmount: number; | ||
amountToRefundControl: FormControl<number>; | ||
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, | ||
}; | ||
}); | ||
} |
Empty file.
38 changes: 38 additions & 0 deletions
38
...der/src/components/payment-for-refund-selector/payment-for-refund-selector.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<vdr-card | ||
[title]="'order.payment' | translate" | ||
[class.selected]="payment.selected" | ||
[class.unselected]="!payment.selected" | ||
*ngFor="let payment of refundablePayments" | ||
> | ||
<ng-template vdrCardControls> | ||
<vdr-select-toggle | ||
size="small" | ||
[title]="'order.refund-this-payment' | translate" | ||
[label]="'order.refund-this-payment' | translate" | ||
[disabled]="refundablePayments.length === 1" | ||
[(selected)]="payment.selected" | ||
(selectedChange)="paymentSelected.emit({ payment: payment, selected: $event })" | ||
></vdr-select-toggle> | ||
</ng-template> | ||
<div class="form-grid"> | ||
<vdr-labeled-data [label]="'order.payment-method' | translate"> | ||
{{ payment.method }} | ||
</vdr-labeled-data> | ||
<vdr-labeled-data [label]="'order.transaction-id' | translate"> | ||
{{ payment.transactionId }} | ||
</vdr-labeled-data> | ||
<vdr-labeled-data [label]="'order.payment-amount' | translate"> | ||
{{ payment.amount | localeCurrency : order.currencyCode }} | ||
</vdr-labeled-data> | ||
<vdr-labeled-data [label]="'order.refundable-amount' | translate"> | ||
{{ payment.refundableAmount | localeCurrency : order.currencyCode }} | ||
</vdr-labeled-data> | ||
</div> | ||
<vdr-form-field [label]="'order.refund-amount' | translate"> | ||
<vdr-currency-input | ||
[readonly]="!payment.selected" | ||
[currencyCode]="order.currencyCode" | ||
[formControl]="payment.amountToRefundControl" | ||
></vdr-currency-input> | ||
</vdr-form-field> | ||
</vdr-card> |
15 changes: 15 additions & 0 deletions
15
...order/src/components/payment-for-refund-selector/payment-for-refund-selector.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters