Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement @woocommerce/price-format dependency #29

Open
wants to merge 25 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6fc3130
Implement @woocommerce/price-format dependency
nielslange Nov 27, 2021
b6204e9
Update dependency @wordpress/components to v19.1.5
renovate-bot Dec 14, 2021
e0ab51f
Remove obsolete checks
nielslange Dec 16, 2021
8d3ba4c
Add default value showing insufficient cart totals progress bar
nielslange Dec 16, 2021
e85f38c
Replace getCurrencyFormat() with getCurrencyFromPriceResponse()
nielslange Dec 16, 2021
fce48d6
Merge branch 'trunk' into add/28-implement-@woocommerce-price-format-…
nielslange Dec 20, 2021
aadbd61
Merge branch 'trunk' into renovate/wordpress-monorepo
nielslange Dec 20, 2021
f7518da
Merge pull request #36 from woocommerce/renovate/wordpress-monorepo
nielslange Dec 20, 2021
2ce76b9
Update wordpress monorepo
renovate-bot Jan 17, 2022
9974eaf
Merge pull request #38 from woocommerce/renovate/wordpress-monorepo
nielslange Jan 24, 2022
63aac05
Remove obsolete icon
nielslange Jan 24, 2022
c48fcc2
Merge pull request #46 from woocommerce/fix/45-remove-obsolete-icon
nielslange Jan 24, 2022
bff7992
Adjust getMinorUnit() function
nielslange Jan 25, 2022
e1c27dc
Use default current totals value from block.json
nielslange Jan 25, 2022
d272664
Remove obsolete attributes from tests
nielslange Jan 25, 2022
fcefbeb
Implement @woocommerce/price-format dependency
nielslange Jan 25, 2022
30f5a83
Remove obsolete checks
nielslange Dec 16, 2021
5c7f6ab
Add default value showing insufficient cart totals progress bar
nielslange Dec 16, 2021
2cf0023
Replace getCurrencyFormat() with getCurrencyFromPriceResponse()
nielslange Dec 16, 2021
1d9e48d
Adjust getMinorUnit() function
nielslange Jan 25, 2022
706a248
Use default current totals value from block.json
nielslange Jan 25, 2022
96d7c07
Remove obsolete attributes from tests
nielslange Jan 25, 2022
0aa53b1
Interim Jest mock commit
nielslange Jan 28, 2022
e6572fb
Merge branch 'add/28-implement-@woocommerce-price-format-dependency' …
nielslange Jan 28, 2022
995f5a0
Fix failing integration test
nielslange Jan 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import ProgressLabel from './components/progress-label';
import ProgressBar from './components/progress-bar';
import './style.scss';

export default function Block( attributes ) {
return (
Expand Down
13 changes: 12 additions & 1 deletion src/components/progress-bar/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
const ProgressBar = ( { currentTotal, freeShippingFrom } ) => {
/**
* External dependencies
*/
import { formatPrice } from '@woocommerce/price-format';

/**
* Internal dependencies
*/
import { getCurrentTotal } from '../../util';

const ProgressBar = ( { freeShippingFrom, cart } ) => {
const currentTotal = getCurrentTotal( cart );
const progress = ( currentTotal / freeShippingFrom ) * 100;
const divWidth = ( progress > 100 ? 100 : progress ) + '%';
const divStyle = { width: divWidth };
Expand Down
23 changes: 20 additions & 3 deletions src/components/progress-label/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
/**
* External dependencies
*/
import {
formatPrice,
getCurrencyFromPriceResponse,
} from '@woocommerce/price-format';

/**
* Internal dependencies
*/
import { getCurrentTotal, getMinorUnit, getRemaining } from '../../util';

const ProgressLabel = ( {
currentTotal,
freeShippingFrom,
labelInsufficientTotals,
labelSufficientTotals,
cart,
} ) => {
const remaining = Number( freeShippingFrom - currentTotal ).toFixed( 2 );
const currentTotal = getCurrentTotal( cart );
const minorUnit = getMinorUnit( cart );
const remaining = getRemaining( freeShippingFrom, currentTotal, minorUnit );
const currency = getCurrencyFromPriceResponse( cart );
const price = formatPrice( remaining, currency );
const message =
remaining > 0
? labelInsufficientTotals.replace( '{amount}', remaining )
? labelInsufficientTotals.replace( '{amount}', price )
: labelSufficientTotals;

return (
Expand Down
1 change: 0 additions & 1 deletion src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import ProgressBar from './components/progress-bar';
import useViewSwitcher from './components/use-view-switcher';
import { notice } from './constants';
import { Icon, progressBarHalf, progressBarFull } from './icons';
import './style.scss';

const BlockSettings = ( { attributes, setAttributes } ) => {
const { freeShippingFrom } = attributes;
Expand Down
44 changes: 44 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Get the current totals.
*
* @param {Object} cart The cart object.
* @returns {Number} The totals of the cart object, if available, else the totals of the cart progress bar preview.
*/
export function getCurrentTotal( cart ) {
if ( undefined !== cart ) {
const total_price = cart.cartTotals.total_price;
const minor_unit = cart.cartTotals.currency_minor_unit;

return total_price / Math.pow( 10, minor_unit );
mikejolley marked this conversation as resolved.
Show resolved Hide resolved
}
return 15;
nielslange marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Get the minor unit.
*
* @param {Object} cart The cart object.
* @returns {Number} The totals of the cart object, if available, else the totals of the cart progress bar preview.
*/
export function getMinorUnit( cart ) {
if ( undefined !== cart ) {
return cart.cartTotals.currency_minor_unit;
nielslange marked this conversation as resolved.
Show resolved Hide resolved
}
return '';
nielslange marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Get the remaining value.
*
* @param {Number} freeShippingFrom The free shipping from value.
* @param {Number} currentTotal The current total value.
* @param {Number} minorUnit The minor unit value.
* @returns {Number} The difference between freeShippingFrom and currentTotal.
*/
export function getRemaining( freeShippingFrom, currentTotal, minorUnit ) {
const remaining = freeShippingFrom - currentTotal;
const power = Math.pow( 10, minorUnit );
const result = remaining.toFixed( minorUnit ) * power;

return result;
mikejolley marked this conversation as resolved.
Show resolved Hide resolved
}
29 changes: 29 additions & 0 deletions src/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Internal dependencies
*/
import { getCurrentTotal, getMinorUnit, getRemaining } from './util';

describe( 'The function getRemaining()', () => {
test.each`
freeShippingFrom | currentTotal | minorUnit | expected
${ 50 } | ${ 48 } | ${ 2 } | ${ 200 }
${ 50 } | ${ 48 } | ${ 3 } | ${ 2000 }
${ 50 } | ${ 48 } | ${ 0 } | ${ 2 }
${ 50 } | ${ 23.5 } | ${ 2 } | ${ 2650 }
${ 50 } | ${ 23.05 } | ${ 2 } | ${ 2695 }
${ 50 } | ${ 23.5 } | ${ 3 } | ${ 26500 }
${ 50 } | ${ 23.05 } | ${ 3 } | ${ 26950 }
${ 50 } | ${ 23.5 } | ${ 0 } | ${ 27 }
${ 50 } | ${ 23.05 } | ${ 0 } | ${ 27 }
`(
'correctly returns the remaining price given $minorUnit minorUnit, $freeShippingFrom freeShippingFrom and $currentTotal currentTotal as $expected',
( { minorUnit, freeShippingFrom, currentTotal, expected } ) => {
const remaining = getRemaining(
freeShippingFrom,
currentTotal,
minorUnit
);
expect( remaining ).toEqual( expected );
}
);
} );
2 changes: 2 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ const WooCommerceDependencyExtractionWebpackPlugin = require( '@wordpress/depend

const wcDepMap = {
'@woocommerce/blocks-checkout': [ 'wc', 'blocksCheckout' ],
'@woocommerce/price-format': [ 'wc', 'priceFormat' ],
};

const wcHandleMap = {
'@woocommerce/blocks-checkout': 'wc-blocks-checkout',
'@woocommerce/price-format': 'wc-price-format',
};

const requestToExternal = ( request ) => {
Expand Down