This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add key to each
CartTotalItem
(#4240)
* Move type defs * Move type guards * Fix imports * Extract prepareTotalItems to TS file * usePaymentMethodInterface as TS file * Fix TS props * Fix currency type defs * Add return type to usePaymentMethodInterface * Add key prop to CartTotalItem * Fixed up js tests * Move SymbolPosition into type-defs package Co-authored-by: Thomas Roberts <[email protected]>
- Loading branch information
Showing
40 changed files
with
209 additions
and
169 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
CartResponseTotals, | ||
objectHasProp, | ||
isString, | ||
} from '@woocommerce/types'; | ||
|
||
export interface CartTotalItem { | ||
key: string; | ||
label: string; | ||
value: number; | ||
valueWithTax: number; | ||
} | ||
|
||
/** | ||
* Prepares the total items into a shape usable for display as passed on to | ||
* registered payment methods. | ||
* | ||
* @param {Object} totals Current cart total items | ||
* @param {boolean} needsShipping Whether or not shipping is needed. | ||
*/ | ||
export const prepareTotalItems = ( | ||
totals: CartResponseTotals, | ||
needsShipping: boolean | ||
): CartTotalItem[] => { | ||
const newTotals = []; | ||
|
||
const factory = ( label: string, property: string ): CartTotalItem => { | ||
const taxProperty = property + '_tax'; | ||
const value = | ||
objectHasProp( totals, property ) && isString( totals[ property ] ) | ||
? parseInt( totals[ property ] as string, 10 ) | ||
: 0; | ||
const tax = | ||
objectHasProp( totals, taxProperty ) && | ||
isString( totals[ taxProperty ] ) | ||
? parseInt( totals[ taxProperty ] as string, 10 ) | ||
: 0; | ||
return { | ||
key: property, | ||
label, | ||
value, | ||
valueWithTax: value + tax, | ||
}; | ||
}; | ||
|
||
newTotals.push( | ||
factory( | ||
__( 'Subtotal:', 'woo-gutenberg-products-block' ), | ||
'total_items' | ||
) | ||
); | ||
|
||
newTotals.push( | ||
factory( __( 'Fees:', 'woo-gutenberg-products-block' ), 'total_fees' ) | ||
); | ||
|
||
newTotals.push( | ||
factory( | ||
__( 'Discount:', 'woo-gutenberg-products-block' ), | ||
'total_discount' | ||
) | ||
); | ||
|
||
newTotals.push( { | ||
key: 'total_tax', | ||
label: __( 'Taxes:', 'woo-gutenberg-products-block' ), | ||
value: parseInt( totals.total_tax, 10 ), | ||
valueWithTax: parseInt( totals.total_tax, 10 ), | ||
} ); | ||
|
||
if ( needsShipping ) { | ||
newTotals.push( | ||
factory( | ||
__( 'Shipping:', 'woo-gutenberg-products-block' ), | ||
'total_shipping' | ||
) | ||
); | ||
} | ||
|
||
return newTotals; | ||
}; |
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
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
Oops, something went wrong.