-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useValidateCampaignWithCountryCodes hook.
- Loading branch information
Showing
6 changed files
with
92 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import useFetchBudgetRecommendationEffect from './useFetchBudgetRecommendationEffect'; | ||
import getHighestBudget from '.~/utils/getHighestBudget'; | ||
import useStoreCurrency from './useStoreCurrency'; | ||
|
||
/** | ||
* @typedef { import(".~/data/actions").CountryCode } CountryCode | ||
*/ | ||
|
||
/** | ||
* Fetch the highest budget recommendation for countries in a side effect. | ||
* | ||
* @param {Array<CountryCode>} countryCodes Country code array. | ||
* @return {Object} Budget recommendation. | ||
*/ | ||
const useHighestBudgetRecommendation = ( countryCodes ) => { | ||
const { code: currency, formatNumber } = useStoreCurrency(); | ||
const { data: budgetData } = | ||
useFetchBudgetRecommendationEffect( countryCodes ); | ||
const budget = getHighestBudget( budgetData?.recommendations ); | ||
|
||
return { | ||
dailyBudget: budget?.daily_budget, | ||
currency, | ||
formatNumber, | ||
}; | ||
}; | ||
|
||
export default useHighestBudgetRecommendation; |
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,42 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import useHighestBudgetRecommendation from './useHighestBudgetRecommendation'; | ||
import validateCampaign from '.~/components/paid-ads/validateCampaign'; | ||
|
||
/** | ||
* @typedef {import('.~/components/types.js').CampaignFormValues} CampaignFormValues | ||
*/ | ||
|
||
/** | ||
* @typedef { import(".~/data/actions").CountryCode } CountryCode | ||
*/ | ||
|
||
/** | ||
* Validate campaign form. Accepts the form values object and returns errors object. | ||
* | ||
* @param {Array<CountryCode>} countryCodes Country code array. | ||
* @return {Object} errors. | ||
*/ | ||
const useValidateCampaignWithCountryCodes = ( countryCodes ) => { | ||
const { currency, dailyBudget, formatNumber } = | ||
useHighestBudgetRecommendation( countryCodes ); | ||
|
||
/** | ||
* Validate campaign form. Accepts the form values object and returns errors object. | ||
* | ||
* @param {CampaignFormValues} values Campaign form values. | ||
* @return {Object} errors. | ||
*/ | ||
const validateCampaignWithCountryCodes = ( values ) => { | ||
return validateCampaign( values, { | ||
currency, | ||
dailyBudget, | ||
formatNumber, | ||
} ); | ||
}; | ||
|
||
return { validateCampaignWithCountryCodes }; | ||
}; | ||
|
||
export default useValidateCampaignWithCountryCodes; |
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