From 03b11679a80b80a013f40ca898c1a56e79422582 Mon Sep 17 00:00:00 2001 From: asvinb Date: Fri, 27 Sep 2024 14:50:30 +0400 Subject: [PATCH] Take precision settings into consideration. --- js/src/components/paid-ads/validateCampaign.js | 11 +++++++---- js/src/hooks/useValidateCampaignWithCountryCodes.js | 8 ++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/js/src/components/paid-ads/validateCampaign.js b/js/src/components/paid-ads/validateCampaign.js index 641068b29f..2662204568 100644 --- a/js/src/components/paid-ads/validateCampaign.js +++ b/js/src/components/paid-ads/validateCampaign.js @@ -31,18 +31,21 @@ const validateCampaign = ( values, opts ) => { Number.isFinite( opts?.dailyBudget ) ) { const { amount } = values; - const { dailyBudget, formatAmount } = opts; - const minAmount = Math.round( dailyBudget * BUDGET_MIN_PERCENT ); + const { dailyBudget, formatAmount, precision } = opts; + + const minAmount = parseFloat( + ( dailyBudget * BUDGET_MIN_PERCENT ).toFixed( precision ) + ); if ( amount < minAmount ) { return { amount: sprintf( /* translators: %1$s: minimum daily budget */ __( - 'Please make sure daily average cost is greater than %s.', + 'Please make sure daily average cost is at least %s.', 'google-listings-and-ads' ), - formatAmount( minAmount - 1 ) + formatAmount( minAmount ) ), }; } diff --git a/js/src/hooks/useValidateCampaignWithCountryCodes.js b/js/src/hooks/useValidateCampaignWithCountryCodes.js index cc135ab4d1..1ac8176277 100644 --- a/js/src/hooks/useValidateCampaignWithCountryCodes.js +++ b/js/src/hooks/useValidateCampaignWithCountryCodes.js @@ -25,6 +25,7 @@ import getHighestBudget from '.~/utils/getHighestBudget'; * @property {(values: CampaignFormValues) => Object} validateCampaignWithCountryCodes A function to validate campaign form values. * @property {number | undefined} dailyBudget The daily budget recommendation. * @property {(number: string | number) => string} formatAmount A function to format an amount according to the user's currency settings. + * @property {number} precision Number of decimal places after the decimal separator. * @property {(countryCodes: Array) => void} refreshCountryCodes A function to refresh the country codes. * @property {string} currencyCode The currency code. * @property {boolean} loaded A boolean indicating whether the budget recommendation data has been resolved and the code currency available. @@ -39,7 +40,7 @@ import getHighestBudget from '.~/utils/getHighestBudget'; const useValidateCampaignWithCountryCodes = ( initialCountryCodes ) => { const { formatAmount, - adsCurrencyConfig: { code }, + adsCurrencyConfig: { code, precision }, } = useAdsCurrency(); const [ countryCodes, setCountryCodes ] = useState( [] ); @@ -60,6 +61,7 @@ const useValidateCampaignWithCountryCodes = ( initialCountryCodes ) => { formatAmount, refreshCountryCodes: setCountryCodes, currencyCode: code, + precision, loaded: true, }; } @@ -85,6 +87,7 @@ const useValidateCampaignWithCountryCodes = ( initialCountryCodes ) => { return validateCampaign( values, { dailyBudget: budget?.daily_budget, formatAmount, + precision, } ); }; @@ -92,12 +95,13 @@ const useValidateCampaignWithCountryCodes = ( initialCountryCodes ) => { validateCampaignWithCountryCodes, dailyBudget: budget?.daily_budget, formatAmount, + precision, refreshCountryCodes: setCountryCodes, currencyCode: code, loaded, }; }, - [ countryCodes, formatAmount, code, initialCountryCodes ] + [ countryCodes, formatAmount, code, initialCountryCodes, precision ] ); };