Skip to content

Commit

Permalink
Take precision settings into consideration.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvinb committed Sep 27, 2024
1 parent 5f2f291 commit 03b1167
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 7 additions & 4 deletions js/src/components/paid-ads/validateCampaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
),
};
}
Expand Down
8 changes: 6 additions & 2 deletions js/src/hooks/useValidateCampaignWithCountryCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<CountryCode>) => 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.
Expand All @@ -39,7 +40,7 @@ import getHighestBudget from '.~/utils/getHighestBudget';
const useValidateCampaignWithCountryCodes = ( initialCountryCodes ) => {
const {
formatAmount,
adsCurrencyConfig: { code },
adsCurrencyConfig: { code, precision },
} = useAdsCurrency();
const [ countryCodes, setCountryCodes ] = useState( [] );

Expand All @@ -60,6 +61,7 @@ const useValidateCampaignWithCountryCodes = ( initialCountryCodes ) => {
formatAmount,
refreshCountryCodes: setCountryCodes,
currencyCode: code,
precision,
loaded: true,
};
}
Expand All @@ -85,19 +87,21 @@ const useValidateCampaignWithCountryCodes = ( initialCountryCodes ) => {
return validateCampaign( values, {
dailyBudget: budget?.daily_budget,
formatAmount,
precision,
} );
};

return {
validateCampaignWithCountryCodes,
dailyBudget: budget?.daily_budget,
formatAmount,
precision,
refreshCountryCodes: setCountryCodes,
currencyCode: code,
loaded,
};
},
[ countryCodes, formatAmount, code, initialCountryCodes ]
[ countryCodes, formatAmount, code, initialCountryCodes, precision ]
);
};

Expand Down

0 comments on commit 03b1167

Please sign in to comment.